Skip to content

Commit

Permalink
Send Unix errors without wrapping them or casting them.
Browse files Browse the repository at this point in the history
While trying to import MountedFast into k8s to detect mount points
using openat2, several tests failed because they rely on
os.IsErrNotExist / os.IsPermission which do not actually test the
underlying error.

For a non-existent path, if we return the bare error, this problem
is solved. I tested for the following cases to ensure that
backward compatibility is not broken:

fmt.Println(os.IsNotExist(err)) returns false
fmt.Println(os.IsNotExist(&os.PathError{
		Op:   "lstat",
		Path: "somepath",
		Err:  err,
})) returns false

fmt.Println(os.IsNotExist(fmt.Errorf("unable: %w", err))) returns false
fmt.Println(errors.Is(fmt.Errorf("unable: %w", err), os.ErrNotExist))
returns true.
  • Loading branch information
manugupt1 committed Apr 5, 2022
1 parent 0335593 commit 13957d6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions mountinfo/mounted_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func mountedByOpenat2(path string) (bool, error) {
Flags: unix.O_PATH | unix.O_CLOEXEC,
})
if err != nil {
return false, &os.PathError{Op: "openat2", Path: dir, Err: err}
return false, err
}
fd, err := unix.Openat2(dirfd, last, &unix.OpenHow{
Flags: unix.O_PATH | unix.O_CLOEXEC | unix.O_NOFOLLOW,
Expand All @@ -59,7 +59,7 @@ func mountedByOpenat2(path string) (bool, error) {
return true, nil
}
// not sure
return false, &os.PathError{Op: "openat2", Path: path, Err: err}
return false, err
}

// mountedFast is similar to MountedFast, except it expects a normalized path.
Expand Down
11 changes: 5 additions & 6 deletions mountinfo/mounted_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package mountinfo

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -15,12 +14,12 @@ func mountedByStat(path string) (bool, error) {
var st unix.Stat_t

if err := unix.Lstat(path, &st); err != nil {
return false, &os.PathError{Op: "stat", Path: path, Err: err}
return false, err
}
dev := st.Dev
parent := filepath.Dir(path)
if err := unix.Lstat(parent, &st); err != nil {
return false, &os.PathError{Op: "stat", Path: parent, Err: err}
return false, err
}
if dev != st.Dev {
// Device differs from that of parent,
Expand All @@ -33,13 +32,13 @@ func mountedByStat(path string) (bool, error) {

func normalizePath(path string) (realPath string, err error) {
if realPath, err = filepath.Abs(path); err != nil {
return "", fmt.Errorf("unable to get absolute path for %q: %w", path, err)
return "", err
}
if realPath, err = filepath.EvalSymlinks(realPath); err != nil {
return "", fmt.Errorf("failed to canonicalise path for %q: %w", path, err)
return "", err
}
if _, err := os.Stat(realPath); err != nil {
return "", fmt.Errorf("failed to stat target of %q: %w", path, err)
return "", err
}
return realPath, nil
}
Expand Down

0 comments on commit 13957d6

Please sign in to comment.