feat: replace github.com/pkg/errors to errors

Signed-off-by: haoyun <yun.hao@daocloud.io>
Co-authored-by: zounengren <zouyee1989@gmail.com>
This commit is contained in:
haoyun
2022-01-07 10:19:31 +08:00
parent 3ccd43c8f6
commit bbe46b8c43
299 changed files with 1896 additions and 1874 deletions

View File

@@ -17,12 +17,12 @@
package mount
import (
"fmt"
"runtime"
"syscall"
"unsafe"
"github.com/containerd/containerd/log"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
@@ -62,7 +62,7 @@ func fMountat(dirfd uintptr, source, target, fstype string, flags uintptr, data
var pipefds [2]int
if err := syscall.Pipe2(pipefds[:], syscall.O_CLOEXEC); err != nil {
return errors.Wrap(err, "failed to open pipe")
return fmt.Errorf("failed to open pipe: %w", err)
}
defer func() {
@@ -82,7 +82,7 @@ func fMountat(dirfd uintptr, source, target, fstype string, flags uintptr, data
)
if errno != 0 {
return errors.Wrap(errno, "failed to fork thread")
return fmt.Errorf("failed to fork thread: %w", errno)
}
defer func() {
@@ -101,11 +101,11 @@ func fMountat(dirfd uintptr, source, target, fstype string, flags uintptr, data
uintptr(unsafe.Pointer(&status)),
unsafe.Sizeof(status))
if errno != 0 {
return errors.Wrap(errno, "failed to read pipe")
return fmt.Errorf("failed to read pipe: %w", errno)
}
if status != 0 {
return errors.Wrap(status, "failed to mount")
return fmt.Errorf("failed to mount: %w", status)
}
return nil

View File

@@ -17,6 +17,7 @@
package mount
import (
"errors"
"os"
"path/filepath"
"syscall"
@@ -24,7 +25,6 @@ import (
"time"
"github.com/containerd/continuity/fs/fstest"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
@@ -125,7 +125,7 @@ func testFMountatWithFileFd(t *testing.T, root string) {
err = fMountat(f.Fd(), filepath.Join(root, "empty"), filepath.Join(root, "work"), "", 0, "")
if !errors.Is(err, expectedErr) {
t.Fatalf("expected error %v, but got %v", expectedErr, errors.Cause(err))
t.Fatalf("expected error %v, but got %v", expectedErr, errors.Unwrap(err))
}
}

View File

@@ -20,10 +20,10 @@
package mount
import (
"fmt"
"path/filepath"
"github.com/moby/sys/mountinfo"
"github.com/pkg/errors"
)
// Lookup returns the mount info corresponds to the path.
@@ -32,10 +32,10 @@ func Lookup(dir string) (Info, error) {
m, err := mountinfo.GetMounts(mountinfo.ParentsFilter(dir))
if err != nil {
return Info{}, errors.Wrapf(err, "failed to find the mount info for %q", dir)
return Info{}, fmt.Errorf("failed to find the mount info for %q: %w", dir, err)
}
if len(m) == 0 {
return Info{}, errors.Errorf("failed to find the mount info for %q", dir)
return Info{}, fmt.Errorf("failed to find the mount info for %q", dir)
}
// find the longest matching mount point

View File

@@ -17,6 +17,7 @@
package mount
import (
"errors"
"fmt"
"math/rand"
"os"
@@ -25,7 +26,6 @@ import (
"time"
"unsafe"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
@@ -59,12 +59,12 @@ func ioctl(fd, req, args uintptr) (uintptr, uintptr, error) {
func getFreeLoopDev() (uint32, error) {
ctrl, err := os.OpenFile(loopControlPath, os.O_RDWR, 0)
if err != nil {
return 0, errors.Errorf("could not open %v: %v", loopControlPath, err)
return 0, fmt.Errorf("could not open %v: %v", loopControlPath, err)
}
defer ctrl.Close()
num, _, err := ioctl(ctrl.Fd(), unix.LOOP_CTL_GET_FREE, 0)
if err != nil {
return 0, errors.Wrap(err, "could not get free loop device")
return 0, fmt.Errorf("could not get free loop device: %w", err)
}
return uint32(num), nil
}
@@ -81,13 +81,13 @@ func setupLoopDev(backingFile, loopDev string, param LoopParams) (_ *os.File, re
back, err := os.OpenFile(backingFile, flags, 0)
if err != nil {
return nil, errors.Wrapf(err, "could not open backing file: %s", backingFile)
return nil, fmt.Errorf("could not open backing file: %s: %w", backingFile, err)
}
defer back.Close()
loop, err := os.OpenFile(loopDev, flags, 0)
if err != nil {
return nil, errors.Wrapf(err, "could not open loop device: %s", loopDev)
return nil, fmt.Errorf("could not open loop device: %s: %w", loopDev, err)
}
defer func() {
if retErr != nil {
@@ -97,7 +97,7 @@ func setupLoopDev(backingFile, loopDev string, param LoopParams) (_ *os.File, re
// 2. Set FD
if _, _, err = ioctl(loop.Fd(), unix.LOOP_SET_FD, back.Fd()); err != nil {
return nil, errors.Wrapf(err, "could not set loop fd for device: %s", loopDev)
return nil, fmt.Errorf("could not set loop fd for device: %s: %w", loopDev, err)
}
// 3. Set Info
@@ -131,7 +131,7 @@ func setupLoopDev(backingFile, loopDev string, param LoopParams) (_ *os.File, re
}
_, _, _ = ioctl(loop.Fd(), unix.LOOP_CLR_FD, 0)
return nil, errors.Errorf("failed to set loop device info: %v", err)
return nil, fmt.Errorf("failed to set loop device info: %v", err)
}
// setupLoop looks for (and possibly creates) a free loop device, and
@@ -200,7 +200,7 @@ func AttachLoopDevice(backingFile string) (string, error) {
func DetachLoopDevice(devices ...string) error {
for _, dev := range devices {
if err := removeLoop(dev); err != nil {
return errors.Wrapf(err, "failed to remove loop device: %s", dev)
return fmt.Errorf("failed to remove loop device: %s: %w", dev, err)
}
}

View File

@@ -17,10 +17,11 @@
package mount
import (
"errors"
"fmt"
"os"
"time"
"github.com/pkg/errors"
exec "golang.org/x/sys/execabs"
"golang.org/x/sys/unix"
)
@@ -64,7 +65,7 @@ func (m *Mount) mountWithHelper(target string) error {
return nil
}
if !errors.Is(err, unix.ECHILD) {
return errors.Wrapf(err, "mount [%v] failed: %q", args, string(out))
return fmt.Errorf("mount [%v] failed: %q: %w", args, string(out), err)
}
// We got ECHILD, we are not sure whether the mount was successful.
// If the mount ID has changed, we are sure we got some new mount, but still not sure it is fully completed.
@@ -77,7 +78,7 @@ func (m *Mount) mountWithHelper(target string) error {
_ = unmount(target, 0)
}
}
return errors.Errorf("mount [%v] failed with ECHILD (retired %d times)", args, retriesOnECHILD)
return fmt.Errorf("mount [%v] failed with ECHILD (retired %d times)", args, retriesOnECHILD)
}
// Unmount the provided mount path with the flags
@@ -101,7 +102,7 @@ func unmount(target string, flags int) error {
}
return nil
}
return errors.Wrapf(unix.EBUSY, "failed to unmount target %s", target)
return fmt.Errorf("failed to unmount target %s: %w", target, unix.EBUSY)
}
// UnmountAll repeatedly unmounts the given mount point until there

View File

@@ -17,13 +17,13 @@
package mount
import (
"errors"
"fmt"
"os"
"path"
"strings"
"time"
"github.com/pkg/errors"
exec "golang.org/x/sys/execabs"
"golang.org/x/sys/unix"
)
@@ -163,7 +163,7 @@ func unmount(target string, flags int) error {
}
return nil
}
return errors.Wrapf(unix.EBUSY, "failed to unmount target %s", target)
return fmt.Errorf("failed to unmount target %s: %w", target, unix.EBUSY)
}
// UnmountAll repeatedly unmounts the given mount point until there
@@ -365,19 +365,22 @@ func mountAt(chdir string, source, target, fstype string, flags uintptr, data st
f, err := os.Open(chdir)
if err != nil {
return errors.Wrap(err, "failed to mountat")
return fmt.Errorf("failed to mountat: %w", err)
}
defer f.Close()
fs, err := f.Stat()
if err != nil {
return errors.Wrap(err, "failed to mountat")
return fmt.Errorf("failed to mountat: %w", err)
}
if !fs.IsDir() {
return errors.Wrap(errors.Errorf("%s is not dir", chdir), "failed to mountat")
return fmt.Errorf("failed to mountat: %s is not dir", chdir)
}
return errors.Wrap(fMountat(f.Fd(), source, target, fstype, flags, data), "failed to mountat")
if err := fMountat(f.Fd(), source, target, fstype, flags, data); err != nil {
return fmt.Errorf("failed to mountat: %w", err)
}
return nil
}
func (m *Mount) mountWithHelper(helperBinary, typePrefix, target string) error {
@@ -406,7 +409,7 @@ func (m *Mount) mountWithHelper(helperBinary, typePrefix, target string) error {
return nil
}
if !errors.Is(err, unix.ECHILD) {
return errors.Wrapf(err, "mount helper [%s %v] failed: %q", helperBinary, args, string(out))
return fmt.Errorf("mount helper [%s %v] failed: %q: %w", helperBinary, args, string(out), err)
}
// We got ECHILD, we are not sure whether the mount was successful.
// If the mount ID has changed, we are sure we got some new mount, but still not sure it is fully completed.
@@ -419,5 +422,5 @@ func (m *Mount) mountWithHelper(helperBinary, typePrefix, target string) error {
_ = unmount(target, 0)
}
}
return errors.Errorf("mount helper [%s %v] failed with ECHILD (retired %d times)", helperBinary, args, retriesOnECHILD)
return fmt.Errorf("mount helper [%s %v] failed with ECHILD (retired %d times)", helperBinary, args, retriesOnECHILD)
}

View File

@@ -19,7 +19,7 @@
package mount
import "github.com/pkg/errors"
import "errors"
var (
// ErrNotImplementOnUnix is returned for methods that are not implemented

View File

@@ -18,12 +18,13 @@ package mount
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/Microsoft/hcsshim"
"github.com/pkg/errors"
)
var (
@@ -34,7 +35,7 @@ var (
// Mount to the provided target
func (m *Mount) Mount(target string) error {
if m.Type != "windows-layer" {
return errors.Errorf("invalid windows mount type: '%s'", m.Type)
return fmt.Errorf("invalid windows mount type: '%s'", m.Type)
}
home, layerID := filepath.Split(m.Source)
@@ -49,22 +50,22 @@ func (m *Mount) Mount(target string) error {
}
if err = hcsshim.ActivateLayer(di, layerID); err != nil {
return errors.Wrapf(err, "failed to activate layer %s", m.Source)
return fmt.Errorf("failed to activate layer %s: %w", m.Source, err)
}
if err = hcsshim.PrepareLayer(di, layerID, parentLayerPaths); err != nil {
return errors.Wrapf(err, "failed to prepare layer %s", m.Source)
return fmt.Errorf("failed to prepare layer %s: %w", m.Source, err)
}
// We can link the layer mount path to the given target. It is an UNC path, and it needs
// a trailing backslash.
mountPath, err := hcsshim.GetLayerMountPath(di, layerID)
if err != nil {
return errors.Wrapf(err, "failed to get layer mount path for %s", m.Source)
return fmt.Errorf("failed to get layer mount path for %s: %w", m.Source, err)
}
mountPath = mountPath + `\`
if err = os.Symlink(mountPath, target); err != nil {
return errors.Wrapf(err, "failed to link mount to taget %s", target)
return fmt.Errorf("failed to link mount to taget %s: %w", target, err)
}
return nil
}
@@ -80,7 +81,7 @@ func (m *Mount) GetParentPaths() ([]string, error) {
if strings.HasPrefix(option, ParentLayerPathsFlag) {
err := json.Unmarshal([]byte(option[len(ParentLayerPathsFlag):]), &parentLayerPaths)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal parent layer paths from mount")
return nil, fmt.Errorf("failed to unmarshal parent layer paths from mount: %w", err)
}
}
}
@@ -97,10 +98,10 @@ func Unmount(mount string, flags int) error {
)
if err := hcsshim.UnprepareLayer(di, layerID); err != nil {
return errors.Wrapf(err, "failed to unprepare layer %s", mount)
return fmt.Errorf("failed to unprepare layer %s: %w", mount, err)
}
if err := hcsshim.DeactivateLayer(di, layerID); err != nil {
return errors.Wrapf(err, "failed to deactivate layer %s", mount)
return fmt.Errorf("failed to deactivate layer %s: %w", mount, err)
}
return nil

View File

@@ -18,10 +18,10 @@ package mount
import (
"context"
"fmt"
"os"
"github.com/containerd/containerd/log"
"github.com/pkg/errors"
)
var tempMountLocation = getTempDir()
@@ -32,7 +32,7 @@ var tempMountLocation = getTempDir()
func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) error) (err error) {
root, uerr := os.MkdirTemp(tempMountLocation, "containerd-mount")
if uerr != nil {
return errors.Wrapf(uerr, "failed to create temp dir")
return fmt.Errorf("failed to create temp dir: %w", err)
}
// We use Remove here instead of RemoveAll.
// The RemoveAll will delete the temp dir and all children it contains.
@@ -50,18 +50,21 @@ func WithTempMount(ctx context.Context, mounts []Mount, f func(root string) erro
// We should do defer first, if not we will not do Unmount when only a part of Mounts are failed.
defer func() {
if uerr = UnmountAll(root, 0); uerr != nil {
uerr = errors.Wrapf(uerr, "failed to unmount %s", root)
uerr = fmt.Errorf("failed to unmount %s: %w", root, uerr)
if err == nil {
err = uerr
} else {
err = errors.Wrap(err, uerr.Error())
err = fmt.Errorf("%s: %w", uerr.Error(), err)
}
}
}()
if uerr = All(mounts, root); uerr != nil {
return errors.Wrapf(uerr, "failed to mount %s", root)
return fmt.Errorf("failed to mount %s: %w", root, uerr)
}
return errors.Wrapf(f(root), "mount callback failed on %s", root)
if err := f(root); err != nil {
return fmt.Errorf("mount callback failed on %s: %w", root, err)
}
return nil
}
func getTempDir() string {