1 {-# OPTIONS_GHC -cpp #-}
2 module System.Posix.Files where
4 import Foreign.Marshal.Alloc ( allocaBytes )
5 import Foreign.C.Error ( throwErrnoIfMinus1Retry )
6 import Foreign.C.String ( withCString )
7 import Foreign.C.Types ( CTime, CInt )
8 import Foreign.Ptr ( Ptr )
10 import System.Posix.Internals
11 ( FDType, CStat, c_fstat, lstat,
12 sizeof_stat, statGetType,
13 st_mode, st_size, st_mtime,
14 s_isreg, s_isdir, s_isfifo, )
15 import System.Posix.Types ( Fd(..), CMode, EpochTime, FileMode )
17 import Data.Bits ( (.|.) )
20 import Data.Int ( Int64 )
22 import System.Posix.Types ( FileOffset )
26 type FileOffset = Int64
29 data FileStatus = FileStatus {
33 fst_size :: FileOffset
36 getFdStatus :: Fd -> IO FileStatus
37 getFdStatus (Fd fd) = do
40 do_stat :: (Ptr CStat -> IO CInt) -> IO FileStatus
41 do_stat stat_func = do
42 allocaBytes sizeof_stat $ \p -> do
43 throwErrnoIfMinus1Retry "do_stat" $
49 return (FileStatus tp mode mtime (fromIntegral size))
51 isNamedPipe :: FileStatus -> Bool
52 isNamedPipe = s_isfifo . fst_mode
54 isDirectory :: FileStatus -> Bool
55 isDirectory = s_isdir . fst_mode
57 isRegularFile :: FileStatus -> Bool
58 isRegularFile = s_isreg . fst_mode
60 isSymbolicLink :: FileStatus -> Bool
61 isSymbolicLink = const False
63 linkCount :: FileStatus -> Int
66 modificationTime :: FileStatus -> EpochTime
67 modificationTime = fst_mtime
69 fileSize :: FileStatus -> FileOffset
75 getFileStatus :: FilePath -> IO ()
76 getFileStatus _ = return ()
78 setFileMode :: FilePath -> () -> IO ()
79 setFileMode _ _ = return ()
82 stdFileMode :: FileMode
83 stdFileMode = (#const S_IRUSR) .|. (#const S_IWUSR)
87 getSymbolicLinkStatus :: FilePath -> IO FileStatus
88 getSymbolicLinkStatus fp =
89 do_stat (\p -> (fp `withCString` (`lstat` p)))
91 -- Dummy implementation of createLink.
92 createLink :: FilePath -> FilePath -> IO ()
93 createLink _ _ = fail "Dummy create link error should be caught."