diff --git a/interp/builtin.go b/interp/builtin.go index 8dc45cb4..b24a3d6a 100644 --- a/interp/builtin.go +++ b/interp/builtin.go @@ -963,7 +963,7 @@ func (r *Runner) changeDir(ctx context.Context, path string) int { if err != nil || !info.IsDir() { return 1 } - if !hasPermissionToDir(info) { + if !hasPermissionToDir(path) { return 1 } r.Dir = path diff --git a/interp/os_notunix.go b/interp/os_notunix.go index f2409321..66ed777a 100644 --- a/interp/os_notunix.go +++ b/interp/os_notunix.go @@ -7,7 +7,6 @@ package interp import ( "fmt" - "os" ) func mkfifo(path string, mode uint32) error { @@ -15,6 +14,6 @@ func mkfifo(path string, mode uint32) error { } // hasPermissionToDir is a no-op on Windows. -func hasPermissionToDir(info os.FileInfo) bool { +func hasPermissionToDir(string) bool { return true } diff --git a/interp/os_unix.go b/interp/os_unix.go index e393399c..3a82d69b 100644 --- a/interp/os_unix.go +++ b/interp/os_unix.go @@ -6,11 +6,6 @@ package interp import ( - "os" - "os/user" - "strconv" - "syscall" - "golang.org/x/sys/unix" ) @@ -18,40 +13,8 @@ func mkfifo(path string, mode uint32) error { return unix.Mkfifo(path, mode) } -// hasPermissionToDir returns if the OS current user has execute permission -// to the given directory -func hasPermissionToDir(info os.FileInfo) bool { - user, err := user.Current() - if err != nil { - return false // unknown user; assume no permissions - } - uid, err := strconv.Atoi(user.Uid) - if err != nil { - return false // on POSIX systems, Uid should always be a decimal number - } - if uid == 0 { - return true // super-user - } - - st, _ := info.Sys().(*syscall.Stat_t) - if st == nil { - panic("unexpected info.Sys type") - } - perm := info.Mode().Perm() - // user (u) - if perm&0o100 != 0 && st.Uid == uint32(uid) { - return true - } - - gid, _ := strconv.Atoi(user.Gid) - // other users in group (g) - if perm&0o010 != 0 && st.Uid != uint32(uid) && st.Gid == uint32(gid) { - return true - } - // remaining users (o) - if perm&0o001 != 0 && st.Uid != uint32(uid) && st.Gid != uint32(gid) { - return true - } - - return false +// hasPermissionToDir returns true if the OS current user has execute +// permission to the given directory +func hasPermissionToDir(path string) bool { + return unix.Access(path, unix.X_OK) == nil }