This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perm.go
72 lines (62 loc) · 1.61 KB
/
perm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package ldbperm
import (
"fmt"
"os"
"strconv"
"github.com/syndtr/goleveldb/leveldb"
)
type LDBPerm struct {
db *leveldb.DB
defaultUser string
defaultGroup string
defaultMode os.FileMode
}
func NewLDBPerm(db *leveldb.DB, user, group string, mode os.FileMode) *LDBPerm {
return &LDBPerm{db, user, group, mode}
}
func (db *LDBPerm) GetOwner(rPath string) (string, error) {
v, err := db.db.Get([]byte(fmt.Sprintf("p:owner:%s", rPath)), nil)
if err != nil {
if err == leveldb.ErrNotFound {
return db.defaultUser, nil
}
return "", err
}
return string(v), nil
}
func (db *LDBPerm) GetGroup(rPath string) (string, error) {
v, err := db.db.Get([]byte(fmt.Sprintf("p:group:%s", rPath)), nil)
if err != nil {
if err == leveldb.ErrNotFound {
return db.defaultGroup, nil
}
return "", err
}
return string(v), nil
}
func (db *LDBPerm) GetMode(rPath string) (os.FileMode, error) {
v, err := db.db.Get([]byte(fmt.Sprintf("p:mode:%s", rPath)), nil)
if err != nil {
if err == leveldb.ErrNotFound {
return db.defaultMode, nil
}
return 0, err
}
mode, err := strconv.Atoi(string(v))
if err != nil {
return 0, err
}
return os.FileMode(mode), nil
}
func (db *LDBPerm) ChOwner(rPath, owner string) error {
return db.db.Put([]byte(fmt.Sprintf("p:owner:%s", rPath)),
[]byte(owner), nil)
}
func (db *LDBPerm) ChGroup(rPath, group string) error {
return db.db.Put([]byte(fmt.Sprintf("p:group:%s", rPath)),
[]byte(group), nil)
}
func (db *LDBPerm) ChMode(rPath string, mode os.FileMode) error {
return db.db.Put([]byte(fmt.Sprintf("p:mode:%s", rPath)),
[]byte(strconv.Itoa(int(mode))), nil)
}