11package daemon // import "github.com/docker/docker/daemon"
22
33import (
4+ "context"
45 "fmt"
56 "io"
67
78 "github.com/docker/docker/container"
89 "github.com/docker/docker/errdefs"
910 "github.com/docker/docker/pkg/archive"
1011 "github.com/docker/docker/pkg/chrootarchive"
11- "github.com/docker/docker/pkg/ioutils"
1212)
1313
1414// ContainerExport writes the contents of the container to the given
1515// writer. An error is returned if the container cannot be found.
16- func (daemon * Daemon ) ContainerExport (name string , out io.Writer ) error {
16+ func (daemon * Daemon ) ContainerExport (ctx context. Context , name string , out io.Writer ) error {
1717 ctr , err := daemon .GetContainer (name )
1818 if err != nil {
1919 return err
@@ -33,49 +33,31 @@ func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
3333 return errdefs .Conflict (err )
3434 }
3535
36- data , err : = daemon .containerExport (ctr )
36+ err = daemon .containerExport (ctx , ctr , out )
3737 if err != nil {
3838 return fmt .Errorf ("Error exporting container %s: %v" , name , err )
3939 }
40- defer data .Close ()
4140
42- // Stream the entire contents of the container (basically a volatile snapshot)
43- if _ , err := io .Copy (out , data ); err != nil {
44- return fmt .Errorf ("Error exporting container %s: %v" , name , err )
45- }
4641 return nil
4742}
4843
49- func (daemon * Daemon ) containerExport (container * container.Container ) ( arch io.ReadCloser , err error ) {
50- rwlayer , err := daemon .imageService .GetLayerByID ( container . ID )
51- if err != nil {
52- return nil , err
53- }
54- defer func () {
44+ func (daemon * Daemon ) containerExport (ctx context. Context , container * container.Container , out io.Writer ) error {
45+ err := daemon .imageService .PerformWithBaseFS ( ctx , container , func ( basefs string ) error {
46+ archv , err := chrootarchive . Tar ( basefs , & archive. TarOptions {
47+ Compression : archive . Uncompressed ,
48+ IDMap : daemon . idMapping ,
49+ }, basefs )
5550 if err != nil {
56- daemon . imageService . ReleaseLayer ( rwlayer )
51+ return err
5752 }
58- }()
59-
60- basefs , err := rwlayer .Mount (container .GetMountLabel ())
61- if err != nil {
62- return nil , err
63- }
6453
65- archv , err := chrootarchive .Tar (basefs , & archive.TarOptions {
66- Compression : archive .Uncompressed ,
67- IDMap : daemon .idMapping ,
68- }, basefs )
69- if err != nil {
70- rwlayer .Unmount ()
71- return nil , err
72- }
73- arch = ioutils .NewReadCloserWrapper (archv , func () error {
74- err := archv .Close ()
75- rwlayer .Unmount ()
76- daemon .imageService .ReleaseLayer (rwlayer )
54+ // Stream the entire contents of the container (basically a volatile snapshot)
55+ _ , err = io .Copy (out , archv )
7756 return err
7857 })
58+ if err != nil {
59+ return err
60+ }
7961 daemon .LogContainerEvent (container , "export" )
80- return arch , err
62+ return nil
8163}
0 commit comments