102 lines
1.6 KiB
Go
102 lines
1.6 KiB
Go
|
package acl
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
NullAttr string = ""
|
||
|
AllAttr string = "*"
|
||
|
)
|
||
|
|
||
|
type AclBit struct {
|
||
|
Acl uint64
|
||
|
Atr string
|
||
|
}
|
||
|
|
||
|
// Initialize acl bits and atributes
|
||
|
func NewAclBit(def uint64, atr string) *AclBit {
|
||
|
acl := new(AclBit)
|
||
|
|
||
|
acl.Acl = def
|
||
|
acl.Atr = atr
|
||
|
|
||
|
return acl
|
||
|
}
|
||
|
|
||
|
// Set acl bits and atributes
|
||
|
func (a *AclBit) Set(acl uint64, atr string) {
|
||
|
a.Acl = acl
|
||
|
a.Atr = atr
|
||
|
}
|
||
|
|
||
|
// Set array acl bits and atributes
|
||
|
func (a *AclBit) SetArray(acl []AclBit) {
|
||
|
var endacl uint64
|
||
|
var endatr string
|
||
|
lenacl := len(acl)
|
||
|
for _, a0 := range acl {
|
||
|
endacl = endacl | a0.Acl
|
||
|
if a0.Atr != NullAttr {
|
||
|
endatr = endatr + a0.Atr
|
||
|
if lenacl > 1 {
|
||
|
endatr = endatr + ","
|
||
|
}
|
||
|
}
|
||
|
lenacl--
|
||
|
}
|
||
|
a.Acl = endacl
|
||
|
a.Atr = endatr
|
||
|
}
|
||
|
|
||
|
// set acl bits
|
||
|
func (a *AclBit) SetAcl(acl uint64) {
|
||
|
a.Acl = acl
|
||
|
}
|
||
|
|
||
|
// set acl atributes
|
||
|
func (a *AclBit) SetAtr(atr string) {
|
||
|
a.Atr = atr
|
||
|
}
|
||
|
|
||
|
// get current acl
|
||
|
func (a *AclBit) Get() *AclBit {
|
||
|
return a
|
||
|
}
|
||
|
|
||
|
// set acl bit in true
|
||
|
func (a *AclBit) SetBitTrue(n int) {
|
||
|
a.Acl = a.Acl | (1 << n)
|
||
|
}
|
||
|
|
||
|
// set acl bit in false
|
||
|
func (a *AclBit) SetBitFalse(n int) {
|
||
|
a.Acl = a.Acl &^ (1 << n)
|
||
|
}
|
||
|
|
||
|
// get acl bit return true or false
|
||
|
func (a *AclBit) GetBit(n int) bool {
|
||
|
var msk uint64 = 1 << n
|
||
|
if (a.Acl & msk) == msk {
|
||
|
return true
|
||
|
} else {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// converting acl bits in string
|
||
|
func (a *AclBit) StringAcl() string {
|
||
|
return strconv.FormatUint(uint64(a.Acl), 2)
|
||
|
}
|
||
|
|
||
|
// converting string bits to uint64
|
||
|
func Uint64(str string) (uint64, error) {
|
||
|
u64, err := strconv.ParseUint(str, 2, 64)
|
||
|
if err != nil {
|
||
|
return 0, errors.New("string not bit formats")
|
||
|
}
|
||
|
return u64, nil
|
||
|
}
|