From 57ed19749ea7a411550cf9a37eab147a6d778dae Mon Sep 17 00:00:00 2001 From: Liam Galvin Date: Mon, 12 Nov 2018 10:30:42 +0000 Subject: [PATCH] improve permission annotation --- hints/perms.go | 64 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 10 deletions(-) diff --git a/hints/perms.go b/hints/perms.go index 83ed533..57454ce 100644 --- a/hints/perms.go +++ b/hints/perms.go @@ -6,14 +6,34 @@ import ( "strings" ) +type fileType uint8 + +const ( + file fileType = iota + directory + characterSpecialFile +) + type perms struct { - IsDirectory bool - Owner access - Group access - World access + Type fileType + Owner access + Group access + World access + SetUID bool + SetGID bool + Sticky bool } +type accessType uint8 + +const ( + owner accessType = iota + group + world +) + type access struct { + Type accessType Read bool Write bool Execute bool @@ -57,14 +77,28 @@ func parsePermissionString(s string) (perms, error) { return perms{}, fmt.Errorf("Invalid permission string") } 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.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.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.Write = s[8] == 'w' p.World.Execute = s[9] == 'x' @@ -93,8 +127,11 @@ func hintPerms(word string, context string, wordX uint16, wordY uint16) *Hint { } typ := "file" - if p.IsDirectory { + switch p.Type { + case directory: typ = "directory" + case characterSpecialFile: + typ = "character special file" } item.Description = fmt.Sprintf(`Permissions: @@ -102,12 +139,19 @@ func hintPerms(word string, context string, wordX uint16, wordY uint16) *Hint { Numeric: %s Owner: %s Group: %s - World: %s`, + World: %s + Setuid: %t + Setgid: %t + Sticky: %t + `, typ, p.Numeric(), p.Owner.Nice(), p.Group.Nice(), p.World.Nice(), + p.SetUID, + p.SetGID, + p.Sticky, ) return item @@ -117,6 +161,6 @@ func hintPerms(word string, context string, wordX uint16, wordY uint16) *Hint { } 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) }