lib9p

Go 9P library.
Log | Files | Refs | LICENSE

uid.go (1232B)


      1 package lib9p
      2 
      3 import (
      4 	"io/fs"
      5 )
      6 
      7 // hasPerm reports whether the user uid have the permission p on the File f.
      8 // p is logical OR of AREAD, AWRITE, AEXEC.
      9 func hasPerm(fsys FS, fi fs.FileInfo, uid string, p fs.FileMode) bool {
     10 	fp := fi.Mode().Perm()
     11 	stat := fi.Sys().(*Stat)
     12 	m := fp & 7 // other
     13 	if (p & m) == p {
     14 		return true
     15 	}
     16 	if stat.Uid == uid {
     17 		m |= (fp >> 6) & 7
     18 		if (p & m) == p {
     19 			return true
     20 		}
     21 	}
     22 	if isGroupMember(fsys, stat.Gid, uid) {
     23 		m |= (fp >> 3) & 7
     24 		if (p & m) == p {
     25 			return true
     26 		}
     27 	}
     28 	return false
     29 }
     30 
     31 // IsGroupMember reports whether the user uid belongs to the group gid.
     32 // If fsys implement GroupFS, it calls fsys.IsGroupMember, otherwise,
     33 // it just reports whether uid is equal to gid.
     34 func isGroupMember(fsys FS, gid, uid string) bool {
     35 	if fsys, ok := fsys.(GroupFS); ok {
     36 		return fsys.IsGroupMember(gid, uid)
     37 	}
     38 	return gid == uid
     39 }
     40 
     41 // IsGroupLeader reports whether the user uid is the leader of the group gid.
     42 // If fsys implement GroupFS, it calls fsys.IsGroupLeader, otherwise,
     43 // it just reports whether uid is equal to gid.
     44 func isGroupLeader(fsys FS, gid, uid string) bool {
     45 	if fsys, ok := fsys.(GroupFS); ok {
     46 		return fsys.IsGroupLeader(gid, uid)
     47 	}
     48 	return gid == uid
     49 }