Skip to content

Commit

Permalink
Implement lstat (#66)
Browse files Browse the repository at this point in the history
* Implement `lstat`

* update changelog

* use `map` instead of `(<$>)`
  • Loading branch information
artemisSystem authored Jun 10, 2022
1 parent 3b55fe6 commit c6c52c5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Add `lstat` (#66 by @artemisSystem)

- Update rmdir' to take options arg
- Added rm and rm' version with and without options arg
Expand Down
1 change: 1 addition & 0 deletions src/Node/FS/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
chown as chownImpl,
chmod as chmodImpl,
stat as statImpl,
lstat as lstatImpl,
link as linkImpl,
symlink as symlinkImpl,
readlink as readlinkImpl,
Expand Down
13 changes: 12 additions & 1 deletion src/Node/FS/Async.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Node.FS.Async
, truncate
, chown
, chmod
, lstat
, stat
, link
, symlink
Expand Down Expand Up @@ -73,6 +74,7 @@ foreign import truncateImpl :: Fn3 FilePath Int (JSCallback Unit) Unit
foreign import chownImpl :: Fn4 FilePath Int Int (JSCallback Unit) Unit
foreign import chmodImpl :: Fn3 FilePath String (JSCallback Unit) Unit
foreign import statImpl :: Fn2 FilePath (JSCallback StatsObj) Unit
foreign import lstatImpl :: Fn2 FilePath (JSCallback StatsObj) Unit
foreign import linkImpl :: Fn3 FilePath FilePath (JSCallback Unit) Unit
foreign import symlinkImpl :: Fn4 FilePath FilePath String (JSCallback Unit) Unit
foreign import readlinkImpl :: Fn2 FilePath (JSCallback FilePath) Unit
Expand Down Expand Up @@ -134,7 +136,16 @@ stat :: FilePath
-> Effect Unit

stat file cb = mkEffect $ \_ -> runFn2
statImpl file (handleCallback $ cb <<< (<$>) Stats)
statImpl file (handleCallback $ cb <<< map Stats)

-- | Gets file or symlink statistics. `lstat` is identical to `stat`, except
-- | that if the `FilePath` is a symbolic link, then the link itself is stat-ed,
-- | not the file that it refers to.
lstat :: FilePath
-> Callback Stats
-> Effect Unit
lstat file cb = mkEffect $ \_ -> runFn2
lstatImpl file (handleCallback $ cb <<< map Stats)

-- | Creates a link to an existing file.
link :: FilePath
Expand Down
1 change: 1 addition & 0 deletions src/Node/FS/Sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
chownSync as chownSyncImpl,
chmodSync as chmodSyncImpl,
statSync as statSyncImpl,
lstatSync as lstatSyncImpl,
linkSync as linkSyncImpl,
symlinkSync as symlinkSyncImpl,
readlinkSync as readlinkSyncImpl,
Expand Down
11 changes: 11 additions & 0 deletions src/Node/FS/Sync.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Node.FS.Sync
, chown
, chmod
, stat
, lstat
, link
, symlink
, readlink
Expand Down Expand Up @@ -60,6 +61,7 @@ foreign import truncateSyncImpl :: Fn2 FilePath Int Unit
foreign import chownSyncImpl :: Fn3 FilePath Int Int Unit
foreign import chmodSyncImpl :: Fn2 FilePath String Unit
foreign import statSyncImpl :: Fn1 FilePath StatsObj
foreign import lstatSyncImpl :: Fn1 FilePath StatsObj
foreign import linkSyncImpl :: Fn2 FilePath FilePath Unit
foreign import symlinkSyncImpl :: Fn3 FilePath FilePath String Unit
foreign import readlinkSyncImpl :: Fn1 FilePath FilePath
Expand Down Expand Up @@ -120,6 +122,15 @@ stat :: FilePath
stat file = map Stats $ mkEffect $ \_ -> runFn1
statSyncImpl file

-- | Gets file or symlink statistics. `lstat` is identical to `stat`, except
-- | that if the `FilePath` is a symbolic link, then the link itself is stat-ed,
-- | not the file that it refers to.
lstat :: FilePath
-> Effect Stats

lstat file = map Stats $ mkEffect $ \_ -> runFn1
lstatSyncImpl file

-- | Creates a link to an existing file.
link :: FilePath
-> FilePath
Expand Down
28 changes: 26 additions & 2 deletions test/Test.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Effect.Console (log)
import Effect.Exception (Error, error, throwException, catchException)
import Node.Buffer as Buffer
import Node.Encoding (Encoding(..))
import Node.FS (FileFlags(..))
import Node.FS (FileFlags(..), SymlinkType(..))
import Node.FS.Async as A
import Node.FS.Stats (statusChangedTime, accessedTime, modifiedTime, isSymbolicLink, isSocket, isFIFO, isCharacterDevice, isBlockDevice, isDirectory, isFile)
import Node.FS.Sync as S
Expand Down Expand Up @@ -74,6 +74,15 @@ main = do
log "statusChangedTime:"
log $ show $ statusChangedTime stats

S.symlink (fp ["tmp", "Test1.js"]) (fp ["tmp", "TestSymlink.js"]) FileLink

lstats <- S.lstat (fp ["tmp", "TestSymlink.js"])
log "\n\nS.lstat:"
log "isSymbolicLink:"
log $ show $ isSymbolicLink lstats

S.unlink (fp ["tmp", "TestSymlink.js"])

A.rename (fp ["tmp", "Test1.js"]) (fp ["tmp", "Test.js"]) $ \x -> do
log "\n\nrename result:"
either (log <<< show) (log <<< show) x
Expand All @@ -92,7 +101,7 @@ main = do
either (log <<< show) log x

A.stat (fp ["test", "Test.purs"]) $ \x -> do
log "\n\nstat:"
log "\n\nA.stat:"
case x of
Left err -> log $ "Error:" <> show err
Right x' -> do
Expand All @@ -117,6 +126,21 @@ main = do
log "statusChangedTime:"
log $ show $ statusChangedTime x'

A.symlink (fp ["tmp", "Test.js"]) (fp ["tmp", "TestSymlink.js"]) FileLink \u ->
case u of
Left err -> log $ "Error:" <> show err
Right _ -> A.lstat (fp ["tmp", "TestSymlink.js"]) \s -> do
log "\n\nA.lstat:"
case s of
Left err -> log $ "Error:" <> show err
Right s' -> do
log "isSymbolicLink:"
log $ show $ isSymbolicLink s'

A.unlink (fp ["tmp", "TestSymlink.js"]) \result -> do
log "\n\nA.unlink result:"
either (log <<< show) (\_ -> log "Success") result

let fdFile = fp ["tmp", "FD.json"]
fd0 <- S.fdOpen fdFile W (Just 420)
buf0 <- Buffer.fromString "[ 42 ]" UTF8
Expand Down

0 comments on commit c6c52c5

Please sign in to comment.