From 63dd8f101816c1253ac8d05e10f4f8a4c80f296a Mon Sep 17 00:00:00 2001 From: shin- Date: Tue, 12 Mar 2013 04:24:26 -0700 Subject: [PATCH] Re-enabled cat, write and ls ; Added creation date to image metadata --- fs/changes.go | 17 ------ fs/store.go | 20 +++++++ server/server.go | 142 +++++++++++++++++++++++++---------------------- 3 files changed, 95 insertions(+), 84 deletions(-) diff --git a/fs/changes.go b/fs/changes.go index 9bd315ad82..659f688c45 100644 --- a/fs/changes.go +++ b/fs/changes.go @@ -125,20 +125,3 @@ func (mp *Mountpoint) Reset() error { } return nil } - -// Open opens the named file for reading. -// func (fs *Filesystem) OpenFile(path string, flag int, perm os.FileMode) (*os.File, error) { -// if err := fs.EnsureMounted(); err != nil { -// return nil, err -// } -// return os.OpenFile(filepath.Join(fs.RootFS, path), flag, perm) -// } - -// ReadDir reads the directory named by dirname, relative to the Filesystem's root, -// and returns a list of sorted directory entries -// func (fs *Filesystem) ReadDir(dirname string) ([]os.FileInfo, error) { -// if err := fs.EnsureMounted(); err != nil { -// return nil, err -// } -// return ioutil.ReadDir(filepath.Join(fs.RootFS, dirname)) -// } diff --git a/fs/store.go b/fs/store.go index c6612d9b19..b0fdadcc16 100644 --- a/fs/store.go +++ b/fs/store.go @@ -8,6 +8,7 @@ import ( _ "github.com/mattn/go-sqlite3" "github.com/shykes/gorp" //Forked to implement CreateTablesOpts "io" + "io/ioutil" "os" "path" "path/filepath" @@ -115,6 +116,7 @@ func (store *Store) Create(layerData Archive, parent *Image, pth, comment string img := &Image{ Id: future.RandomId(), Comment: comment, + Created: time.Now().Unix(), store: store, } // FIXME: we shouldn't have to pass os.Stderr to AddLayer()... @@ -163,6 +165,7 @@ type Image struct { Id string Parent string Comment string + Created int64 store *Store `db:"-"` } @@ -356,6 +359,23 @@ func (store *Store) FetchMountpoint(root, rw string) (*Mountpoint, error) { return mp, nil } +// OpenFile opens the named file for reading. +func (mp *Mountpoint) OpenFile(path string, flag int, perm os.FileMode) (*os.File, error) { + if err := mp.EnsureMounted(); err != nil { + return nil, err + } + return os.OpenFile(filepath.Join(mp.Root, path), flag, perm) +} + +// ReadDir reads the directory named by dirname, relative to the Mountpoint's root, +// and returns a list of sorted directory entries +func (mp *Mountpoint) ReadDir(dirname string) ([]os.FileInfo, error) { + if err := mp.EnsureMounted(); err != nil { + return nil, err + } + return ioutil.ReadDir(filepath.Join(mp.Root, dirname)) +} + func (store *Store) AddTag(imageId, tagName string) error { if image, err := store.Get(imageId); err != nil { return err diff --git a/server/server.go b/server/server.go index c403ed187a..654ca03c1a 100644 --- a/server/server.go +++ b/server/server.go @@ -89,10 +89,18 @@ func (srv *Server) CmdWait(stdin io.ReadCloser, stdout io.Writer, args ...string // 'docker info': display system-wide information. func (srv *Server) CmdInfo(stdin io.ReadCloser, stdout io.Writer, args ...string) error { + images, _ := srv.images.Images() + var imgcount int + if images == nil { + imgcount = 0 + } else { + imgcount = len(images) + } + fmt.Fprintf(stdout, "containers: %d\nversion: %s\nimages: %d\n", len(srv.containers.List()), VERSION, - 0) // FIXME: Number of images + imgcount) return nil } @@ -211,73 +219,73 @@ func (srv *Server) CmdMount(stdin io.ReadCloser, stdout io.Writer, args ...strin return nil } -// func (srv *Server) CmdCat(stdin io.ReadCloser, stdout io.Writer, args ...string) error { -// cmd := rcli.Subcmd(stdout, "cat", "[OPTIONS] CONTAINER PATH", "write the contents of a container's file to standard output") -// if err := cmd.Parse(args); err != nil { -// cmd.Usage() -// return nil -// } -// if cmd.NArg() < 2 { -// cmd.Usage() -// return nil -// } -// name, path := cmd.Arg(0), cmd.Arg(1) -// if container := srv.containers.Get(name); container != nil { -// if f, err := container.Mountpoint.OpenFile(path, os.O_RDONLY, 0); err != nil { -// return err -// } else if _, err := io.Copy(stdout, f); err != nil { -// return err -// } -// return nil -// } -// return errors.New("No such container: " + name) -// } +func (srv *Server) CmdCat(stdin io.ReadCloser, stdout io.Writer, args ...string) error { + cmd := rcli.Subcmd(stdout, "cat", "[OPTIONS] CONTAINER PATH", "write the contents of a container's file to standard output") + if err := cmd.Parse(args); err != nil { + cmd.Usage() + return nil + } + if cmd.NArg() < 2 { + cmd.Usage() + return nil + } + name, path := cmd.Arg(0), cmd.Arg(1) + if container := srv.containers.Get(name); container != nil { + if f, err := container.Mountpoint.OpenFile(path, os.O_RDONLY, 0); err != nil { + return err + } else if _, err := io.Copy(stdout, f); err != nil { + return err + } + return nil + } + return errors.New("No such container: " + name) +} -// func (srv *Server) CmdWrite(stdin io.ReadCloser, stdout io.Writer, args ...string) error { -// cmd := rcli.Subcmd(stdout, "write", "[OPTIONS] CONTAINER PATH", "write the contents of standard input to a container's file") -// if err := cmd.Parse(args); err != nil { -// cmd.Usage() -// return nil -// } -// if cmd.NArg() < 2 { -// cmd.Usage() -// return nil -// } -// name, path := cmd.Arg(0), cmd.Arg(1) -// if container := srv.containers.Get(name); container != nil { -// if f, err := container.Mountpoint.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600); err != nil { -// return err -// } else if _, err := io.Copy(f, stdin); err != nil { -// return err -// } -// return nil -// } -// return errors.New("No such container: " + name) -// } +func (srv *Server) CmdWrite(stdin io.ReadCloser, stdout io.Writer, args ...string) error { + cmd := rcli.Subcmd(stdout, "write", "[OPTIONS] CONTAINER PATH", "write the contents of standard input to a container's file") + if err := cmd.Parse(args); err != nil { + cmd.Usage() + return nil + } + if cmd.NArg() < 2 { + cmd.Usage() + return nil + } + name, path := cmd.Arg(0), cmd.Arg(1) + if container := srv.containers.Get(name); container != nil { + if f, err := container.Mountpoint.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600); err != nil { + return err + } else if _, err := io.Copy(f, stdin); err != nil { + return err + } + return nil + } + return errors.New("No such container: " + name) +} -// func (srv *Server) CmdLs(stdin io.ReadCloser, stdout io.Writer, args ...string) error { -// cmd := rcli.Subcmd(stdout, "ls", "[OPTIONS] CONTAINER PATH", "List the contents of a container's directory") -// if err := cmd.Parse(args); err != nil { -// cmd.Usage() -// return nil -// } -// if cmd.NArg() < 2 { -// cmd.Usage() -// return nil -// } -// name, path := cmd.Arg(0), cmd.Arg(1) -// if container := srv.containers.Get(name); container != nil { -// if files, err := container.Mountpoint.ReadDir(path); err != nil { -// return err -// } else { -// for _, f := range files { -// fmt.Fprintln(stdout, f.Name()) -// } -// } -// return nil -// } -// return errors.New("No such container: " + name) -// } +func (srv *Server) CmdLs(stdin io.ReadCloser, stdout io.Writer, args ...string) error { + cmd := rcli.Subcmd(stdout, "ls", "[OPTIONS] CONTAINER PATH", "List the contents of a container's directory") + if err := cmd.Parse(args); err != nil { + cmd.Usage() + return nil + } + if cmd.NArg() < 2 { + cmd.Usage() + return nil + } + name, path := cmd.Arg(0), cmd.Arg(1) + if container := srv.containers.Get(name); container != nil { + if files, err := container.Mountpoint.ReadDir(path); err != nil { + return err + } else { + for _, f := range files { + fmt.Fprintln(stdout, f.Name()) + } + } + return nil + } + return errors.New("No such container: " + name) +} func (srv *Server) CmdInspect(stdin io.ReadCloser, stdout io.Writer, args ...string) error { cmd := rcli.Subcmd(stdout, "inspect", "[OPTIONS] CONTAINER", "Return low-level information on a container") @@ -496,7 +504,7 @@ func (srv *Server) CmdImages(stdin io.ReadCloser, stdout io.Writer, args ...stri for idx, field := range []string{ /* NAME */ name, /* ID */ img.Id, - /* CREATED */ future.HumanDuration(time.Now().Sub(time.Now())) + " ago", // FIXME: should be img.Created + /* CREATED */ future.HumanDuration(time.Now().Sub(time.Unix(img.Created, 0))) + " ago", /* PARENT */ img.Parent, } { if idx == 0 {