Merge pull request #57 from liamg/permission-extras

improve permission annotation
This commit is contained in:
Liam Galvin 2018-11-12 10:33:37 +00:00 committed by GitHub
commit 7b181ac241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 54 additions and 10 deletions

View File

@ -6,14 +6,34 @@ import (
"strings" "strings"
) )
type fileType uint8
const (
file fileType = iota
directory
characterSpecialFile
)
type perms struct { type perms struct {
IsDirectory bool Type fileType
Owner access Owner access
Group access Group access
World access World access
SetUID bool
SetGID bool
Sticky bool
} }
type accessType uint8
const (
owner accessType = iota
group
world
)
type access struct { type access struct {
Type accessType
Read bool Read bool
Write bool Write bool
Execute bool Execute bool
@ -57,14 +77,28 @@ func parsePermissionString(s string) (perms, error) {
return perms{}, fmt.Errorf("Invalid permission string") return perms{}, fmt.Errorf("Invalid permission string")
} }
p := perms{} p := perms{}
p.IsDirectory = s[0] == 'd' switch s[0] {
case 'c':
p.Type = characterSpecialFile
case 'd':
p.Type = directory
default:
p.Type = file
}
p.SetUID = s[3] == 's' || s[3] == 'S'
p.SetGID = s[6] == 's' || s[6] == 'S'
p.Sticky = s[9] == 't' || s[9] == 'T'
p.Owner.Type = owner
p.Owner.Read = s[1] == 'r' p.Owner.Read = s[1] == 'r'
p.Owner.Write = s[2] == 'w' p.Owner.Write = s[2] == 'w'
p.Owner.Execute = s[3] == 'x' p.Owner.Execute = s[3] == 'x' || s[3] == 's'
p.Group.Type = group
p.Group.Read = s[4] == 'r' p.Group.Read = s[4] == 'r'
p.Group.Write = s[5] == 'w' p.Group.Write = s[5] == 'w'
p.Group.Execute = s[6] == 'x' p.Group.Execute = s[6] == 'x' || s[6] == 's'
p.World.Type = world
p.World.Read = s[7] == 'r' p.World.Read = s[7] == 'r'
p.World.Write = s[8] == 'w' p.World.Write = s[8] == 'w'
p.World.Execute = s[9] == 'x' p.World.Execute = s[9] == 'x'
@ -93,8 +127,11 @@ func hintPerms(word string, context string, wordX uint16, wordY uint16) *Hint {
} }
typ := "file" typ := "file"
if p.IsDirectory { switch p.Type {
case directory:
typ = "directory" typ = "directory"
case characterSpecialFile:
typ = "character special file"
} }
item.Description = fmt.Sprintf(`Permissions: item.Description = fmt.Sprintf(`Permissions:
@ -102,12 +139,19 @@ func hintPerms(word string, context string, wordX uint16, wordY uint16) *Hint {
Numeric: %s Numeric: %s
Owner: %s Owner: %s
Group: %s Group: %s
World: %s`, World: %s
Setuid: %t
Setgid: %t
Sticky: %t
`,
typ, typ,
p.Numeric(), p.Numeric(),
p.Owner.Nice(), p.Owner.Nice(),
p.Group.Nice(), p.Group.Nice(),
p.World.Nice(), p.World.Nice(),
p.SetUID,
p.SetGID,
p.Sticky,
) )
return item return item
@ -117,6 +161,6 @@ func hintPerms(word string, context string, wordX uint16, wordY uint16) *Hint {
} }
func isPermString(s string) bool { func isPermString(s string) bool {
re := regexp.MustCompile("[dl\\-sS]{1}[sSrwx\\-]{9}") re := regexp.MustCompile("[cdl\\-sS]{1}[sStTrwx\\-]{9}")
return re.MatchString(s) return re.MatchString(s)
} }