daemon/export.go
8031c23b
 package daemon
 
 import (
a793564b
 	"fmt"
8031c23b
 	"io"
0a734182
 
6bb0d181
 	"github.com/docker/docker/container"
1c94f5f5
 	"github.com/docker/docker/pkg/archive"
 	"github.com/docker/docker/pkg/ioutils"
8031c23b
 )
 
abd72d40
 // ContainerExport writes the contents of the container to the given
 // writer. An error is returned if the container cannot be found.
b08f071e
 func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
d7d512bb
 	container, err := daemon.GetContainer(name)
d25a6537
 	if err != nil {
c79b9bab
 		return err
8031c23b
 	}
d25a6537
 
1c94f5f5
 	data, err := daemon.containerExport(container)
d25a6537
 	if err != nil {
a793564b
 		return fmt.Errorf("Error exporting container %s: %v", name, err)
d25a6537
 	}
 	defer data.Close()
 
 	// Stream the entire contents of the container (basically a volatile snapshot)
6b737752
 	if _, err := io.Copy(out, data); err != nil {
a793564b
 		return fmt.Errorf("Error exporting container %s: %v", name, err)
d25a6537
 	}
c79b9bab
 	return nil
8031c23b
 }
1c94f5f5
 
6bb0d181
 func (daemon *Daemon) containerExport(container *container.Container) (archive.Archive, error) {
1c94f5f5
 	if err := daemon.Mount(container); err != nil {
 		return nil, err
 	}
 
 	uidMaps, gidMaps := daemon.GetUIDGIDMaps()
6bb0d181
 	archive, err := archive.TarWithOptions(container.BaseFS, &archive.TarOptions{
1c94f5f5
 		Compression: archive.Uncompressed,
 		UIDMaps:     uidMaps,
 		GIDMaps:     gidMaps,
 	})
 	if err != nil {
3a497650
 		daemon.Unmount(container)
1c94f5f5
 		return nil, err
 	}
 	arch := ioutils.NewReadCloserWrapper(archive, func() error {
 		err := archive.Close()
3a497650
 		daemon.Unmount(container)
1c94f5f5
 		return err
 	})
ca5ede2d
 	daemon.LogContainerEvent(container, "export")
1c94f5f5
 	return arch, err
 }