@@ -26,11 +26,12 @@ import Data.Int (Int(), fromNumber, toNumber)
26
26
newtype Perm = Perm { r :: Boolean , w :: Boolean , x :: Boolean }
27
27
28
28
-- | A `Perms` value includes all the permissions information about a
29
- -- | particular file or directory,
29
+ -- | particular file or directory, by storing a `Perm` value for each of the
30
+ -- | file owner, the group, and others.
30
31
newtype Perms = Perms { u :: Perm , g :: Perm , o :: Perm }
31
32
32
- -- | No permissions. This is the identity of the Semigroup (<>) operation for
33
- -- | Perm.
33
+ -- | No permissions. This is the identity of the ` Semigroup` operation ` (<>)`
34
+ -- | for ` Perm` .
34
35
none :: Perm
35
36
none = Perm { r: false , w: false , x: false }
36
37
@@ -54,10 +55,13 @@ instance semigroupPerm :: Semigroup Perm where
54
55
(<>) (Perm { r = r0, w = w0, x = x0 }) (Perm { r = r1, w = w1, x = x1 }) =
55
56
Perm { r: r0 || r1, w: w0 || w1, x: x0 || x1 }
56
57
58
+ -- | Attempt to parse a `Perms` value from a `String` containing an octal
59
+ -- | integer. For example,
60
+ -- | `permsFromString "644" == Just (mkPerms (r <> w) r r)`.
57
61
permsFromString :: String -> Maybe Perms
58
62
permsFromString = _perms <<< toCharArray
59
63
where
60
- _perms (u : g : o : [] ) =
64
+ _perms [u, g, o] =
61
65
mkPerms <$> permFromChar u
62
66
<*> permFromChar g
63
67
<*> permFromChar o
@@ -77,7 +81,7 @@ permFromChar = _perm <<< charString
77
81
_perm _ = Nothing
78
82
79
83
-- | Create a `Perm` value. The arguments represent the readable, writable, and
80
- -- | executable permissions respectively .
84
+ -- | executable permissions, in that order .
81
85
mkPerm :: Boolean -> Boolean -> Boolean -> Perm
82
86
mkPerm r w x = Perm { r: r, w: w, x: x }
83
87
@@ -86,15 +90,25 @@ mkPerm r w x = Perm { r: r, w: w, x: x }
86
90
mkPerms :: Perm -> Perm -> Perm -> Perms
87
91
mkPerms u g o = Perms { u: u, g: g, o: o }
88
92
93
+ -- | Convert a `Perm` to an octal digit. For example:
94
+ -- |
95
+ -- | * `permToInt r == 4`
96
+ -- | * `permToInt w == 2`
97
+ -- | * `permToInt (r <> w) == 6`
89
98
permToInt :: Perm -> Int
90
99
permToInt (Perm { r = r, w = w, x = x }) = fromNumber $
91
100
(if r then 4 else 0 )
92
101
+ (if w then 2 else 0 )
93
102
+ (if x then 1 else 0 )
94
103
104
+ -- | Convert a `Perm` to an octal string, via `permToInt`.
95
105
permToString :: Perm -> String
96
106
permToString = show <<< toNumber <<< permToInt
97
107
108
+ -- | Convert a `Perms` value to an octal string, in a format similar to that
109
+ -- | accepted by `chmod`. For example:
110
+ -- |
111
+ -- | * `permsToString (mkPerms (r <> w) r r) == "0644"`
98
112
permsToString :: Perms -> String
99
113
permsToString (Perms { u = u, g = g, o = o }) =
100
114
" 0"
0 commit comments