Skip to content

Commit fa7b8a6

Browse files
committed
Initial commit
0 parents  commit fa7b8a6

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# perm
2+
3+
Print human-friendly descriptions of a file's permissions
4+
5+
## Usage:
6+
7+
```
8+
$ ls perm/perm.go
9+
-rw-r--r-- 1 tiago tiago 906 ago 24 22:29 perm/perm.go
10+
$ go run main.go --file perm/perm.go
11+
owner can read and write to it
12+
group members can read from it
13+
others can read from it
14+
```

main.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/tmatias/perm/perm"
10+
)
11+
12+
func main() {
13+
f := flag.String("file", "", "file for which to describe permissions")
14+
flag.Parse()
15+
16+
fi, err := os.Stat(*f)
17+
if err != nil {
18+
log.Fatal(err)
19+
}
20+
bits := fi.Mode().Perm()
21+
owner, group, other := perm.Permissions(int(bits))
22+
23+
printPerm("owner", owner)
24+
printPerm("group members", group)
25+
printPerm("others", other)
26+
}
27+
28+
func printPerm(who string, bits int) {
29+
what, err := perm.Description(bits)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
fmt.Printf("%s %s\n", who, what)
34+
}

perm/perm.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package perm
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Description returns an human-friendly description of unix file permission bits
8+
func Description(bits int) (string, error) {
9+
switch bits {
10+
case 0:
11+
return "cannot do anything", nil
12+
case 1:
13+
return "can execute it", nil
14+
case 2:
15+
return "can write to it", nil
16+
case 3:
17+
return "can execute and write to it", nil
18+
case 4:
19+
return "can read from it", nil
20+
case 5:
21+
return "can read and execute it", nil
22+
case 6:
23+
return "can read and write to it", nil
24+
case 7:
25+
return "can read, write and execute it", nil
26+
default:
27+
return "", fmt.Errorf("Invalid bits %d", bits)
28+
}
29+
}
30+
31+
// Permissions return three integers containing permissions for the owner, group and everyone else
32+
// from an int representing unix file permissions bits
33+
func Permissions(bits int) (owner int, group int, other int) {
34+
owner = bits >> 6 & 7
35+
group = bits >> 3 & 7
36+
other = bits & 7
37+
return
38+
}

perm/perm_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package perm
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPermDescription(t *testing.T) {
8+
tt := []struct {
9+
name string
10+
bits int
11+
desc string
12+
}{
13+
{"no permission", 0, "cannot do anything"},
14+
{"execute permission", 1, "can execute it"},
15+
{"write permission", 2, "can write to it"},
16+
{"execute and write permission", 3, "can execute and write to it"},
17+
{"read permission", 4, "can read from it"},
18+
{"read and execute permission", 5, "can read and execute it"},
19+
{"write permission", 6, "can read and write to it"},
20+
{"read, write and execute permission", 7, "can read, write and execute it"},
21+
}
22+
23+
for _, tc := range tt {
24+
t.Run(tc.name, func(t *testing.T) {
25+
desc, err := Description(tc.bits)
26+
if err != nil {
27+
t.Error(err)
28+
}
29+
if desc != tc.desc {
30+
t.Errorf("expected description of %d to be '%s'; got '%s'", tc.bits, tc.desc, desc)
31+
}
32+
})
33+
}
34+
}
35+
36+
func TestPermissions(t *testing.T) {
37+
tt := []struct {
38+
name string
39+
bits int
40+
owner int
41+
group int
42+
other int
43+
}{
44+
{"world readable and writable", 0777, 7, 7, 7},
45+
{"readable only by the owner", 0400, 4, 0, 0},
46+
}
47+
48+
for _, tc := range tt {
49+
t.Run(tc.name, func(t *testing.T) {
50+
owner, group, other := Permissions(tc.bits)
51+
if owner != tc.owner {
52+
t.Errorf("expected owner permission for %d to be %d; got %d", tc.bits, tc.owner, owner)
53+
}
54+
if group != tc.group {
55+
t.Errorf("expected group permission for %d to be %d; got %d", tc.bits, tc.group, owner)
56+
}
57+
if other != tc.other {
58+
t.Errorf("expected other permission for %d to be %d; got %d", tc.bits, tc.other, owner)
59+
}
60+
})
61+
}
62+
}

0 commit comments

Comments
 (0)