{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Z.IO.FileSystem
(
File, initFile, readFile, writeFile
, FileMode(DEFAULT_MODE, S_IRWXU, S_IRUSR, S_IWUSR
, S_IXUSR, S_IRWXG, S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH
)
, FileFlag(O_APPEND, O_CREAT, O_DIRECT, O_DSYNC, O_EXCL
, O_EXLOCK, O_NOATIME, O_NOFOLLOW, O_RDONLY, O_RDWR, O_SYMLINK
, O_SYNC, O_TRUNC, O_WRONLY, O_RANDOM, O_SHORT_LIVED, O_SEQUENTIAL, O_TEMPORARY
)
, mkdir
, unlink
, mkdtemp
, rmdir
, DirEntType(..)
, scandir
, UVStat(..), UVTimeSpec(..)
, stat, lstat, fstat
, rename
, fsync, fdatasync
, ftruncate
, UVCopyFileFlag(COPYFILE_DEFAULT, COPYFILE_EXCL, COPYFILE_FICLONE)
, copyfile
, UVAccessMode(F_OK, R_OK, W_OK, X_OK)
, AccessResult(..)
, access
, chmod, fchmod
, utime, futime
, UVSymlinkFlag(SYMLINK_DEFAULT, SYMLINK_DIR, SYMLINK_JUNCTION)
, link, symlink
, readlink, realpath
) where
import Control.Monad
import Data.Word
import Data.Int
import Data.IORef
import Foreign.Ptr
import Foreign.Storable (peekElemOff)
import Foreign.Marshal.Alloc (allocaBytes)
import Z.Data.CBytes as CBytes
import Z.Foreign
import Z.IO.Buffered
import Z.IO.Exception
import Z.IO.Resource
import Z.IO.UV.Errno
import Z.IO.UV.FFI
import Prelude hiding (writeFile, readFile)
data File = File {-# UNPACK #-} !UVFD
{-# UNPACK #-} !(IORef Bool)
checkFileClosed :: HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed :: File -> (UVFD -> IO a) -> IO a
checkFileClosed (File UVFD
fd IORef Bool
closedRef) UVFD -> IO a
f = do
Bool
closed <- IORef Bool -> IO Bool
forall a. IORef a -> IO a
readIORef IORef Bool
closedRef
if Bool
closed then IO a
forall a. HasCallStack => IO a
throwECLOSED else UVFD -> IO a
f UVFD
fd
instance Input File where
readInput :: File -> Ptr Word8 -> Int -> IO Int
readInput File
f Ptr Word8
buf Int
bufSiz = HasCallStack => File -> Ptr Word8 -> Int -> Int64 -> IO Int
File -> Ptr Word8 -> Int -> Int64 -> IO Int
readFile File
f Ptr Word8
buf Int
bufSiz (-Int64
1)
readFile :: HasCallStack
=> File
-> Ptr Word8
-> Int
-> Int64
-> IO Int
readFile :: File -> Ptr Word8 -> Int -> Int64 -> IO Int
readFile File
uvf Ptr Word8
buf Int
bufSiz Int64
off =
File -> (UVFD -> IO Int) -> IO Int
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO Int) -> IO Int) -> (UVFD -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \ UVFD
fd -> IO Int -> IO Int
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwUVIfMinus (IO Int -> IO Int) -> IO Int -> IO Int
forall a b. (a -> b) -> a -> b
$ UVFD -> Ptr Word8 -> Int -> Int64 -> IO Int
hs_uv_fs_read UVFD
fd Ptr Word8
buf Int
bufSiz Int64
off
instance Output File where
writeOutput :: File -> Ptr Word8 -> Int -> IO ()
writeOutput File
f Ptr Word8
buf Int
bufSiz = HasCallStack => File -> Ptr Word8 -> Int -> Int64 -> IO ()
File -> Ptr Word8 -> Int -> Int64 -> IO ()
writeFile File
f Ptr Word8
buf Int
bufSiz (-Int64
1)
writeFile :: HasCallStack
=> File
-> Ptr Word8
-> Int
-> Int64
-> IO ()
writeFile :: File -> Ptr Word8 -> Int -> Int64 -> IO ()
writeFile File
uvf Ptr Word8
buf0 Int
bufSiz0 Int64
off0 =
File -> (UVFD -> IO ()) -> IO ()
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO ()) -> IO ()) -> (UVFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \UVFD
fd -> if Int64
off0 Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== -Int64
1 then UVFD -> Ptr Word8 -> Int -> IO ()
go UVFD
fd Ptr Word8
buf0 Int
bufSiz0
else UVFD -> Ptr Word8 -> Int -> Int64 -> IO ()
go' UVFD
fd Ptr Word8
buf0 Int
bufSiz0 Int64
off0
where
go :: UVFD -> Ptr Word8 -> Int -> IO ()
go UVFD
fd !Ptr Word8
buf !Int
bufSiz = do
Int
written <- IO Int -> IO Int
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwUVIfMinus (UVFD -> Ptr Word8 -> Int -> Int64 -> IO Int
hs_uv_fs_write UVFD
fd Ptr Word8
buf Int
bufSiz (-Int64
1))
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
written Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
bufSiz)
(UVFD -> Ptr Word8 -> Int -> IO ()
go UVFD
fd (Ptr Word8
buf Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
written) (Int
bufSizInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
written))
go' :: UVFD -> Ptr Word8 -> Int -> Int64 -> IO ()
go' UVFD
fd !Ptr Word8
buf !Int
bufSiz !Int64
off = do
Int
written <- IO Int -> IO Int
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwUVIfMinus (UVFD -> Ptr Word8 -> Int -> Int64 -> IO Int
hs_uv_fs_write UVFD
fd Ptr Word8
buf Int
bufSiz Int64
off)
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
written Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
bufSiz) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
UVFD -> Ptr Word8 -> Int -> Int64 -> IO ()
go' UVFD
fd (Ptr Word8
buf Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
written)
(Int
bufSizInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
written)
(Int64
offInt64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
written)
initFile :: HasCallStack
=> CBytes
-> FileFlag
-> FileMode
-> Resource File
initFile :: CBytes -> FileFlag -> FileMode -> Resource File
initFile CBytes
path FileFlag
flags FileMode
mode =
IO File -> (File -> IO ()) -> Resource File
forall a. IO a -> (a -> IO ()) -> Resource a
initResource
(do !UVFD
fd <- CBytes -> (CString -> IO UVFD) -> IO UVFD
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO UVFD) -> IO UVFD)
-> (CString -> IO UVFD) -> IO UVFD
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
IO UVFD -> IO UVFD
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwUVIfMinus (IO UVFD -> IO UVFD) -> IO UVFD -> IO UVFD
forall a b. (a -> b) -> a -> b
$ CString -> FileFlag -> FileMode -> IO UVFD
hs_uv_fs_open CString
p FileFlag
flags FileMode
mode
UVFD -> IORef Bool -> File
File UVFD
fd (IORef Bool -> File) -> IO (IORef Bool) -> IO File
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Bool -> IO (IORef Bool)
forall a. a -> IO (IORef a)
newIORef Bool
False)
(\ (File UVFD
fd IORef Bool
closedRef) -> do
Bool
closed <- IORef Bool -> IO Bool
forall a. IORef a -> IO a
readIORef IORef Bool
closedRef
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
closed (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (UVFD -> IO Int
hs_uv_fs_close UVFD
fd)
IORef Bool -> Bool -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef Bool
closedRef Bool
True)
mkdir :: HasCallStack => CBytes -> FileMode -> IO ()
mkdir :: CBytes -> FileMode -> IO ()
mkdir CBytes
path FileMode
mode = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ())
-> ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO Int) -> IO ()) -> (CString -> IO Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
CString -> FileMode -> IO Int
hs_uv_fs_mkdir CString
p FileMode
mode
unlink :: HasCallStack => CBytes -> IO ()
unlink :: CBytes -> IO ()
unlink CBytes
path = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path CString -> IO Int
hs_uv_fs_unlink)
mkdtemp :: HasCallStack => CBytes -> IO CBytes
mkdtemp :: CBytes -> IO CBytes
mkdtemp CBytes
path = do
let size :: Int
size = CBytes -> Int
CBytes.length CBytes
path
CBytes -> (CString -> IO CBytes) -> IO CBytes
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO CBytes) -> IO CBytes)
-> (CString -> IO CBytes) -> IO CBytes
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
HasCallStack => Int -> (CString -> IO Int) -> IO CBytes
Int -> (CString -> IO Int) -> IO CBytes
CBytes.create (Int
sizeInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
7) ((CString -> IO Int) -> IO CBytes)
-> (CString -> IO Int) -> IO CBytes
forall a b. (a -> b) -> a -> b
$ \ CString
p' -> do
IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (CString -> Int -> CString -> IO Int
hs_uv_fs_mkdtemp CString
p Int
size CString
p')
Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
sizeInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
6)
rmdir :: HasCallStack => CBytes -> IO ()
rmdir :: CBytes -> IO ()
rmdir CBytes
path = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path CString -> IO Int
hs_uv_fs_rmdir)
scandir :: HasCallStack => CBytes -> IO [(CBytes, DirEntType)]
scandir :: CBytes -> IO [(CBytes, DirEntType)]
scandir CBytes
path = do
IO (Ptr (Ptr UVDirEnt), Int)
-> ((Ptr (Ptr UVDirEnt), Int) -> IO ())
-> ((Ptr (Ptr UVDirEnt), Int) -> IO [(CBytes, DirEntType)])
-> IO [(CBytes, DirEntType)]
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket
(CBytes
-> (CString -> IO (Ptr (Ptr UVDirEnt), Int))
-> IO (Ptr (Ptr UVDirEnt), Int)
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO (Ptr (Ptr UVDirEnt), Int))
-> IO (Ptr (Ptr UVDirEnt), Int))
-> (CString -> IO (Ptr (Ptr UVDirEnt), Int))
-> IO (Ptr (Ptr UVDirEnt), Int)
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
(MBA# (Ptr (Ptr UVDirEnt)) -> IO Int)
-> IO (Ptr (Ptr UVDirEnt), Int)
forall a b.
Prim a =>
(MBA# (Ptr (Ptr UVDirEnt)) -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# (Ptr (Ptr UVDirEnt)) -> IO Int)
-> IO (Ptr (Ptr UVDirEnt), Int))
-> (MBA# (Ptr (Ptr UVDirEnt)) -> IO Int)
-> IO (Ptr (Ptr UVDirEnt), Int)
forall a b. (a -> b) -> a -> b
$ \ MBA# (Ptr (Ptr UVDirEnt))
dents ->
IO Int -> IO Int
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwUVIfMinus (CString -> MBA# (Ptr (Ptr UVDirEnt)) -> IO Int
hs_uv_fs_scandir CString
p MBA# (Ptr (Ptr UVDirEnt))
dents))
(\ (Ptr (Ptr UVDirEnt)
dents, Int
n) -> Ptr (Ptr UVDirEnt) -> Int -> IO ()
hs_uv_fs_scandir_cleanup Ptr (Ptr UVDirEnt)
dents Int
n)
(\ (Ptr (Ptr UVDirEnt)
dents, Int
n) -> [Int]
-> (Int -> IO (CBytes, DirEntType)) -> IO [(CBytes, DirEntType)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Int
0..Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1] ((Int -> IO (CBytes, DirEntType)) -> IO [(CBytes, DirEntType)])
-> (Int -> IO (CBytes, DirEntType)) -> IO [(CBytes, DirEntType)]
forall a b. (a -> b) -> a -> b
$ \ Int
i -> do
Ptr UVDirEnt
dent <- Ptr (Ptr UVDirEnt) -> Int -> IO (Ptr UVDirEnt)
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr (Ptr UVDirEnt)
dents Int
i
(CString
p, UVDirEntType
typ) <- Ptr UVDirEnt -> IO (CString, UVDirEntType)
peekUVDirEnt Ptr UVDirEnt
dent
let !typ' :: DirEntType
typ' = UVDirEntType -> DirEntType
fromUVDirEntType UVDirEntType
typ
!CBytes
p' <- CString -> IO CBytes
fromCString CString
p
(CBytes, DirEntType) -> IO (CBytes, DirEntType)
forall (m :: * -> *) a. Monad m => a -> m a
return (CBytes
p', DirEntType
typ'))
stat :: HasCallStack => CBytes -> IO UVStat
stat :: CBytes -> IO UVStat
stat CBytes
path = CBytes -> (CString -> IO UVStat) -> IO UVStat
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO UVStat) -> IO UVStat)
-> (CString -> IO UVStat) -> IO UVStat
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
Int -> (Ptr UVStat -> IO UVStat) -> IO UVStat
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
uvStatSize ((Ptr UVStat -> IO UVStat) -> IO UVStat)
-> (Ptr UVStat -> IO UVStat) -> IO UVStat
forall a b. (a -> b) -> a -> b
$ \ Ptr UVStat
s -> do
IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (CString -> Ptr UVStat -> IO Int
hs_uv_fs_stat CString
p Ptr UVStat
s)
Ptr UVStat -> IO UVStat
peekUVStat Ptr UVStat
s
lstat :: HasCallStack => CBytes -> IO UVStat
lstat :: CBytes -> IO UVStat
lstat CBytes
path = CBytes -> (CString -> IO UVStat) -> IO UVStat
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO UVStat) -> IO UVStat)
-> (CString -> IO UVStat) -> IO UVStat
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
Int -> (Ptr UVStat -> IO UVStat) -> IO UVStat
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
uvStatSize ((Ptr UVStat -> IO UVStat) -> IO UVStat)
-> (Ptr UVStat -> IO UVStat) -> IO UVStat
forall a b. (a -> b) -> a -> b
$ \ Ptr UVStat
s -> do
IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (CString -> Ptr UVStat -> IO Int
hs_uv_fs_lstat CString
p Ptr UVStat
s)
Ptr UVStat -> IO UVStat
peekUVStat Ptr UVStat
s
fstat :: HasCallStack => File -> IO UVStat
fstat :: File -> IO UVStat
fstat File
uvf = File -> (UVFD -> IO UVStat) -> IO UVStat
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO UVStat) -> IO UVStat)
-> (UVFD -> IO UVStat) -> IO UVStat
forall a b. (a -> b) -> a -> b
$ \ UVFD
fd ->
Int -> (Ptr UVStat -> IO UVStat) -> IO UVStat
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
uvStatSize ((Ptr UVStat -> IO UVStat) -> IO UVStat)
-> (Ptr UVStat -> IO UVStat) -> IO UVStat
forall a b. (a -> b) -> a -> b
$ \ Ptr UVStat
s -> do
IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (UVFD -> Ptr UVStat -> IO Int
hs_uv_fs_fstat UVFD
fd Ptr UVStat
s)
Ptr UVStat -> IO UVStat
peekUVStat Ptr UVStat
s
rename :: HasCallStack => CBytes -> CBytes -> IO ()
rename :: CBytes -> CBytes -> IO ()
rename CBytes
path CBytes
path' = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ())
-> ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO Int) -> IO ()) -> (CString -> IO Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path' (CString -> CString -> IO Int
hs_uv_fs_rename CString
p)
fsync :: HasCallStack => File -> IO ()
fsync :: File -> IO ()
fsync File
uvf = File -> (UVFD -> IO ()) -> IO ()
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO ()) -> IO ()) -> (UVFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ UVFD
fd -> IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ()) -> IO Int -> IO ()
forall a b. (a -> b) -> a -> b
$ UVFD -> IO Int
hs_uv_fs_fsync UVFD
fd
fdatasync :: HasCallStack => File -> IO ()
fdatasync :: File -> IO ()
fdatasync File
uvf = File -> (UVFD -> IO ()) -> IO ()
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO ()) -> IO ()) -> (UVFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ UVFD
fd -> IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ()) -> IO Int -> IO ()
forall a b. (a -> b) -> a -> b
$ UVFD -> IO Int
hs_uv_fs_fdatasync UVFD
fd
ftruncate :: HasCallStack => File -> Int64 -> IO ()
ftruncate :: File -> Int64 -> IO ()
ftruncate File
uvf Int64
off = File -> (UVFD -> IO ()) -> IO ()
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO ()) -> IO ()) -> (UVFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ UVFD
fd -> IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ()) -> IO Int -> IO ()
forall a b. (a -> b) -> a -> b
$ UVFD -> Int64 -> IO Int
hs_uv_fs_ftruncate UVFD
fd Int64
off
copyfile :: HasCallStack => CBytes -> CBytes -> UVCopyFileFlag -> IO ()
copyfile :: CBytes -> CBytes -> UVCopyFileFlag -> IO ()
copyfile CBytes
path CBytes
path' UVCopyFileFlag
flag = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ())
-> ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO Int) -> IO ()) -> (CString -> IO Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path' ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \ CString
p' -> CString -> CString -> UVCopyFileFlag -> IO Int
hs_uv_fs_copyfile CString
p CString
p' UVCopyFileFlag
flag
access :: HasCallStack => CBytes -> UVAccessMode -> IO AccessResult
access :: CBytes -> UVAccessMode -> IO AccessResult
access CBytes
path UVAccessMode
mode = do
CInt
r <- CBytes -> (CString -> IO CInt) -> IO CInt
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO CInt) -> IO CInt)
-> (CString -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ CString
p -> Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CInt) -> IO Int -> IO CInt
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CString -> UVAccessMode -> IO Int
hs_uv_fs_access CString
p UVAccessMode
mode
if | CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
0 -> AccessResult -> IO AccessResult
forall (m :: * -> *) a. Monad m => a -> m a
return AccessResult
AccessOK
| CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
UV_ENOENT -> AccessResult -> IO AccessResult
forall (m :: * -> *) a. Monad m => a -> m a
return AccessResult
NoExistence
| CInt
r CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
UV_EACCES -> AccessResult -> IO AccessResult
forall (m :: * -> *) a. Monad m => a -> m a
return AccessResult
NoPermission
| Bool
otherwise -> do
String
name <- CInt -> IO String
uvErrName CInt
r
String
desc <- CInt -> IO String
uvStdError CInt
r
CInt -> IOEInfo -> IO AccessResult
forall a. CInt -> IOEInfo -> IO a
throwUVError CInt
r (String -> String -> CallStack -> IOEInfo
IOEInfo String
name String
desc CallStack
HasCallStack => CallStack
callStack)
chmod :: HasCallStack => CBytes -> FileMode -> IO ()
chmod :: CBytes -> FileMode -> IO ()
chmod CBytes
path FileMode
mode = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ())
-> ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO Int) -> IO ()) -> (CString -> IO Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ CString
p -> CString -> FileMode -> IO Int
hs_uv_fs_chmod CString
p FileMode
mode
fchmod :: HasCallStack => File -> FileMode -> IO ()
fchmod :: File -> FileMode -> IO ()
fchmod File
uvf FileMode
mode = File -> (UVFD -> IO ()) -> IO ()
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO ()) -> IO ()) -> (UVFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ UVFD
fd -> IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ()) -> IO Int -> IO ()
forall a b. (a -> b) -> a -> b
$ UVFD -> FileMode -> IO Int
hs_uv_fs_fchmod UVFD
fd FileMode
mode
utime :: HasCallStack
=> CBytes
-> Double
-> Double
-> IO ()
utime :: CBytes -> Double -> Double -> IO ()
utime CBytes
path Double
atime Double
mtime = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ())
-> ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO Int) -> IO ()) -> (CString -> IO Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ CString
p -> CString -> Double -> Double -> IO Int
hs_uv_fs_utime CString
p Double
atime Double
mtime
futime :: HasCallStack => File -> Double -> Double -> IO ()
futime :: File -> Double -> Double -> IO ()
futime File
uvf Double
atime Double
mtime = File -> (UVFD -> IO ()) -> IO ()
forall a. HasCallStack => File -> (UVFD -> IO a) -> IO a
checkFileClosed File
uvf ((UVFD -> IO ()) -> IO ()) -> (UVFD -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ UVFD
fd ->
IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (UVFD -> Double -> Double -> IO Int
hs_uv_fs_futime UVFD
fd Double
atime Double
mtime)
link :: HasCallStack => CBytes -> CBytes -> IO ()
link :: CBytes -> CBytes -> IO ()
link CBytes
path CBytes
path' = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ())
-> ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO Int) -> IO ()) -> (CString -> IO Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path' ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ CString -> CString -> IO Int
hs_uv_fs_link CString
p
symlink :: HasCallStack => CBytes -> CBytes -> UVSymlinkFlag -> IO ()
symlink :: CBytes -> CBytes -> UVSymlinkFlag -> IO ()
symlink CBytes
path CBytes
path' UVSymlinkFlag
flag = IO Int -> IO ()
forall a. (HasCallStack, Integral a) => IO a -> IO ()
throwUVIfMinus_ (IO Int -> IO ())
-> ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO Int) -> IO ()) -> (CString -> IO Int) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
CBytes -> (CString -> IO Int) -> IO Int
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path' ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ \ CString
p' -> CString -> CString -> UVSymlinkFlag -> IO Int
hs_uv_fs_symlink CString
p CString
p' UVSymlinkFlag
flag
readlink :: HasCallStack => CBytes -> IO CBytes
readlink :: CBytes -> IO CBytes
readlink CBytes
path = do
IO (CString, Int)
-> ((CString, Int) -> IO ())
-> ((CString, Int) -> IO CBytes)
-> IO CBytes
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket
(CBytes -> (CString -> IO (CString, Int)) -> IO (CString, Int)
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO (CString, Int)) -> IO (CString, Int))
-> (CString -> IO (CString, Int)) -> IO (CString, Int)
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
(MBA# (Ptr (Ptr UVDirEnt)) -> IO Int) -> IO (CString, Int)
forall a b.
Prim a =>
(MBA# (Ptr (Ptr UVDirEnt)) -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# (Ptr (Ptr UVDirEnt)) -> IO Int) -> IO (CString, Int))
-> (MBA# (Ptr (Ptr UVDirEnt)) -> IO Int) -> IO (CString, Int)
forall a b. (a -> b) -> a -> b
$ \ MBA# (Ptr (Ptr UVDirEnt))
p' ->
IO Int -> IO Int
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwUVIfMinus (CString -> MBA# (Ptr (Ptr UVDirEnt)) -> IO Int
hs_uv_fs_readlink CString
p MBA# (Ptr (Ptr UVDirEnt))
p'))
(CString -> IO ()
hs_uv_fs_readlink_cleanup (CString -> IO ())
-> ((CString, Int) -> CString) -> (CString, Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CString, Int) -> CString
forall a b. (a, b) -> a
fst)
(CString -> IO CBytes
fromCString (CString -> IO CBytes)
-> ((CString, Int) -> CString) -> (CString, Int) -> IO CBytes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CString, Int) -> CString
forall a b. (a, b) -> a
fst)
realpath :: HasCallStack => CBytes -> IO CBytes
realpath :: CBytes -> IO CBytes
realpath CBytes
path = do
IO (CString, Int)
-> ((CString, Int) -> IO ())
-> ((CString, Int) -> IO CBytes)
-> IO CBytes
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket
(CBytes -> (CString -> IO (CString, Int)) -> IO (CString, Int)
forall a. CBytes -> (CString -> IO a) -> IO a
withCBytes CBytes
path ((CString -> IO (CString, Int)) -> IO (CString, Int))
-> (CString -> IO (CString, Int)) -> IO (CString, Int)
forall a b. (a -> b) -> a -> b
$ \ CString
p ->
(MBA# (Ptr (Ptr UVDirEnt)) -> IO Int) -> IO (CString, Int)
forall a b.
Prim a =>
(MBA# (Ptr (Ptr UVDirEnt)) -> IO b) -> IO (a, b)
allocPrimUnsafe ((MBA# (Ptr (Ptr UVDirEnt)) -> IO Int) -> IO (CString, Int))
-> (MBA# (Ptr (Ptr UVDirEnt)) -> IO Int) -> IO (CString, Int)
forall a b. (a -> b) -> a -> b
$ \ MBA# (Ptr (Ptr UVDirEnt))
p' ->
IO Int -> IO Int
forall a. (HasCallStack, Integral a) => IO a -> IO a
throwUVIfMinus (CString -> MBA# (Ptr (Ptr UVDirEnt)) -> IO Int
hs_uv_fs_realpath CString
p MBA# (Ptr (Ptr UVDirEnt))
p'))
(CString -> IO ()
hs_uv_fs_readlink_cleanup (CString -> IO ())
-> ((CString, Int) -> CString) -> (CString, Int) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CString, Int) -> CString
forall a b. (a, b) -> a
fst)
(CString -> IO CBytes
fromCString (CString -> IO CBytes)
-> ((CString, Int) -> CString) -> (CString, Int) -> IO CBytes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CString, Int) -> CString
forall a b. (a, b) -> a
fst)