1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/daemon/export.go
Cory Snider 098a44c07f Finish refactor of UID/GID usage to a new struct
Finish the refactor which was partially completed with commit
34536c498d, passing around IdentityMapping structs instead of pairs of
[]IDMap slices.

Existing code which uses []IDMap relies on zero-valued fields to be
valid, empty mappings. So in order to successfully finish the
refactoring without introducing bugs, their replacement therefore also
needs to have a useful zero value which represents an empty mapping.
Change IdentityMapping to be a pass-by-value type so that there are no
nil pointers to worry about.

The functionality provided by the deprecated NewIDMappingsFromMaps
function is required by unit tests to to construct arbitrary
IdentityMapping values. And the daemon will always need to access the
mappings to pass them to the Linux kernel. Accommodate these use cases
by exporting the struct fields instead. BuildKit currently depends on
the UIDs and GIDs methods so we cannot get rid of them yet.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-03-14 16:28:57 -04:00

80 lines
2.1 KiB
Go

package daemon // import "github.com/docker/docker/daemon"
import (
"fmt"
"io"
"github.com/docker/docker/container"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/ioutils"
)
// ContainerExport writes the contents of the container to the given
// writer. An error is returned if the container cannot be found.
func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
ctr, err := daemon.GetContainer(name)
if err != nil {
return err
}
if isWindows && ctr.OS == "windows" {
return fmt.Errorf("the daemon on this operating system does not support exporting Windows containers")
}
if ctr.IsDead() {
err := fmt.Errorf("You cannot export container %s which is Dead", ctr.ID)
return errdefs.Conflict(err)
}
if ctr.IsRemovalInProgress() {
err := fmt.Errorf("You cannot export container %s which is being removed", ctr.ID)
return errdefs.Conflict(err)
}
data, err := daemon.containerExport(ctr)
if err != nil {
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
}
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() {
if err != nil {
daemon.imageService.ReleaseLayer(rwlayer)
}
}()
basefs, err := rwlayer.Mount(container.GetMountLabel())
if err != nil {
return nil, err
}
archv, err := archivePath(basefs, basefs.Path(), &archive.TarOptions{
Compression: archive.Uncompressed,
IDMap: daemon.idMapping,
}, basefs.Path())
if err != nil {
rwlayer.Unmount()
return nil, err
}
arch = ioutils.NewReadCloserWrapper(archv, func() error {
err := archv.Close()
rwlayer.Unmount()
daemon.imageService.ReleaseLayer(rwlayer)
return err
})
daemon.LogContainerEvent(container, "export")
return arch, err
}