Skip to content

Commit 1658abe

Browse files
felixSchlhdgarrood
authored andcommitted
Use a proper type for file permissions
1 parent 0a94fd7 commit 1658abe

File tree

5 files changed

+162
-24
lines changed

5 files changed

+162
-24
lines changed

README.md

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ chown :: forall eff. FilePath -> Number -> Number -> Callback eff Unit -> Eff (f
6161
#### `chmod`
6262

6363
``` purescript
64-
chmod :: forall eff. FilePath -> Number -> Callback eff Unit -> Eff (fs :: FS | eff) Unit
64+
chmod :: forall eff. FilePath -> Perms -> Callback eff Unit -> Eff (fs :: FS | eff) Unit
6565
```
6666

6767
#### `stat`
@@ -121,7 +121,7 @@ mkdir :: forall eff. FilePath -> Callback eff Unit -> Eff (fs :: FS | eff) Unit
121121
#### `mkdir'`
122122

123123
``` purescript
124-
mkdir' :: forall eff. FilePath -> Number -> Callback eff Unit -> Eff (fs :: FS | eff) Unit
124+
mkdir' :: forall eff. FilePath -> Perms -> Callback eff Unit -> Eff (fs :: FS | eff) Unit
125125
```
126126

127127
#### `readdir`
@@ -179,6 +179,51 @@ exists :: forall eff. FilePath -> (Boolean -> Eff eff Unit) -> Eff (fs :: FS | e
179179
```
180180

181181

182+
## Module Node.FS.Perms
183+
184+
#### `Perms`
185+
186+
``` purescript
187+
data Perms
188+
```
189+
190+
191+
#### `permsFromString`
192+
193+
``` purescript
194+
permsFromString :: String -> Maybe Perms
195+
```
196+
197+
198+
#### `permsToString`
199+
200+
``` purescript
201+
permsToString :: Perms -> String
202+
```
203+
204+
205+
#### `permsToNum`
206+
207+
``` purescript
208+
permsToNum :: Perms -> Number
209+
```
210+
211+
212+
#### `showPerm`
213+
214+
``` purescript
215+
instance showPerm :: Show Perm
216+
```
217+
218+
219+
#### `showPerms`
220+
221+
``` purescript
222+
instance showPerms :: Show Perms
223+
```
224+
225+
226+
182227
## Module Node.FS.Stats
183228

184229
#### `StatsObj`
@@ -357,7 +402,7 @@ chown :: forall eff. FilePath -> Number -> Number -> Eff (err :: Exception, fs :
357402
#### `chmod`
358403

359404
``` purescript
360-
chmod :: forall eff. FilePath -> Number -> Eff (err :: Exception, fs :: FS | eff) Unit
405+
chmod :: forall eff. FilePath -> Perms -> Eff (err :: Exception, fs :: FS | eff) Unit
361406
```
362407

363408
#### `stat`
@@ -417,7 +462,7 @@ mkdir :: forall eff. FilePath -> Eff (err :: Exception, fs :: FS | eff) Unit
417462
#### `mkdir'`
418463

419464
``` purescript
420-
mkdir' :: forall eff. FilePath -> Number -> Eff (err :: Exception, fs :: FS | eff) Unit
465+
mkdir' :: forall eff. FilePath -> Perms -> Eff (err :: Exception, fs :: FS | eff) Unit
421466
```
422467

423468
#### `readdir`
@@ -514,4 +559,4 @@ fdFlush :: forall eff. FileDescriptor -> Eff (fs :: FS, err :: Exception | eff)
514559

515560
``` purescript
516561
fdClose :: forall eff. FileDescriptor -> Eff (fs :: FS, err :: Exception | eff) Unit
517-
```
562+
```

bower.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"purescript-node-buffer": "~0.0.1",
2525
"purescript-node-path": "~0.3.0",
2626
"purescript-datetime": "~0.5.0",
27-
"purescript-exceptions": "~0.2.1"
27+
"purescript-exceptions": "~0.2.1",
28+
"purescript-strings": "~0.4.5",
29+
"purescript-globals": "~0.1.6",
30+
"purescript-integers": "~0.1.0"
2831
}
2932
}

src/Node/FS/Async.purs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ import Data.Time
3333
import Data.Either
3434
import Data.Function
3535
import Data.Maybe
36+
import Data.Maybe.Unsafe(fromJust)
3637
import Node.Buffer (Buffer(..))
3738
import Node.Encoding
3839
import Node.FS
3940
import Node.FS.Stats
4041
import Node.Path (FilePath())
42+
import Node.FS.Perms
4143

4244
foreign import data Nullable :: * -> *
4345

@@ -61,15 +63,15 @@ foreign import fs "var fs = require('fs');" ::
6163
{ rename :: Fn3 FilePath FilePath (JSCallback Unit) Unit
6264
, truncate :: Fn3 FilePath Number (JSCallback Unit) Unit
6365
, chown :: Fn4 FilePath Number Number (JSCallback Unit) Unit
64-
, chmod :: Fn3 FilePath Number (JSCallback Unit) Unit
66+
, chmod :: Fn3 FilePath String (JSCallback Unit) Unit
6567
, stat :: Fn2 FilePath (JSCallback StatsObj) Unit
6668
, link :: Fn3 FilePath FilePath (JSCallback Unit) Unit
6769
, symlink :: Fn4 FilePath FilePath String (JSCallback Unit) Unit
6870
, readlink :: Fn2 FilePath (JSCallback FilePath) Unit
6971
, realpath :: forall cache. Fn3 FilePath { | cache } (JSCallback FilePath) Unit
7072
, unlink :: Fn2 FilePath (JSCallback Unit) Unit
7173
, rmdir :: Fn2 FilePath (JSCallback Unit) Unit
72-
, mkdir :: Fn3 FilePath Number (JSCallback Unit) Unit
74+
, mkdir :: Fn3 FilePath String (JSCallback Unit) Unit
7375
, readdir :: Fn2 FilePath (JSCallback [FilePath]) Unit
7476
, utimes :: Fn4 FilePath Number Number (JSCallback Unit) Unit
7577
, readFile :: forall a opts. Fn3 FilePath { | opts } (JSCallback a) Unit
@@ -126,12 +128,12 @@ chown file uid gid cb = mkEff $ \_ -> runFn4
126128
-- Changes the permissions of a file.
127129
--
128130
chmod :: forall eff. FilePath
129-
-> Number
131+
-> Perms
130132
-> Callback eff Unit
131133
-> Eff (fs :: FS | eff) Unit
132134

133-
chmod file mode cb = mkEff $ \_ -> runFn3
134-
fs.chmod file mode (handleCallback cb)
135+
chmod file perms cb = mkEff $ \_ -> runFn3
136+
fs.chmod file (permsToString perms) (handleCallback cb)
135137

136138
-- |
137139
-- Gets file statistics.
@@ -225,18 +227,18 @@ mkdir :: forall eff. FilePath
225227
-> Callback eff Unit
226228
-> Eff (fs :: FS | eff) Unit
227229

228-
mkdir = flip mkdir' 777
230+
mkdir = flip mkdir' $ mkPerms (r <> w <> x) (r <> w <> x) (r <> w <> x)
229231

230232
-- |
231233
-- Makes a new directory with the specified permissions.
232234
--
233235
mkdir' :: forall eff. FilePath
234-
-> Number
236+
-> Perms
235237
-> Callback eff Unit
236238
-> Eff (fs :: FS | eff) Unit
237239

238-
mkdir' file mode cb = mkEff $ \_ -> runFn3
239-
fs.mkdir file mode (handleCallback cb)
240+
mkdir' file perms cb = mkEff $ \_ -> runFn3
241+
fs.mkdir file (permsToString perms) (handleCallback cb)
240242

241243
-- |
242244
-- Reads the contents of a directory.

src/Node/FS/Perms.purs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
module Node.FS.Perms
2+
( permsFromString
3+
, permsToString
4+
, permsToInt
5+
, none
6+
, r
7+
, w
8+
, x
9+
, mkPerms
10+
, Perms()
11+
, Perm()
12+
) where
13+
14+
import Global (readInt)
15+
import Data.Maybe (Maybe(..))
16+
import Data.Char (Char(), charString)
17+
import Data.String (toCharArray)
18+
import Data.Int (Int(), fromNumber, toNumber)
19+
20+
newtype Perm = Perm { r :: Boolean, w :: Boolean, x :: Boolean }
21+
newtype Perms = Perms { u :: Perm, g :: Perm, o :: Perm }
22+
23+
none = Perm { r: false, w: false, x: false }
24+
r = Perm { r: true, w: false, x: false }
25+
w = Perm { r: false, w: true, x: false }
26+
x = Perm { r: false, w: false, x: true }
27+
28+
instance semigroupPerm :: Semigroup Perm where
29+
(<>) (Perm { r = r0, w = w0, x = x0 }) (Perm { r = r1, w = w1, x = x1 }) =
30+
Perm { r: r0 || r1, w: w0 || w1, x: x0 || x1 }
31+
32+
permsFromString :: String -> Maybe Perms
33+
permsFromString = _perms <<< toCharArray
34+
where
35+
_perms (u : g : o : []) =
36+
mkPerms <$> permFromChar u
37+
<*> permFromChar g
38+
<*> permFromChar o
39+
_perms _ = Nothing
40+
41+
permFromChar :: Char -> Maybe Perm
42+
permFromChar = _perm <<< charString
43+
where
44+
_perm "0" = Just $ none
45+
_perm "1" = Just $ x
46+
_perm "2" = Just $ w
47+
_perm "3" = Just $ w <> x
48+
_perm "4" = Just $ r
49+
_perm "5" = Just $ r <> x
50+
_perm "6" = Just $ r <> w
51+
_perm "7" = Just $ r <> w <> x
52+
_perm _ = Nothing
53+
54+
mkPerm :: Boolean -> Boolean -> Boolean -> Perm
55+
mkPerm r w x = Perm { r: r, w: w, x: x }
56+
57+
mkPerms :: Perm -> Perm -> Perm -> Perms
58+
mkPerms u g o = Perms { u: u, g: g, o: o }
59+
60+
permToInt :: Perm -> Int
61+
permToInt (Perm { r = r, w = w, x = x }) = fromNumber $
62+
(if r then 4 else 0)
63+
+ (if w then 2 else 0)
64+
+ (if x then 1 else 0)
65+
66+
permToString :: Perm -> String
67+
permToString = show <<< toNumber <<< permToInt
68+
69+
permsToString :: Perms -> String
70+
permsToString (Perms { u = u, g = g, o = o }) =
71+
"0"
72+
++ permToString u
73+
++ permToString g
74+
++ permToString o
75+
76+
permsToInt :: Perms -> Int
77+
permsToInt p = fromNumber $ readInt 8 $ permsToString p
78+
79+
instance showPerm :: Show Perm where
80+
show (Perm { r = r, w = w, x = x }) =
81+
(if r then "r" else "-")
82+
++ (if w then "w" else "-")
83+
++ (if x then "x" else "-")
84+
85+
instance showPerms :: Show Perms where
86+
show (Perms { u = u, g = g, o = o }) = show u ++ show g ++ show o

src/Node/FS/Sync.purs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ import Data.Time
4545
import Data.Either
4646
import Data.Function
4747
import Data.Maybe (Maybe(..))
48+
import Data.Maybe.Unsafe(fromJust)
4849
import Node.Buffer (Buffer(..), size)
4950
import Node.Encoding
5051
import Node.FS
5152
import Node.FS.Stats
5253
import Node.Path (FilePath())
54+
import Node.FS.Perms
5355

5456
foreign import data FileDescriptor :: *
5557

@@ -67,15 +69,15 @@ foreign import fs "var fs = require('fs');" ::
6769
{ renameSync :: Fn2 FilePath FilePath Unit
6870
, truncateSync :: Fn2 FilePath Number Unit
6971
, chownSync :: Fn3 FilePath Number Number Unit
70-
, chmodSync :: Fn2 FilePath Number Unit
72+
, chmodSync :: Fn2 FilePath String Unit
7173
, statSync :: Fn1 FilePath StatsObj
7274
, linkSync :: Fn2 FilePath FilePath Unit
7375
, symlinkSync :: Fn3 FilePath FilePath String Unit
7476
, readlinkSync :: Fn1 FilePath FilePath
7577
, realpathSync :: forall cache. Fn2 FilePath { | cache } FilePath
7678
, unlinkSync :: Fn1 FilePath Unit
7779
, rmdirSync :: Fn1 FilePath Unit
78-
, mkdirSync :: Fn2 FilePath Number Unit
80+
, mkdirSync :: Fn2 FilePath String Unit
7981
, readdirSync :: Fn1 FilePath [FilePath]
8082
, utimesSync :: Fn3 FilePath Number Number Unit
8183
, readFileSync :: forall a opts. Fn2 FilePath { | opts } a
@@ -157,11 +159,11 @@ chown file uid gid = mkEff $ \_ -> runFn3
157159
-- Changes the permissions of a file.
158160
--
159161
chmod :: forall eff. FilePath
160-
-> Number
162+
-> Perms
161163
-> Eff (fs :: FS, err :: Exception | eff) Unit
162164

163-
chmod file mode = mkEff $ \_ -> runFn2
164-
fs.chmodSync file mode
165+
chmod file perms = mkEff $ \_ -> runFn2
166+
fs.chmodSync file (permsToString perms)
165167

166168
-- |
167169
-- Gets file statistics.
@@ -246,17 +248,17 @@ rmdir file = mkEff $ \_ -> runFn1
246248
mkdir :: forall eff. FilePath
247249
-> Eff (fs :: FS, err :: Exception | eff) Unit
248250

249-
mkdir = flip mkdir' 777
251+
mkdir = flip mkdir' $ mkPerms (r <> w <> x) (r <> w <> x) (r <> w <> x)
250252

251253
-- |
252254
-- Makes a new directory with the specified permissions.
253255
--
254256
mkdir' :: forall eff. FilePath
255-
-> Number
257+
-> Perms
256258
-> Eff (fs :: FS, err :: Exception | eff) Unit
257259

258-
mkdir' file mode = mkEff $ \_ -> runFn2
259-
fs.mkdirSync file mode
260+
mkdir' file perms = mkEff $ \_ -> runFn2
261+
fs.mkdirSync file (permsToString perms)
260262

261263
-- |
262264
-- Reads the contents of a directory.

0 commit comments

Comments
 (0)