-
Notifications
You must be signed in to change notification settings - Fork 472
/
permissions.go
69 lines (61 loc) 路 2.8 KB
/
permissions.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
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2023, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*--------------------------------------------------------------------------- */
package shared
// Permissions is associated to OpenNebula resources
// * uu: USER USE bit.
// * um: USER MANAGE bit.
// * ua: USER ADMIN bit.
// * gu: GROUP USE bit.
// * gm: GROUP MANAGE bit.
// * ga: GROUP ADMIN bit.
// * ou: OTHER USE bit.
// * om: OTHER MANAGE bit.
// * oa: OTHER ADMIN bit.
type Permissions struct {
OwnerU int8 `xml:"OWNER_U" json:"OWNER_U,string"`
OwnerM int8 `xml:"OWNER_M" json:"OWNER_M,string"`
OwnerA int8 `xml:"OWNER_A" json:"OWNER_A,string"`
GroupU int8 `xml:"GROUP_U" json:"GROUP_U,string"`
GroupM int8 `xml:"GROUP_M" json:"GROUP_M,string"`
GroupA int8 `xml:"GROUP_A" json:"GROUP_A,string"`
OtherU int8 `xml:"OTHER_U" json:"OTHER_U,string"`
OtherM int8 `xml:"OTHER_M" json:"OTHER_M,string"`
OtherA int8 `xml:"OTHER_A" json:"OTHER_A,string"`
}
var permStr = [8]string{"---", "--a", "-m-", "-ma", "u--", "u-a", "um-", "uma"}
func NewDefaultPermission() Permissions {
return Permissions{-1, -1, -1, -1, -1, -1, -1, -1, -1}
}
// If a bit is set to -1, it will not change when calling Chmod
func (p Permissions) ToArgs() []interface{} {
return []interface{}{
p.OwnerU, p.OwnerM, p.OwnerA,
p.GroupU, p.GroupM, p.GroupA,
p.OtherU, p.OtherM, p.OtherA,
}
}
func (p Permissions) String() string {
owner := permStr[p.OwnerU<<2|p.OwnerM<<1|p.OwnerA]
group := permStr[p.GroupU<<2|p.GroupM<<1|p.GroupA]
other := permStr[p.OtherU<<2|p.OtherM<<1|p.OtherA]
return owner + group + other
}
func (p Permissions) Octet() int {
owner := int(p.OwnerU<<2|p.OwnerM<<1|p.OwnerA)
group := int(p.GroupU<<2|p.GroupM<<1|p.GroupA)
other := int(p.OtherU<<2|p.OtherM<<1|p.OtherA)
return owner * 100 + group * 10 + other
}