diff --git a/archive/tar.go b/archive/tar.go index c9a4830..75b74e0 100644 --- a/archive/tar.go +++ b/archive/tar.go @@ -14,8 +14,8 @@ import ( "syscall" "time" - "github.com/Sirupsen/logrus" "github.com/docker/containerd/fs" + "github.com/docker/containerd/log" "github.com/pkg/errors" ) @@ -29,14 +29,14 @@ var ( breakoutError = errors.New("file name outside of root") ) -// TarFromChanges returns a tar stream of the computed filesystem +// Diff returns a tar stream of the computed filesystem // difference between the provided directories. // // Produces a tar using OCI style file markers for deletions. Deleted // files will be prepended with the prefix ".wh.". This style is // based off AUFS whiteouts. // See https://github.com/opencontainers/image-spec/blob/master/layer.md -func DiffTarStream(ctx context.Context, a, b string) io.ReadCloser { +func Diff(ctx context.Context, a, b string) io.ReadCloser { r, w := io.Pipe() go func() { @@ -48,7 +48,7 @@ func DiffTarStream(ctx context.Context, a, b string) io.ReadCloser { err = cw.Close() } if err = w.CloseWithError(err); err != nil { - logrus.Errorf("Error closing tar pipe: %v", err) + log.G(ctx).WithError(err).Debugf("closing tar pipe failed") } }() @@ -76,9 +76,9 @@ const ( whiteoutOpaqueDir = whiteoutMetaPrefix + ".opq" ) -// ApplyDiffTar applies a tar stream of an OCI style diff tar. +// Apply applies a tar stream of an OCI style diff tar. // See https://github.com/opencontainers/image-spec/blob/master/layer.md#applying-changesets -func ApplyDiffTar(ctx context.Context, root string, r io.Reader) (int64, error) { +func Apply(ctx context.Context, root string, r io.Reader) (int64, error) { root = filepath.Clean(root) fn := prepareApply() defer fn() @@ -114,6 +114,7 @@ func ApplyDiffTar(ctx context.Context, root string, r io.Reader) (int64, error) hdr.Name = filepath.Clean(hdr.Name) if skipFile(hdr) { + log.G(ctx).Warnf("file %q ignored: archive may not be supported on system", hdr.Name) continue } @@ -147,7 +148,7 @@ func ApplyDiffTar(ctx context.Context, root string, r io.Reader) (int64, error) } defer os.RemoveAll(aufsTempdir) } - if err := createTarFile(filepath.Join(aufsTempdir, basename), root, hdr, tr); err != nil { + if err := createTarFile(ctx, filepath.Join(aufsTempdir, basename), root, hdr, tr); err != nil { return 0, err } } @@ -236,7 +237,7 @@ func ApplyDiffTar(ctx context.Context, root string, r io.Reader) (int64, error) srcData = tmpFile } - if err := createTarFile(path, root, srcHdr, srcData); err != nil { + if err := createTarFile(ctx, path, root, srcHdr, srcData); err != nil { return 0, err } @@ -384,7 +385,7 @@ func (cw *changeWriter) Close() error { return nil } -func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader) error { +func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header, reader io.Reader) error { // hdr.Mode is in linux format, which we can use for sycalls, // but for os.Foo() calls we need the mode converted to os.FileMode, // so use hdrInfo.Mode() (they differ for e.g. setuid bits) @@ -451,7 +452,7 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader) e } case tar.TypeXGlobalHeader: - logrus.Debug("PAX Global Extended Headers found and ignored") + log.G(ctx).Debug("PAX Global Extended Headers found and ignored") return nil default: @@ -465,24 +466,16 @@ func createTarFile(path, extractDir string, hdr *tar.Header, reader io.Reader) e } } - var warnErrors []string for key, value := range hdr.Xattrs { if err := setxattr(path, key, value); err != nil { if errors.Cause(err) == syscall.ENOTSUP { - // Collect errors only for filesystem warning - warnErrors = append(warnErrors, err.Error()) + log.G(ctx).WithError(err).Warnf("ignored xattr %s in archive", key) continue } return err } } - if len(warnErrors) > 0 { - logrus.WithFields(logrus.Fields{ - "errors": warnErrors, - }).Warn("ignored xattrs in archive: underlying filesystem doesn't support them") - } - // There is no LChmod, so ignore mode for symlink. Also, this // must happen after chown, as that can modify the file mode if err := handleLChmod(hdr, path, hdrInfo); err != nil { diff --git a/archive/tar_test.go b/archive/tar_test.go index 416fa28..9ec960e 100644 --- a/archive/tar_test.go +++ b/archive/tar_test.go @@ -5,7 +5,6 @@ import ( "io/ioutil" "os" "os/exec" - "os/user" "testing" "time" @@ -16,7 +15,7 @@ import ( "github.com/pkg/errors" ) -const tarCmd = "/usr/bin/tar" +const tarCmd = "tar" // baseApplier creates a basic filesystem layout // with multiple types of files for basic tests. @@ -113,7 +112,7 @@ func testApply(a fstest.Applier) error { return errors.Wrap(err, "failed to start command") } - if _, err := ApplyDiffTar(context.Background(), dest, arch); err != nil { + if _, err := Apply(context.Background(), dest, arch); err != nil { return errors.Wrap(err, "failed to apply tar stream") } @@ -136,7 +135,7 @@ func testBaseDiff(a fstest.Applier) error { return errors.Wrap(err, "failed to apply filesystem changes") } - arch := DiffTarStream(context.Background(), "", td) + arch := Diff(context.Background(), "", td) cmd := exec.Command(tarCmd, "x", "-C", dest) cmd.Stdin = arch @@ -184,7 +183,7 @@ func diffApply(ctx context.Context, a fstest.Applier, base, dest string) error { return errors.Wrap(err, "failed to apply changes to base") } - if _, err := ApplyDiffTar(ctx, dest, DiffTarStream(ctx, baseCopy, base)); err != nil { + if _, err := Apply(ctx, dest, Diff(ctx, baseCopy, base)); err != nil { return errors.Wrap(err, "failed to apply tar stream") } @@ -204,21 +203,7 @@ func readDirNames(p string) ([]string, error) { } func requireTar(t *testing.T) { - if _, err := os.Stat(tarCmd); err != nil { - if os.IsNotExist(err) { - t.Skipf("%s not found, skipping", tarCmd) - } else { - t.Fatalf("Unable to stat %s: %v", tarCmd, err) - } - } -} - -func requireRoot(t *testing.T) { - u, err := user.Current() - if err != nil { - t.Fatalf("Unable to get current user: %v", err) - } - if u.Uid != "0" { - t.Skipf("test requires root, skipping") + if _, err := exec.LookPath(tarCmd); err != nil { + t.Skipf("%s not found, skipping", tarCmd) } } diff --git a/archive/tar_windows.go b/archive/tar_windows.go index 25345a1..1c52f3a 100644 --- a/archive/tar_windows.go +++ b/archive/tar_windows.go @@ -7,7 +7,6 @@ import ( "os" "strings" - "github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/system" ) @@ -78,7 +77,6 @@ func skipFile(hdr *tar.Header) bool { // to cater for the situation where someone does manage to upload a Linux // image but have it tagged as Windows inadvertently. if strings.Contains(hdr.Name, ":") { - logrus.Warnf("Windows: Ignoring %s (is this a Linux image?)", hdr.Name) return true } diff --git a/cmd/dist/apply.go b/cmd/dist/apply.go index 3ebf558..598c722 100644 --- a/cmd/dist/apply.go +++ b/cmd/dist/apply.go @@ -28,7 +28,7 @@ var applyCommand = cli.Command{ return err } - if _, err := archive.ApplyDiffTar(ctx, dir, rd); err != nil { + if _, err := archive.Apply(ctx, dir, rd); err != nil { return err } diff --git a/rootfs/apply.go b/rootfs/apply.go index a2a2600..74b7d6a 100644 --- a/rootfs/apply.go +++ b/rootfs/apply.go @@ -63,7 +63,7 @@ func ApplyLayer(snapshots snapshot.Snapshotter, mounter Mounter, rd io.Reader, p return "", err } - if _, err := archive.ApplyDiffTar(context.Background(), key, rd); err != nil { + if _, err := archive.Apply(context.Background(), key, rd); err != nil { return "", err }