pkg/idtools: CanAccess(): reorder checks to allow early return
Merge the accessible() function into CanAccess, and check world-
readable permissions first, before checking owner and group.
@@ -90,20 +90,17 @@ func CanAccess(path string, pair Identity) bool {
if err != nil {
return false
}
- fileMode := os.FileMode(statInfo.Mode())
- permBits := fileMode.Perm()
- return accessible(statInfo.UID() == uint32(pair.UID),
- statInfo.GID() == uint32(pair.GID), permBits)
-}
-
-func accessible(isOwner, isGroup bool, perms os.FileMode) bool {
- if isOwner && (perms&0100 == 0100) {
+ perms := os.FileMode(statInfo.Mode()).Perm()
+ if perms&0o001 == 0o001 {
+ // world access
return true
}
- if isGroup && (perms&0010 == 0010) {
+ if statInfo.UID() == uint32(pair.UID) && (perms&0o100 == 0o100) {
+ // owner access.
return true
}
- if perms&0001 == 0001 {
+ if statInfo.GID() == uint32(pair.GID) && (perms&0o010 == 0o010) {
+ // group access.
return true
}
return false
