From 2a3934f1dae43589c50df8fa545d20405f64d7af Mon Sep 17 00:00:00 2001 From: Urvashi Mohnani Date: Tue, 21 Nov 2017 16:51:17 -0500 Subject: Update kpod export to use the new container state and runtime Signed-off-by: Urvashi Mohnani Closes: #59 Approved by: rhatdan --- libpod/container.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'libpod') diff --git a/libpod/container.go b/libpod/container.go index f167adc35..9b48d2ca1 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -3,12 +3,15 @@ package libpod import ( "encoding/json" "fmt" + "io" "io/ioutil" + "os" "path/filepath" "sync" "time" "github.com/containers/storage" + "github.com/containers/storage/pkg/archive" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/term" spec "github.com/opencontainers/runtime-spec/specs-go" @@ -469,7 +472,40 @@ func (c *Container) Unpause() error { // Export exports a container's root filesystem as a tar archive // The archive will be saved as a file at the given path func (c *Container) Export(path string) error { - return ErrNotImplemented + c.lock.Lock() + defer c.lock.Unlock() + + if err := c.syncContainer(); err != nil { + return err + } + + mountPoint := c.state.Mountpoint + if !c.state.Mounted { + mount, err := c.runtime.store.Mount(c.ID(), c.config.MountLabel) + if err != nil { + return errors.Wrapf(err, "error mounting container %q", c.ID()) + } + mountPoint = mount + defer func() { + if err := c.runtime.store.Unmount(c.ID()); err != nil { + logrus.Errorf("error unmounting container %q: %v", c.ID(), err) + } + }() + } + + input, err := archive.Tar(mountPoint, archive.Uncompressed) + if err != nil { + return errors.Wrapf(err, "error reading container directory %q", c.ID()) + } + + outFile, err := os.Create(path) + if err != nil { + return errors.Wrapf(err, "error creating file %q", path) + } + defer outFile.Close() + + _, err = io.Copy(outFile, input) + return err } // Commit commits the changes between a container and its image, creating a new -- cgit v1.2.3-54-g00ecf