Skip to content

Commit

Permalink
feat: replace github.com/pkg/errors to errors
Browse files Browse the repository at this point in the history
Signed-off-by: haoyun <yun.hao@daocloud.io>
Co-authored-by: zounengren <zouyee1989@gmail.com>
  • Loading branch information
jonyhy96 and zouyee committed Jan 7, 2022
1 parent 3ccd43c commit bbe46b8
Show file tree
Hide file tree
Showing 299 changed files with 1,896 additions and 1,874 deletions.
37 changes: 19 additions & 18 deletions archive/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package archive
import (
"archive/tar"
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -30,7 +32,6 @@ import (

"github.com/containerd/containerd/log"
"github.com/containerd/continuity/fs"
"github.com/pkg/errors"
)

var bufPool = &sync.Pool{
Expand Down Expand Up @@ -76,7 +77,7 @@ func WriteDiff(ctx context.Context, w io.Writer, a, b string, opts ...WriteDiffO
var options WriteDiffOptions
for _, opt := range opts {
if err := opt(&options); err != nil {
return errors.Wrap(err, "failed to apply option")
return fmt.Errorf("failed to apply option: %w", err)
}
}
if options.writeDiffFunc == nil {
Expand All @@ -97,7 +98,7 @@ func writeDiffNaive(ctx context.Context, w io.Writer, a, b string, _ WriteDiffOp
cw := NewChangeWriter(w, b)
err := fs.Changes(ctx, a, b, cw.HandleChange)
if err != nil {
return errors.Wrap(err, "failed to create diff tar stream")
return fmt.Errorf("failed to create diff tar stream: %w", err)
}
return cw.Close()
}
Expand Down Expand Up @@ -128,7 +129,7 @@ func Apply(ctx context.Context, root string, r io.Reader, opts ...ApplyOpt) (int
var options ApplyOptions
for _, opt := range opts {
if err := opt(&options); err != nil {
return 0, errors.Wrap(err, "failed to apply option")
return 0, fmt.Errorf("failed to apply option: %w", err)
}
}
if options.Filter == nil {
Expand Down Expand Up @@ -236,7 +237,7 @@ func applyNaive(ctx context.Context, root string, r io.Reader, options ApplyOpti
ppath, base := filepath.Split(hdr.Name)
ppath, err = fs.RootPath(root, ppath)
if err != nil {
return 0, errors.Wrap(err, "failed to get root path")
return 0, fmt.Errorf("failed to get root path: %w", err)
}

// Join to root before joining to parent path to ensure relative links are
Expand Down Expand Up @@ -266,7 +267,7 @@ func applyNaive(ctx context.Context, root string, r io.Reader, options ApplyOpti
}
writeFile, err := convertWhiteout(hdr, path)
if err != nil {
return 0, errors.Wrapf(err, "failed to convert whiteout file %q", hdr.Name)
return 0, fmt.Errorf("failed to convert whiteout file %q: %w", hdr.Name, err)
}
if !writeFile {
continue
Expand Down Expand Up @@ -373,7 +374,7 @@ func createTarFile(ctx context.Context, path, extractDir string, hdr *tar.Header
return nil

default:
return errors.Errorf("unhandled tar header type %d\n", hdr.Typeflag)
return fmt.Errorf("unhandled tar header type %d", hdr.Typeflag)
}

// Lchown is not supported on Windows.
Expand Down Expand Up @@ -520,7 +521,7 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
return err
}
if err := cw.tw.WriteHeader(hdr); err != nil {
return errors.Wrap(err, "failed to write whiteout header")
return fmt.Errorf("failed to write whiteout header: %w", err)
}
} else {
var (
Expand Down Expand Up @@ -555,12 +556,12 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
if strings.HasPrefix(name, string(filepath.Separator)) {
name, err = filepath.Rel(string(filepath.Separator), name)
if err != nil {
return errors.Wrap(err, "failed to make path relative")
return fmt.Errorf("failed to make path relative: %w", err)
}
}
name, err = tarName(name)
if err != nil {
return errors.Wrap(err, "cannot canonicalize path")
return fmt.Errorf("cannot canonicalize path: %w", err)
}
// suffix with '/' for directories
if f.IsDir() && !strings.HasSuffix(name, "/") {
Expand All @@ -569,7 +570,7 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
hdr.Name = name

if err := setHeaderForSpecialDevice(hdr, name, f); err != nil {
return errors.Wrap(err, "failed to set device headers")
return fmt.Errorf("failed to set device headers: %w", err)
}

// additionalLinks stores file names which must be linked to
Expand Down Expand Up @@ -597,7 +598,7 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
}

if capability, err := getxattr(source, "security.capability"); err != nil {
return errors.Wrap(err, "failed to get capabilities xattr")
return fmt.Errorf("failed to get capabilities xattr: %w", err)
} else if len(capability) > 0 {
if hdr.PAXRecords == nil {
hdr.PAXRecords = map[string]string{}
Expand All @@ -609,19 +610,19 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
return err
}
if err := cw.tw.WriteHeader(hdr); err != nil {
return errors.Wrap(err, "failed to write file header")
return fmt.Errorf("failed to write file header: %w", err)
}

if hdr.Typeflag == tar.TypeReg && hdr.Size > 0 {
file, err := open(source)
if err != nil {
return errors.Wrapf(err, "failed to open path: %v", source)
return fmt.Errorf("failed to open path: %v: %w", source, err)
}
defer file.Close()

n, err := copyBuffered(context.TODO(), cw.tw, file)
if err != nil {
return errors.Wrap(err, "failed to copy")
return fmt.Errorf("failed to copy: %w", err)
}
if n != hdr.Size {
return errors.New("short write copying file")
Expand All @@ -640,7 +641,7 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
return err
}
if err := cw.tw.WriteHeader(hdr); err != nil {
return errors.Wrap(err, "failed to write file header")
return fmt.Errorf("failed to write file header: %w", err)
}
}
}
Expand All @@ -651,7 +652,7 @@ func (cw *ChangeWriter) HandleChange(k fs.ChangeKind, p string, f os.FileInfo, e
// Close closes this writer.
func (cw *ChangeWriter) Close() error {
if err := cw.tw.Close(); err != nil {
return errors.Wrap(err, "failed to close tar writer")
return fmt.Errorf("failed to close tar writer: %w", err)
}
return nil
}
Expand Down Expand Up @@ -764,7 +765,7 @@ func validateWhiteout(path string) error {
dir += string(filepath.Separator)
}
if !strings.HasPrefix(originalPath, dir) {
return errors.Wrapf(errInvalidArchive, "invalid whiteout name: %v", base)
return fmt.Errorf("invalid whiteout name: %v: %w", base, errInvalidArchive)
}
}
return nil
Expand Down
21 changes: 10 additions & 11 deletions archive/tar_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/containerd/containerd/snapshots/overlay/overlayutils"
"github.com/containerd/continuity/fs"
"github.com/containerd/continuity/fs/fstest"
"github.com/pkg/errors"
)

func TestOverlayApply(t *testing.T) {
Expand Down Expand Up @@ -72,7 +71,7 @@ func TestOverlayApplyNoParents(t *testing.T) {
cw.addedDirs = nil
err := fs.Changes(ctx, a, b, cw.HandleChange)
if err != nil {
return errors.Wrap(err, "failed to create diff tar stream")
return fmt.Errorf("failed to create diff tar stream: %w", err)
}
return cw.Close()
},
Expand All @@ -97,7 +96,7 @@ type contextKey struct{}
func (d overlayDiffApplier) TestContext(ctx context.Context) (context.Context, func(), error) {
merged, err := os.MkdirTemp(d.tmp, "merged")
if err != nil {
return ctx, nil, errors.Wrap(err, "failed to make merged dir")
return ctx, nil, fmt.Errorf("failed to make merged dir: %w", err)
}

oc := &overlayContext{
Expand All @@ -118,7 +117,7 @@ func (d overlayDiffApplier) Apply(ctx context.Context, a fstest.Applier) (string

applyCopy, err := os.MkdirTemp(d.tmp, "apply-copy-")
if err != nil {
return "", nil, errors.Wrap(err, "failed to create temp dir")
return "", nil, fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(applyCopy)

Expand All @@ -128,33 +127,33 @@ func (d overlayDiffApplier) Apply(ctx context.Context, a fstest.Applier) (string
}

if err = fs.CopyDir(applyCopy, base); err != nil {
return "", nil, errors.Wrap(err, "failed to copy base")
return "", nil, fmt.Errorf("failed to copy base: %w", err)
}

if err := a.Apply(applyCopy); err != nil {
return "", nil, errors.Wrap(err, "failed to apply changes to copy of base")
return "", nil, fmt.Errorf("failed to apply changes to copy of base: %w", err)
}

buf := bytes.NewBuffer(nil)

if err := d.diff(ctx, buf, base, applyCopy); err != nil {
return "", nil, errors.Wrap(err, "failed to create diff")
return "", nil, fmt.Errorf("failed to create diff: %w", err)
}

if oc.mounted {
if err := mount.Unmount(oc.merged, 0); err != nil {
return "", nil, errors.Wrap(err, "failed to unmount")
return "", nil, fmt.Errorf("failed to unmount: %w", err)
}
oc.mounted = false
}

next, err := os.MkdirTemp(d.tmp, "lower-")
if err != nil {
return "", nil, errors.Wrap(err, "failed to create temp dir")
return "", nil, fmt.Errorf("failed to create temp dir: %w", err)
}

if _, err = Apply(ctx, next, buf, WithConvertWhiteout(OverlayConvertWhiteout), WithParents(oc.lowers)); err != nil {
return "", nil, errors.Wrap(err, "failed to apply tar stream")
return "", nil, fmt.Errorf("failed to apply tar stream: %w", err)
}

oc.lowers = append([]string{next}, oc.lowers...)
Expand All @@ -172,7 +171,7 @@ func (d overlayDiffApplier) Apply(ctx context.Context, a fstest.Applier) (string
}

if err := m.Mount(oc.merged); err != nil {
return "", nil, errors.Wrapf(err, "failed to mount: %v", m)
return "", nil, fmt.Errorf("failed to mount: %v: %w", m, err)
}
oc.mounted = true

Expand Down
Loading

0 comments on commit bbe46b8

Please sign in to comment.