Skip to content

Implement lstat #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Bugfixes:

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 @@ -70,6 +71,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 @@ -130,7 +132,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 @@ -57,6 +58,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 @@ -116,6 +118,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