Skip to content

Commit c6c52c5

Browse files
Implement lstat (#66)
* Implement `lstat` * update changelog * use `map` instead of `(<$>)`
1 parent 3b55fe6 commit c6c52c5

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add `lstat` (#66 by @artemisSystem)
1011

1112
- Update rmdir' to take options arg
1213
- Added rm and rm' version with and without options arg

src/Node/FS/Async.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export {
44
chown as chownImpl,
55
chmod as chmodImpl,
66
stat as statImpl,
7+
lstat as lstatImpl,
78
link as linkImpl,
89
symlink as symlinkImpl,
910
readlink as readlinkImpl,

src/Node/FS/Async.purs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Node.FS.Async
44
, truncate
55
, chown
66
, chmod
7+
, lstat
78
, stat
89
, link
910
, symlink
@@ -73,6 +74,7 @@ foreign import truncateImpl :: Fn3 FilePath Int (JSCallback Unit) Unit
7374
foreign import chownImpl :: Fn4 FilePath Int Int (JSCallback Unit) Unit
7475
foreign import chmodImpl :: Fn3 FilePath String (JSCallback Unit) Unit
7576
foreign import statImpl :: Fn2 FilePath (JSCallback StatsObj) Unit
77+
foreign import lstatImpl :: Fn2 FilePath (JSCallback StatsObj) Unit
7678
foreign import linkImpl :: Fn3 FilePath FilePath (JSCallback Unit) Unit
7779
foreign import symlinkImpl :: Fn4 FilePath FilePath String (JSCallback Unit) Unit
7880
foreign import readlinkImpl :: Fn2 FilePath (JSCallback FilePath) Unit
@@ -134,7 +136,16 @@ stat :: FilePath
134136
-> Effect Unit
135137

136138
stat file cb = mkEffect $ \_ -> runFn2
137-
statImpl file (handleCallback $ cb <<< (<$>) Stats)
139+
statImpl file (handleCallback $ cb <<< map Stats)
140+
141+
-- | Gets file or symlink statistics. `lstat` is identical to `stat`, except
142+
-- | that if the `FilePath` is a symbolic link, then the link itself is stat-ed,
143+
-- | not the file that it refers to.
144+
lstat :: FilePath
145+
-> Callback Stats
146+
-> Effect Unit
147+
lstat file cb = mkEffect $ \_ -> runFn2
148+
lstatImpl file (handleCallback $ cb <<< map Stats)
138149

139150
-- | Creates a link to an existing file.
140151
link :: FilePath

src/Node/FS/Sync.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export {
44
chownSync as chownSyncImpl,
55
chmodSync as chmodSyncImpl,
66
statSync as statSyncImpl,
7+
lstatSync as lstatSyncImpl,
78
linkSync as linkSyncImpl,
89
symlinkSync as symlinkSyncImpl,
910
readlinkSync as readlinkSyncImpl,

src/Node/FS/Sync.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Node.FS.Sync
44
, chown
55
, chmod
66
, stat
7+
, lstat
78
, link
89
, symlink
910
, readlink
@@ -60,6 +61,7 @@ foreign import truncateSyncImpl :: Fn2 FilePath Int Unit
6061
foreign import chownSyncImpl :: Fn3 FilePath Int Int Unit
6162
foreign import chmodSyncImpl :: Fn2 FilePath String Unit
6263
foreign import statSyncImpl :: Fn1 FilePath StatsObj
64+
foreign import lstatSyncImpl :: Fn1 FilePath StatsObj
6365
foreign import linkSyncImpl :: Fn2 FilePath FilePath Unit
6466
foreign import symlinkSyncImpl :: Fn3 FilePath FilePath String Unit
6567
foreign import readlinkSyncImpl :: Fn1 FilePath FilePath
@@ -120,6 +122,15 @@ stat :: FilePath
120122
stat file = map Stats $ mkEffect $ \_ -> runFn1
121123
statSyncImpl file
122124

125+
-- | Gets file or symlink statistics. `lstat` is identical to `stat`, except
126+
-- | that if the `FilePath` is a symbolic link, then the link itself is stat-ed,
127+
-- | not the file that it refers to.
128+
lstat :: FilePath
129+
-> Effect Stats
130+
131+
lstat file = map Stats $ mkEffect $ \_ -> runFn1
132+
lstatSyncImpl file
133+
123134
-- | Creates a link to an existing file.
124135
link :: FilePath
125136
-> FilePath

test/Test.purs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Effect.Console (log)
1010
import Effect.Exception (Error, error, throwException, catchException)
1111
import Node.Buffer as Buffer
1212
import Node.Encoding (Encoding(..))
13-
import Node.FS (FileFlags(..))
13+
import Node.FS (FileFlags(..), SymlinkType(..))
1414
import Node.FS.Async as A
1515
import Node.FS.Stats (statusChangedTime, accessedTime, modifiedTime, isSymbolicLink, isSocket, isFIFO, isCharacterDevice, isBlockDevice, isDirectory, isFile)
1616
import Node.FS.Sync as S
@@ -74,6 +74,15 @@ main = do
7474
log "statusChangedTime:"
7575
log $ show $ statusChangedTime stats
7676

77+
S.symlink (fp ["tmp", "Test1.js"]) (fp ["tmp", "TestSymlink.js"]) FileLink
78+
79+
lstats <- S.lstat (fp ["tmp", "TestSymlink.js"])
80+
log "\n\nS.lstat:"
81+
log "isSymbolicLink:"
82+
log $ show $ isSymbolicLink lstats
83+
84+
S.unlink (fp ["tmp", "TestSymlink.js"])
85+
7786
A.rename (fp ["tmp", "Test1.js"]) (fp ["tmp", "Test.js"]) $ \x -> do
7887
log "\n\nrename result:"
7988
either (log <<< show) (log <<< show) x
@@ -92,7 +101,7 @@ main = do
92101
either (log <<< show) log x
93102

94103
A.stat (fp ["test", "Test.purs"]) $ \x -> do
95-
log "\n\nstat:"
104+
log "\n\nA.stat:"
96105
case x of
97106
Left err -> log $ "Error:" <> show err
98107
Right x' -> do
@@ -117,6 +126,21 @@ main = do
117126
log "statusChangedTime:"
118127
log $ show $ statusChangedTime x'
119128

129+
A.symlink (fp ["tmp", "Test.js"]) (fp ["tmp", "TestSymlink.js"]) FileLink \u ->
130+
case u of
131+
Left err -> log $ "Error:" <> show err
132+
Right _ -> A.lstat (fp ["tmp", "TestSymlink.js"]) \s -> do
133+
log "\n\nA.lstat:"
134+
case s of
135+
Left err -> log $ "Error:" <> show err
136+
Right s' -> do
137+
log "isSymbolicLink:"
138+
log $ show $ isSymbolicLink s'
139+
140+
A.unlink (fp ["tmp", "TestSymlink.js"]) \result -> do
141+
log "\n\nA.unlink result:"
142+
either (log <<< show) (\_ -> log "Success") result
143+
120144
let fdFile = fp ["tmp", "FD.json"]
121145
fd0 <- S.fdOpen fdFile W (Just 420)
122146
buf0 <- Buffer.fromString "[ 42 ]" UTF8

0 commit comments

Comments
 (0)