/
Perms.purs
187 lines (159 loc) · 5.73 KB
/
Perms.purs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
module Node.FS.Perms
( Perm
, mkPerm
, none
, read
, write
, execute
, all
, Perms
, mkPerms
, permsAll
, permsReadWrite
, permsFromString
, permsToString
, permsToInt
) where
import Prelude
import Data.Enum (toEnum)
import Data.Int (fromStringAs, octal)
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.String (Pattern(Pattern), joinWith, drop, indexOf)
import Data.String.CodeUnits (charAt, toCharArray)
import Partial.Unsafe (unsafePartial)
-- | A `Perm` value specifies what is allowed to be done with a particular
-- | file by a particular class of user — that is, whether it is
-- | readable, writable, and/or executable. It has a `Semiring` instance, which
-- | allows you to combine permissions:
-- |
-- | - `(+)` adds `Perm` values together. For example, `read + write` means
-- | "readable and writable".
-- | - `(*)` masks permissions. It can be thought of as selecting only the
-- | permissions that two `Perm` values have in common. For example:
-- | `(read + write) * (write + execute) == write`.
-- |
-- | You can think also of a `Perm` value as a subset of the set
-- | `{ read, write, execute }`; then, `(+)` and `(*)` represent set union and
-- | intersection respectively.
newtype Perm = Perm { r :: Boolean, w :: Boolean, x :: Boolean }
derive newtype instance eqPerm :: Eq Perm
instance ordPerm :: Ord Perm where
compare (Perm { r: r1, w: w1, x: x1 }) (Perm { r: r2, w: w2, x: x2 }) =
compare [ r1, w1, x1 ] [ r2, w2, x2 ]
instance showPerm :: Show Perm where
show p | p == none = "none"
show p | p == all = "all"
show (Perm { r: r, w: w, x: x }) =
joinWith " + " ps
where
ps =
(if r then [ "read" ] else [])
<> (if w then [ "write" ] else [])
<>
(if x then [ "execute" ] else [])
instance semiringPerm :: Semiring Perm where
add (Perm { r: r0, w: w0, x: x0 }) (Perm { r: r1, w: w1, x: x1 }) =
Perm { r: r0 || r1, w: w0 || w1, x: x0 || x1 }
zero = Perm { r: false, w: false, x: false }
mul (Perm { r: r0, w: w0, x: x0 }) (Perm { r: r1, w: w1, x: x1 }) =
Perm { r: r0 && r1, w: w0 && w1, x: x0 && x1 }
one = Perm { r: true, w: true, x: true }
-- | Create a `Perm` value. The arguments represent the readable, writable, and
-- | executable permissions, in that order.
mkPerm :: Boolean -> Boolean -> Boolean -> Perm
mkPerm r w x = Perm { r: r, w: w, x: x }
-- | No permissions. This is the identity of the `Semiring` operation `(+)`
-- | for `Perm`; that is, it is the same as `zero`.
none :: Perm
none = zero
-- | The "readable" permission.
read :: Perm
read = Perm { r: true, w: false, x: false }
-- | The "writable" permission.
write :: Perm
write = Perm { r: false, w: true, x: false }
-- | The "executable" permission.
execute :: Perm
execute = Perm { r: false, w: false, x: true }
-- | All permissions: readable, writable, and executable. This is the identity
-- | of the `Semiring` operation `(*)` for `Perm`; that is, it is the same as
-- | `one`.
all :: Perm
all = one
-- | A `Perms` value includes all the permissions information about a
-- | particular file or directory, by storing a `Perm` value for each of the
-- | file owner, the group, and any other users.
newtype Perms = Perms { u :: Perm, g :: Perm, o :: Perm }
derive newtype instance eqPerms :: Eq Perms
instance ordPerms :: Ord Perms where
compare (Perms { u: u1, g: g1, o: o1 }) (Perms { u: u2, g: g2, o: o2 }) =
compare [ u1, g1, o1 ] [ u2, g2, o2 ]
instance showPerms :: Show Perms where
show (Perms { u: u, g: g, o: o }) =
"mkPerms " <> joinWith " " (f <$> [ u, g, o ])
where
f perm =
let
str = show perm
in
if isNothing $ indexOf (Pattern " ") str then str
else "(" <> str <> ")"
-- | Attempt to parse a `Perms` value from a `String` containing an octal
-- | integer. For example,
-- | `permsFromString "0644" == Just (mkPerms (read + write) read read)`.
permsFromString :: String -> Maybe Perms
permsFromString = _perms <<< toCharArray <<< dropPrefix zeroChar
where
zeroChar = unsafePartial $ fromJust $ toEnum 48
dropPrefix x xs
| charAt 0 xs == Just x = drop 1 xs
| otherwise = xs
_perms [ u, g, o ] =
mkPerms <$> permFromChar u
<*> permFromChar g
<*> permFromChar o
_perms _ = Nothing
permFromChar :: Char -> Maybe Perm
permFromChar c = case c of
'0' -> Just $ none
'1' -> Just $ execute
'2' -> Just $ write
'3' -> Just $ write + execute
'4' -> Just $ read
'5' -> Just $ read + execute
'6' -> Just $ read + write
'7' -> Just $ read + write + execute
_ -> Nothing
-- | Create a `Perms` value. The arguments represent the owner's, group's, and
-- | other users' permission sets, respectively.
mkPerms :: Perm -> Perm -> Perm -> Perms
mkPerms u g o = Perms { u: u, g: g, o: o }
permsAll :: Perms
permsAll = mkPerms all all all
permsReadWrite :: Perms
permsReadWrite = mkPerms all all none
-- | Convert a `Perm` to an octal digit. For example:
-- |
-- | * `permToInt r == 4`
-- | * `permToInt w == 2`
-- | * `permToInt (r + w) == 6`
permToInt :: Perm -> Int
permToInt (Perm { r: r, w: w, x: x }) =
(if r then 4 else 0)
+ (if w then 2 else 0)
+ (if x then 1 else 0)
-- | Convert a `Perm` to an octal string, via `permToInt`.
permToString :: Perm -> String
permToString = show <<< permToInt
-- | Convert a `Perms` value to an octal string, in a format similar to that
-- | accepted by `chmod`. For example:
-- | `permsToString (mkPerms (read + write) read read) == "0644"`
permsToString :: Perms -> String
permsToString (Perms { u: u, g: g, o: o }) =
"0"
<> permToString u
<> permToString g
<> permToString o
-- | Convert a `Perms` value to an `Int`, via `permsToString`.
permsToInt :: Perms -> Int
permsToInt = unsafePartial $ fromJust <<< fromStringAs octal <<< permsToString