From e37a2d1879271a3529f1cfbb49dc180bab47fb08 Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Thu, 22 Sep 2022 20:32:37 -0400 Subject: [PATCH] pkg/containerfs: delete Archiver, Driver They were needed for Linux Containers on Windows, which is no longer supported. Signed-off-by: Cory Snider --- builder/dockerfile/copy.go | 6 +- builder/dockerfile/internals.go | 51 +------- pkg/containerfs/archiver.go | 204 -------------------------------- pkg/containerfs/containerfs.go | 6 - 4 files changed, 6 insertions(+), 261 deletions(-) delete mode 100644 pkg/containerfs/archiver.go diff --git a/builder/dockerfile/copy.go b/builder/dockerfile/copy.go index cc424f3555..72c291a7a3 100644 --- a/builder/dockerfile/copy.go +++ b/builder/dockerfile/copy.go @@ -449,7 +449,7 @@ func downloadSource(output io.Writer, stdout io.Writer, srcURL string) (remote b type copyFileOptions struct { decompress bool identity *idtools.Identity - archiver Archiver + archiver *archive.Archiver } func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions) error { @@ -490,7 +490,7 @@ func performCopyForInfo(dest copyInfo, source copyInfo, options copyFileOptions) return copyFile(archiver, srcPath, destPath, options.identity) } -func copyDirectory(archiver Archiver, source, dest string, identity *idtools.Identity) error { +func copyDirectory(archiver *archive.Archiver, source, dest string, identity *idtools.Identity) error { destExists, err := isExistingDirectory(dest) if err != nil { return errors.Wrapf(err, "failed to query destination path") @@ -505,7 +505,7 @@ func copyDirectory(archiver Archiver, source, dest string, identity *idtools.Ide return nil } -func copyFile(archiver Archiver, source, dest string, identity *idtools.Identity) error { +func copyFile(archiver *archive.Archiver, source, dest string, identity *idtools.Identity) error { if identity == nil { // Use system.MkdirAll here, which is a custom version of os.MkdirAll // modified for use on Windows to handle volume GUID paths. These paths diff --git a/builder/dockerfile/internals.go b/builder/dockerfile/internals.go index 1bc445d383..183da86ff0 100644 --- a/builder/dockerfile/internals.go +++ b/builder/dockerfile/internals.go @@ -7,7 +7,6 @@ import ( "crypto/sha256" "encoding/hex" "fmt" - "io" "strings" "github.com/docker/docker/api/types" @@ -17,8 +16,6 @@ import ( "github.com/docker/docker/image" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/chrootarchive" - "github.com/docker/docker/pkg/containerfs" - "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/stringid" "github.com/docker/go-connections/nat" specs "github.com/opencontainers/image-spec/specs-go/v1" @@ -26,50 +23,8 @@ import ( "github.com/sirupsen/logrus" ) -// Archiver defines an interface for copying files from one destination to -// another using Tar/Untar. -type Archiver interface { - TarUntar(src, dst string) error - UntarPath(src, dst string) error - CopyWithTar(src, dst string) error - CopyFileWithTar(src, dst string) error - IdentityMapping() idtools.IdentityMapping -} - -// The builder will use the following interfaces if the container fs implements -// these for optimized copies to and from the container. -type extractor interface { - ExtractArchive(src io.Reader, dst string, opts *archive.TarOptions) error -} - -type archiver interface { - ArchivePath(src string, opts *archive.TarOptions) (io.ReadCloser, error) -} - -// helper functions to get tar/untar func -func untarFunc(i interface{}) containerfs.UntarFunc { - if ea, ok := i.(extractor); ok { - return ea.ExtractArchive - } - return chrootarchive.Untar -} - -func tarFunc(i interface{}) containerfs.TarFunc { - if ap, ok := i.(archiver); ok { - return ap.ArchivePath - } - return archive.TarWithOptions -} - -func (b *Builder) getArchiver(src, dst containerfs.Driver) Archiver { - t, u := tarFunc(src), untarFunc(dst) - return &containerfs.Archiver{ - SrcDriver: src, - DstDriver: dst, - Tar: t, - Untar: u, - IDMapping: b.idMapping, - } +func (b *Builder) getArchiver() *archive.Archiver { + return chrootarchive.NewArchiver(b.idMapping) } func (b *Builder) commit(dispatchState *dispatchState, comment string) error { @@ -205,7 +160,7 @@ func (b *Builder) performCopy(req dispatchRequest, inst copyInstruction) error { for _, info := range inst.infos { opts := copyFileOptions{ decompress: inst.allowLocalDecompression, - archiver: b.getArchiver(info.root, destInfo.root), + archiver: b.getArchiver(), } if !inst.preserveOwnership { opts.identity = &identity diff --git a/pkg/containerfs/archiver.go b/pkg/containerfs/archiver.go deleted file mode 100644 index 23e076514e..0000000000 --- a/pkg/containerfs/archiver.go +++ /dev/null @@ -1,204 +0,0 @@ -package containerfs // import "github.com/docker/docker/pkg/containerfs" - -import ( - "archive/tar" - "errors" - "io" - "os" - "path/filepath" - "runtime" - "time" - - "github.com/docker/docker/pkg/archive" - "github.com/docker/docker/pkg/idtools" - "github.com/docker/docker/pkg/system" - "github.com/sirupsen/logrus" -) - -// TarFunc provides a function definition for a custom Tar function -type TarFunc func(string, *archive.TarOptions) (io.ReadCloser, error) - -// UntarFunc provides a function definition for a custom Untar function -type UntarFunc func(io.Reader, string, *archive.TarOptions) error - -// Archiver provides a similar implementation of the archive.Archiver package with the rootfs abstraction -type Archiver struct { - SrcDriver Driver - DstDriver Driver - Tar TarFunc - Untar UntarFunc - IDMapping idtools.IdentityMapping -} - -// TarUntar is a convenience function which calls Tar and Untar, with the output of one piped into the other. -// If either Tar or Untar fails, TarUntar aborts and returns the error. -func (archiver *Archiver) TarUntar(src, dst string) error { - logrus.Debugf("TarUntar(%s %s)", src, dst) - tarArchive, err := archiver.Tar(src, &archive.TarOptions{Compression: archive.Uncompressed}) - if err != nil { - return err - } - defer tarArchive.Close() - options := &archive.TarOptions{ - IDMap: archiver.IDMapping, - } - return archiver.Untar(tarArchive, dst, options) -} - -// UntarPath untar a file from path to a destination, src is the source tar file path. -func (archiver *Archiver) UntarPath(src, dst string) error { - tarArchive, err := archiver.SrcDriver.Open(src) - if err != nil { - return err - } - defer tarArchive.Close() - options := &archive.TarOptions{ - IDMap: archiver.IDMapping, - } - return archiver.Untar(tarArchive, dst, options) -} - -// CopyWithTar creates a tar archive of filesystem path `src`, and -// unpacks it at filesystem path `dst`. -// The archive is streamed directly with fixed buffering and no -// intermediary disk IO. -func (archiver *Archiver) CopyWithTar(src, dst string) error { - srcSt, err := archiver.SrcDriver.Stat(src) - if err != nil { - return err - } - if !srcSt.IsDir() { - return archiver.CopyFileWithTar(src, dst) - } - - // if this archiver is set up with ID mapping we need to create - // the new destination directory with the remapped root UID/GID pair - // as owner - - identity := idtools.Identity{UID: archiver.IDMapping.RootPair().UID, GID: archiver.IDMapping.RootPair().GID} - - // Create dst, copy src's content into it - if err := idtools.MkdirAllAndChownNew(dst, 0755, identity); err != nil { - return err - } - logrus.Debugf("Calling TarUntar(%s, %s)", src, dst) - return archiver.TarUntar(src, dst) -} - -// CopyFileWithTar emulates the behavior of the 'cp' command-line -// for a single file. It copies a regular file from path `src` to -// path `dst`, and preserves all its metadata. -func (archiver *Archiver) CopyFileWithTar(src, dst string) (retErr error) { - logrus.Debugf("CopyFileWithTar(%s, %s)", src, dst) - srcDriver := archiver.SrcDriver - dstDriver := archiver.DstDriver - - srcSt, retErr := srcDriver.Stat(src) - if retErr != nil { - return retErr - } - - if srcSt.IsDir() { - return errors.New("cannot copy a directory") - } - - // Clean up the trailing slash. This must be done in an operating - // system specific manner. - if dst[len(dst)-1] == dstDriver.Separator() { - dst = dstDriver.Join(dst, srcDriver.Base(src)) - } - - // The original call was system.MkdirAll, which is just - // os.MkdirAll on not-Windows and changed for Windows. - if runtime.GOOS == "windows" { - // Now we are WCOW - if err := system.MkdirAll(filepath.Dir(dst), 0700); err != nil { - return err - } - } else { - // We can just use the driver.MkdirAll function - if err := dstDriver.MkdirAll(dstDriver.Dir(dst), 0700); err != nil { - return err - } - } - - r, w := io.Pipe() - errC := make(chan error, 1) - - go func() { - defer close(errC) - errC <- func() error { - defer w.Close() - - srcF, err := srcDriver.Open(src) - if err != nil { - return err - } - defer srcF.Close() - - hdr, err := archive.FileInfoHeaderNoLookups(srcSt, "") - if err != nil { - return err - } - hdr.Format = tar.FormatPAX - hdr.ModTime = hdr.ModTime.Truncate(time.Second) - hdr.AccessTime = time.Time{} - hdr.ChangeTime = time.Time{} - hdr.Name = dstDriver.Base(dst) - if runtime.GOOS == "windows" { - hdr.Mode = int64(chmodTarEntry(os.FileMode(hdr.Mode))) - } else { - hdr.Mode = int64(os.FileMode(hdr.Mode)) - } - - if err := remapIDs(archiver.IDMapping, hdr); err != nil { - return err - } - - tw := tar.NewWriter(w) - defer tw.Close() - if err := tw.WriteHeader(hdr); err != nil { - return err - } - if _, err := io.Copy(tw, srcF); err != nil { - return err - } - return nil - }() - }() - defer func() { - if err := <-errC; retErr == nil && err != nil { - retErr = err - } - }() - - retErr = archiver.Untar(r, dstDriver.Dir(dst), nil) - if retErr != nil { - r.CloseWithError(retErr) - } - return retErr -} - -// IdentityMapping returns the IdentityMapping of the archiver. -func (archiver *Archiver) IdentityMapping() idtools.IdentityMapping { - return archiver.IDMapping -} - -func remapIDs(idMapping idtools.IdentityMapping, hdr *tar.Header) error { - ids, err := idMapping.ToHost(idtools.Identity{UID: hdr.Uid, GID: hdr.Gid}) - hdr.Uid, hdr.Gid = ids.UID, ids.GID - return err -} - -// chmodTarEntry is used to adjust the file permissions used in tar header based -// on the platform the archival is done. -func chmodTarEntry(perm os.FileMode) os.FileMode { - // perm &= 0755 // this 0-ed out tar flags (like link, regular file, directory marker etc.) - permPart := perm & os.ModePerm - noPermPart := perm &^ os.ModePerm - // Add the x bit: make everything +x from windows - permPart |= 0111 - permPart &= 0755 - - return noPermPart | permPart -} diff --git a/pkg/containerfs/containerfs.go b/pkg/containerfs/containerfs.go index 60d5fa417a..a5124d1791 100644 --- a/pkg/containerfs/containerfs.go +++ b/pkg/containerfs/containerfs.go @@ -14,12 +14,6 @@ type ContainerFS interface { // on the local system, so the continuity operations must be used Path() string - Driver -} - -// Driver combines both continuity's Driver and PathDriver interfaces with a Platform -// field to determine the OS. -type Driver interface { // Driver & PathDriver provide methods to manipulate files & paths driver.Driver pathdriver.PathDriver