|
1 | 1 | module Node.FS.Stats
|
2 |
| - ( Stats(..) |
3 |
| - , StatsObj |
| 2 | + ( Stats |
4 | 3 | , isFile
|
5 | 4 | , isDirectory
|
6 | 5 | , isBlockDevice
|
7 | 6 | , isCharacterDevice
|
8 | 7 | , isFIFO
|
9 | 8 | , isSocket
|
10 | 9 | , isSymbolicLink
|
| 10 | + , dev |
| 11 | + , inode |
| 12 | + , mode |
| 13 | + , nlink |
| 14 | + , uid |
| 15 | + , gid |
| 16 | + , rdev |
| 17 | + , size |
| 18 | + , blkSize |
| 19 | + , blocks |
| 20 | + , accessedTimeMs |
| 21 | + , modifiedTimeMs |
| 22 | + , statusChangedTimeMs |
| 23 | + , birthtimeMs |
11 | 24 | , accessedTime
|
12 | 25 | , modifiedTime
|
13 | 26 | , statusChangedTime
|
| 27 | + , birthTime |
14 | 28 | ) where
|
15 | 29 |
|
16 | 30 | import Prelude
|
17 | 31 |
|
18 | 32 | import Data.DateTime (DateTime)
|
19 |
| -import Data.Function.Uncurried (Fn2, Fn0, runFn2) |
| 33 | +import Data.Function.Uncurried (Fn1, runFn1) |
20 | 34 | import Data.JSDate (JSDate, toDateTime)
|
21 |
| -import Data.Maybe (fromJust) |
22 |
| -import Partial.Unsafe (unsafePartial) |
23 |
| - |
24 |
| -type StatsObj = |
25 |
| - { dev :: Number |
26 |
| - , mode :: Number |
27 |
| - , nlink :: Number |
28 |
| - , uid :: Number |
29 |
| - , gid :: Number |
30 |
| - , rdev :: Number |
31 |
| - , ino :: Number |
32 |
| - , size :: Number |
33 |
| - , atime :: JSDate |
34 |
| - , mtime :: JSDate |
35 |
| - , ctime :: JSDate |
36 |
| - , isFile :: Fn0 Boolean |
37 |
| - , isDirectory :: Fn0 Boolean |
38 |
| - , isBlockDevice :: Fn0 Boolean |
39 |
| - , isCharacterDevice :: Fn0 Boolean |
40 |
| - , isFIFO :: Fn0 Boolean |
41 |
| - , isSocket :: Fn0 Boolean |
42 |
| - } |
43 |
| - |
44 |
| --- | Stats wrapper to provide a usable interface to the underlying properties and methods. |
45 |
| -data Stats = Stats StatsObj |
46 |
| - |
47 |
| -foreign import showStatsObj :: StatsObj -> String |
| 35 | +import Data.Maybe (Maybe(..)) |
| 36 | +import Data.Time.Duration (Milliseconds) |
| 37 | +import Partial.Unsafe (unsafeCrashWith) |
48 | 38 |
|
49 |
| -instance showStats :: Show Stats where |
50 |
| - show (Stats o) = "Stats " <> showStatsObj o |
51 |
| - |
52 |
| -foreign import statsMethod :: Fn2 String StatsObj Boolean |
| 39 | +foreign import data Stats :: Type |
53 | 40 |
|
54 |
| -isFile :: Stats -> Boolean |
55 |
| -isFile (Stats s) = runFn2 statsMethod "isFile" s |
| 41 | +foreign import showStatsObj :: Stats -> String |
56 | 42 |
|
57 |
| -isDirectory :: Stats -> Boolean |
58 |
| -isDirectory (Stats s) = runFn2 statsMethod "isDirectory" s |
| 43 | +instance showStats :: Show Stats where |
| 44 | + show s = "Stats " <> showStatsObj s |
59 | 45 |
|
60 | 46 | isBlockDevice :: Stats -> Boolean
|
61 |
| -isBlockDevice (Stats s) = runFn2 statsMethod "isBlockDevice" s |
| 47 | +isBlockDevice s = runFn1 isBlockDeviceImpl s |
| 48 | + |
| 49 | +foreign import isBlockDeviceImpl :: Fn1 (Stats) (Boolean) |
62 | 50 |
|
63 | 51 | isCharacterDevice :: Stats -> Boolean
|
64 |
| -isCharacterDevice (Stats s) = runFn2 statsMethod "isCharacterDevice" s |
| 52 | +isCharacterDevice s = runFn1 isCharacterDeviceImpl s |
| 53 | + |
| 54 | +foreign import isCharacterDeviceImpl :: Fn1 (Stats) (Boolean) |
| 55 | + |
| 56 | +-- | Returns true if the <fs.Stats> object describes a file system directory. |
| 57 | +-- | If the `fs.Stats`> object was obtained from `fs.lstat()`, |
| 58 | +-- | this method will always return `false``. This is because `fs.lstat()` |
| 59 | +-- | returns information about a symbolic link itself and not the path to which it resolves. |
| 60 | +isDirectory :: Stats -> Boolean |
| 61 | +isDirectory s = runFn1 isDirectoryImpl s |
| 62 | + |
| 63 | +foreign import isDirectoryImpl :: Fn1 (Stats) (Boolean) |
65 | 64 |
|
66 | 65 | isFIFO :: Stats -> Boolean
|
67 |
| -isFIFO (Stats s) = runFn2 statsMethod "isFIFO" s |
| 66 | +isFIFO s = runFn1 isFIFOImpl s |
| 67 | + |
| 68 | +foreign import isFIFOImpl :: Fn1 (Stats) (Boolean) |
| 69 | + |
| 70 | +isFile :: Stats -> Boolean |
| 71 | +isFile s = runFn1 isFileImpl s |
| 72 | + |
| 73 | +foreign import isFileImpl :: Fn1 (Stats) (Boolean) |
68 | 74 |
|
69 | 75 | isSocket :: Stats -> Boolean
|
70 |
| -isSocket (Stats s) = runFn2 statsMethod "isSocket" s |
| 76 | +isSocket s = runFn1 isSocketImpl s |
| 77 | + |
| 78 | +foreign import isSocketImpl :: Fn1 (Stats) (Boolean) |
71 | 79 |
|
72 | 80 | isSymbolicLink :: Stats -> Boolean
|
73 |
| -isSymbolicLink (Stats s) = runFn2 statsMethod "isSymbolicLink" s |
| 81 | +isSymbolicLink s = runFn1 isSymbolicLinkImpl s |
| 82 | + |
| 83 | +foreign import isSymbolicLinkImpl :: Fn1 (Stats) (Boolean) |
| 84 | + |
| 85 | +-- | The numeric identifier of the device containing the file. |
| 86 | +dev :: Stats -> Number |
| 87 | +dev s = runFn1 devImpl s |
| 88 | + |
| 89 | +foreign import devImpl :: Fn1 (Stats) (Number) |
| 90 | + |
| 91 | +-- | The file system specific "Inode" number for the file. |
| 92 | +inode :: Stats -> Number |
| 93 | +inode s = runFn1 inodeImpl s |
| 94 | + |
| 95 | +foreign import inodeImpl :: Fn1 (Stats) (Number) |
| 96 | + |
| 97 | +-- | A bit-field describing the file type and mode. |
| 98 | +mode :: Stats -> Number |
| 99 | +mode s = runFn1 modeImpl s |
| 100 | + |
| 101 | +foreign import modeImpl :: Fn1 (Stats) (Number) |
| 102 | + |
| 103 | +-- | The number of hard-links that exist for the file. |
| 104 | +nlink :: Stats -> Number |
| 105 | +nlink s = runFn1 nlinkImpl s |
| 106 | + |
| 107 | +foreign import nlinkImpl :: Fn1 (Stats) (Number) |
| 108 | + |
| 109 | +-- | The numeric user identifier of the user that owns the file (POSIX). |
| 110 | +uid :: Stats -> Number |
| 111 | +uid s = runFn1 uidImpl s |
| 112 | + |
| 113 | +foreign import uidImpl :: Fn1 (Stats) (Number) |
| 114 | + |
| 115 | +-- | The numeric group identifier of the group that owns the file (POSIX). |
| 116 | +gid :: Stats -> Number |
| 117 | +gid s = runFn1 gidImpl s |
| 118 | + |
| 119 | +foreign import gidImpl :: Fn1 (Stats) (Number) |
| 120 | + |
| 121 | +-- | A numeric device identifier if the file represents a device. |
| 122 | +rdev :: Stats -> Number |
| 123 | +rdev s = runFn1 rdevImpl s |
| 124 | + |
| 125 | +foreign import rdevImpl :: Fn1 (Stats) (Number) |
| 126 | + |
| 127 | +-- | The size of the file in bytes. |
| 128 | +-- | If the underlying file system does not support getting the size of the file, this will be 0. |
| 129 | +size :: Stats -> Number |
| 130 | +size s = runFn1 sizeImpl s |
| 131 | + |
| 132 | +foreign import sizeImpl :: Fn1 (Stats) (Number) |
| 133 | + |
| 134 | +-- | The file system block size for i/o operations. |
| 135 | +blkSize :: Stats -> Number |
| 136 | +blkSize s = runFn1 blkSizeImpl s |
| 137 | + |
| 138 | +foreign import blkSizeImpl :: Fn1 (Stats) (Number) |
| 139 | + |
| 140 | +-- | The number of blocks allocated for this file. |
| 141 | +blocks :: Stats -> Number |
| 142 | +blocks s = runFn1 blocksImpl s |
| 143 | + |
| 144 | +foreign import blocksImpl :: Fn1 (Stats) (Number) |
| 145 | + |
| 146 | +accessedTimeMs :: Stats -> Milliseconds |
| 147 | +accessedTimeMs s = runFn1 accessedTimeMsImpl s |
| 148 | + |
| 149 | +foreign import accessedTimeMsImpl :: Fn1 (Stats) (Milliseconds) |
| 150 | + |
| 151 | +modifiedTimeMs :: Stats -> Milliseconds |
| 152 | +modifiedTimeMs s = runFn1 modifiedTimeMsImpl s |
| 153 | + |
| 154 | +foreign import modifiedTimeMsImpl :: Fn1 (Stats) (Milliseconds) |
| 155 | + |
| 156 | +statusChangedTimeMs :: Stats -> Milliseconds |
| 157 | +statusChangedTimeMs s = runFn1 statusChangedTimeMsImpl s |
| 158 | + |
| 159 | +foreign import statusChangedTimeMsImpl :: Fn1 (Stats) (Milliseconds) |
| 160 | + |
| 161 | +birthtimeMs :: Stats -> Milliseconds |
| 162 | +birthtimeMs s = runFn1 birthtimeMsImpl s |
| 163 | + |
| 164 | +foreign import birthtimeMsImpl :: Fn1 (Stats) (Milliseconds) |
74 | 165 |
|
75 | 166 | accessedTime :: Stats -> DateTime
|
76 |
| -accessedTime (Stats s) = unsafePartial $ fromJust (toDateTime s.atime) |
| 167 | +accessedTime s = case toDateTime $ runFn1 accessedTimeImpl s of |
| 168 | + Just d -> d |
| 169 | + Nothing -> unsafeCrashWith $ "Impossible: `accessedTime` returned invalid DateTime value." |
| 170 | + |
| 171 | +foreign import accessedTimeImpl :: Fn1 (Stats) (JSDate) |
77 | 172 |
|
78 | 173 | modifiedTime :: Stats -> DateTime
|
79 |
| -modifiedTime (Stats s) = unsafePartial $ fromJust (toDateTime s.mtime) |
| 174 | +modifiedTime s = case toDateTime $ runFn1 modifiedTimeImpl s of |
| 175 | + Just d -> d |
| 176 | + Nothing -> unsafeCrashWith $ "Impossible: `modifiedTime` returned invalid DateTime value." |
| 177 | + |
| 178 | +foreign import modifiedTimeImpl :: Fn1 (Stats) (JSDate) |
80 | 179 |
|
81 | 180 | statusChangedTime :: Stats -> DateTime
|
82 |
| -statusChangedTime (Stats s) = unsafePartial $ fromJust (toDateTime s.ctime) |
| 181 | +statusChangedTime s = case toDateTime $ runFn1 statusChangedTimeImpl s of |
| 182 | + Just d -> d |
| 183 | + Nothing -> unsafeCrashWith $ "Impossible: `statusChangedTime` returned invalid DateTime value." |
| 184 | + |
| 185 | +foreign import statusChangedTimeImpl :: Fn1 (Stats) (JSDate) |
| 186 | + |
| 187 | +birthTime :: Stats -> DateTime |
| 188 | +birthTime s = case toDateTime $ runFn1 birthTimeImpl s of |
| 189 | + Just d -> d |
| 190 | + Nothing -> unsafeCrashWith $ "Impossible: `birthTime` returned invalid DateTime value." |
| 191 | + |
| 192 | +foreign import birthTimeImpl :: Fn1 (Stats) (JSDate) |
0 commit comments