| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package hostarch |
| 16 |
|
| 17 |
import "golang.org/x/sys/unix" |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
type AccessType struct { |
| 24 |
|
| 25 |
Read bool |
| 26 |
|
| 27 |
|
| 28 |
Write bool |
| 29 |
|
| 30 |
|
| 31 |
Execute bool |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
func (a AccessType) String() string { |
| 37 |
bits := [3]byte{'-', '-', '-'} |
| 38 |
if a.Read { |
| 39 |
bits[0] = 'r' |
| 40 |
} |
| 41 |
if a.Write { |
| 42 |
bits[1] = 'w' |
| 43 |
} |
| 44 |
if a.Execute { |
| 45 |
bits[2] = 'x' |
| 46 |
} |
| 47 |
return string(bits[:]) |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
func (a AccessType) Any() bool { |
| 52 |
return a.Read || a.Write || a.Execute |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
func (a AccessType) Prot() int { |
| 57 |
var prot int |
| 58 |
if a.Read { |
| 59 |
prot |= unix.PROT_READ |
| 60 |
} |
| 61 |
if a.Write { |
| 62 |
prot |= unix.PROT_WRITE |
| 63 |
} |
| 64 |
if a.Execute { |
| 65 |
prot |= unix.PROT_EXEC |
| 66 |
} |
| 67 |
return prot |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func (a AccessType) SupersetOf(other AccessType) bool { |
| 73 |
if !a.Read && other.Read { |
| 74 |
return false |
| 75 |
} |
| 76 |
if !a.Write && other.Write { |
| 77 |
return false |
| 78 |
} |
| 79 |
if !a.Execute && other.Execute { |
| 80 |
return false |
| 81 |
} |
| 82 |
return true |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
func (a AccessType) Intersect(other AccessType) AccessType { |
| 87 |
return AccessType{ |
| 88 |
Read: a.Read && other.Read, |
| 89 |
Write: a.Write && other.Write, |
| 90 |
Execute: a.Execute && other.Execute, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
func (a AccessType) Union(other AccessType) AccessType { |
| 96 |
return AccessType{ |
| 97 |
Read: a.Read || other.Read, |
| 98 |
Write: a.Write || other.Write, |
| 99 |
Execute: a.Execute || other.Execute, |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
func (a AccessType) Effective() AccessType { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if a.Write || a.Execute { |
| 113 |
a.Read = true |
| 114 |
} |
| 115 |
return a |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
var ( |
| 120 |
NoAccess = AccessType{} |
| 121 |
Read = AccessType{Read: true} |
| 122 |
Write = AccessType{Write: true} |
| 123 |
Execute = AccessType{Execute: true} |
| 124 |
ReadWrite = AccessType{Read: true, Write: true} |
| 125 |
AnyAccess = AccessType{Read: true, Write: true, Execute: true} |
| 126 |
) |