|
@@ -1,6 +1,7 @@
|
|
package daemon // import "github.com/docker/docker/daemon"
|
|
package daemon // import "github.com/docker/docker/daemon"
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "context"
|
|
"fmt"
|
|
"fmt"
|
|
"io"
|
|
"io"
|
|
|
|
|
|
@@ -8,12 +9,11 @@ import (
|
|
"github.com/docker/docker/errdefs"
|
|
"github.com/docker/docker/errdefs"
|
|
"github.com/docker/docker/pkg/archive"
|
|
"github.com/docker/docker/pkg/archive"
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
|
- "github.com/docker/docker/pkg/ioutils"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
// ContainerExport writes the contents of the container to the given
|
|
// ContainerExport writes the contents of the container to the given
|
|
// writer. An error is returned if the container cannot be found.
|
|
// writer. An error is returned if the container cannot be found.
|
|
-func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
|
|
|
|
|
|
+func (daemon *Daemon) ContainerExport(ctx context.Context, name string, out io.Writer) error {
|
|
ctr, err := daemon.GetContainer(name)
|
|
ctr, err := daemon.GetContainer(name)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -33,49 +33,31 @@ func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
|
|
return errdefs.Conflict(err)
|
|
return errdefs.Conflict(err)
|
|
}
|
|
}
|
|
|
|
|
|
- data, err := daemon.containerExport(ctr)
|
|
|
|
|
|
+ err = daemon.containerExport(ctx, ctr, out)
|
|
if err != nil {
|
|
if err != nil {
|
|
return fmt.Errorf("Error exporting container %s: %v", name, err)
|
|
return fmt.Errorf("Error exporting container %s: %v", name, err)
|
|
}
|
|
}
|
|
- defer data.Close()
|
|
|
|
|
|
|
|
- // Stream the entire contents of the container (basically a volatile snapshot)
|
|
|
|
- if _, err := io.Copy(out, data); err != nil {
|
|
|
|
- return fmt.Errorf("Error exporting container %s: %v", name, err)
|
|
|
|
- }
|
|
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (daemon *Daemon) containerExport(container *container.Container) (arch io.ReadCloser, err error) {
|
|
|
|
- rwlayer, err := daemon.imageService.GetLayerByID(container.ID)
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- defer func() {
|
|
|
|
|
|
+func (daemon *Daemon) containerExport(ctx context.Context, container *container.Container, out io.Writer) error {
|
|
|
|
+ err := daemon.imageService.PerformWithBaseFS(ctx, container, func(basefs string) error {
|
|
|
|
+ archv, err := chrootarchive.Tar(basefs, &archive.TarOptions{
|
|
|
|
+ Compression: archive.Uncompressed,
|
|
|
|
+ IDMap: daemon.idMapping,
|
|
|
|
+ }, basefs)
|
|
if err != nil {
|
|
if err != nil {
|
|
- daemon.imageService.ReleaseLayer(rwlayer)
|
|
|
|
|
|
+ return err
|
|
}
|
|
}
|
|
- }()
|
|
|
|
-
|
|
|
|
- basefs, err := rwlayer.Mount(container.GetMountLabel())
|
|
|
|
- if err != nil {
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
|
|
|
|
- archv, err := chrootarchive.Tar(basefs, &archive.TarOptions{
|
|
|
|
- Compression: archive.Uncompressed,
|
|
|
|
- IDMap: daemon.idMapping,
|
|
|
|
- }, basefs)
|
|
|
|
- if err != nil {
|
|
|
|
- rwlayer.Unmount()
|
|
|
|
- return nil, err
|
|
|
|
- }
|
|
|
|
- arch = ioutils.NewReadCloserWrapper(archv, func() error {
|
|
|
|
- err := archv.Close()
|
|
|
|
- rwlayer.Unmount()
|
|
|
|
- daemon.imageService.ReleaseLayer(rwlayer)
|
|
|
|
|
|
+ // Stream the entire contents of the container (basically a volatile snapshot)
|
|
|
|
+ _, err = io.Copy(out, archv)
|
|
return err
|
|
return err
|
|
})
|
|
})
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
daemon.LogContainerEvent(container, "export")
|
|
daemon.LogContainerEvent(container, "export")
|
|
- return arch, err
|
|
|
|
|
|
+ return nil
|
|
}
|
|
}
|