system-fileio 0.1.1 → 0.2
raw patch · 3 files changed
+84/−66 lines, 3 files
Files
- System/Directory.hs +1/−65
- System/File.hs +82/−0
- system-fileio.cabal +1/−1
System/Directory.hs view
@@ -16,12 +16,10 @@ module System.Directory ( - -- * File and directory attributes+ -- * Generic operations isFile , isDirectory- , fileSize , rename- , modified , canonicalizePath , listDirectory @@ -61,17 +59,10 @@ #ifdef CABAL_OS_WINDOWS -import Data.Bits ((.|.))-import Data.Time ( UTCTime(..)- , fromGregorian- , secondsToDiffTime- , picosecondsToDiffTime) import qualified System.Win32 as Win32 #else -import Data.Time (UTCTime)-import Data.Time.Clock.POSIX (posixSecondsToUTCTime) import qualified System.Posix as Posix import qualified System.Posix.Error as Posix @@ -94,47 +85,6 @@ isDirectory :: FilePath -> IO Bool isDirectory path = SD.doesDirectoryExist (encode path) --- | Get when the object at a given path was last modified.-modified :: FilePath -> IO UTCTime-modified path = do-#ifdef CABAL_OS_WINDOWS- info <- withHANDLE path Win32.getFileInformationByHandle- let ftime = Win32.bhfiLastWriteTime info- stime <- Win32.fileTimeToSystemTime ftime- - let date = fromGregorian- (fromIntegral (Win32.wYear stime))- (fromIntegral (Win32.wMonth stime))- (fromIntegral (Win32.wDay stime))- - let seconds = secondsToDiffTime $- (toInteger (Win32.wHour stime) * 3600) +- (toInteger (Win32.wMinute stime) * 60) +- (toInteger (Win32.wSecond stime))- - let msecs = picosecondsToDiffTime $- (toInteger (Win32.wMilliseconds stime) * 1000000000)- - return (UTCTime date (seconds + msecs))-#else- stat <- Posix.getFileStatus (encode path)- let mtime = Posix.modificationTime stat- return (posixSecondsToUTCTime (realToFrac mtime))-#endif---- | Get the size of an object at a given path. For special objects like--- links or directories, the size is filesystem‐ and--- platform‐dependent.-fileSize :: FilePath -> IO Integer-fileSize path = do-#ifdef CABAL_OS_WINDOWS- info <- withHANDLE path Win32.getFileInformationByHandle- return (toInteger (Win32.bhfiSize info))-#else- stat <- Posix.getFileStatus (encode path)- return (toInteger (Posix.fileSize stat))-#endif- -- | Rename a filesystem object. Some operating systems have restrictions -- on what objects can be renamed; see linked documentation for details. --@@ -352,17 +302,3 @@ return $ case label of Just text -> append dir (R.fromText currentOS text) Nothing -> dir--#ifdef CABAL_OS_WINDOWS-withHANDLE :: FilePath -> (Win32.HANDLE -> IO a) -> IO a-withHANDLE path = Exc.bracket open close where- open = Win32.createFile- (encode path)- Win32.gENERIC_READ- (Win32.fILE_SHARE_READ .|. Win32.fILE_SHARE_WRITE)- Nothing- Win32.oPEN_EXISTING- 0- Nothing- close = Win32.closeHandle-#endif
System/File.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE PackageImports #-} -- |@@ -18,6 +19,10 @@ -- * File operations , copyFile + -- * File information+ , getModified+ , getSize+ -- * Binary files , openFile , withFile@@ -42,6 +47,24 @@ import System.FilePath (FilePath) import System.FileIO.Internal (encode) +#ifdef CABAL_OS_WINDOWS++import qualified Control.Exception as Exc+import Data.Bits ((.|.))+import Data.Time ( UTCTime(..)+ , fromGregorian+ , secondsToDiffTime+ , picosecondsToDiffTime)+import qualified System.Win32 as Win32++#else++import Data.Time (UTCTime)+import Data.Time.Clock.POSIX (posixSecondsToUTCTime)+import qualified System.Posix as Posix++#endif+ import qualified "directory" System.Directory as SD -- | Copy a file to a new entry in the filesystem. If a file already exists@@ -55,6 +78,51 @@ -> IO () copyFile old new = SD.copyFile (encode old) (encode new) +-- | Get when the object at a given path was last modified.+--+-- Since: 0.2+getModified :: FilePath -> IO UTCTime+getModified path = do+#ifdef CABAL_OS_WINDOWS+ info <- withHANDLE path Win32.getFileInformationByHandle+ let ftime = Win32.bhfiLastWriteTime info+ stime <- Win32.fileTimeToSystemTime ftime+ + let date = fromGregorian+ (fromIntegral (Win32.wYear stime))+ (fromIntegral (Win32.wMonth stime))+ (fromIntegral (Win32.wDay stime))+ + let seconds = secondsToDiffTime $+ (toInteger (Win32.wHour stime) * 3600) ++ (toInteger (Win32.wMinute stime) * 60) ++ (toInteger (Win32.wSecond stime))+ + let msecs = picosecondsToDiffTime $+ (toInteger (Win32.wMilliseconds stime) * 1000000000)+ + return (UTCTime date (seconds + msecs))+#else+ stat <- Posix.getFileStatus (encode path)+ let mtime = Posix.modificationTime stat+ return (posixSecondsToUTCTime (realToFrac mtime))+#endif++-- | Get the size of an object at a given path. For special objects like+-- links or directories, the size is filesystem‐ and+-- platform‐dependent.+--+-- Since: 0.2+getSize :: FilePath -> IO Integer+getSize path = do+#ifdef CABAL_OS_WINDOWS+ info <- withHANDLE path Win32.getFileInformationByHandle+ return (toInteger (Win32.bhfiSize info))+#else+ stat <- Posix.getFileStatus (encode path)+ return (toInteger (Posix.fileSize stat))+#endif+ -- | Open a file in binary mode, and return an open 'Handle'. The 'Handle' -- should be 'IO.hClose'd when it is no longer needed. --@@ -130,3 +198,17 @@ -- See: 'T.appendFile' appendTextFile :: FilePath -> T.Text -> IO () appendTextFile path = T.appendFile (encode path)++#ifdef CABAL_OS_WINDOWS+withHANDLE :: FilePath -> (Win32.HANDLE -> IO a) -> IO a+withHANDLE path = Exc.bracket open close where+ open = Win32.createFile+ (encode path)+ Win32.gENERIC_READ+ (Win32.fILE_SHARE_READ .|. Win32.fILE_SHARE_WRITE)+ Nothing+ Win32.oPEN_EXISTING+ 0+ Nothing+ close = Win32.closeHandle+#endif
system-fileio.cabal view
@@ -1,5 +1,5 @@ name: system-fileio-version: 0.1.1+version: 0.2 synopsis: High-level filesystem interaction license: MIT license-file: license.txt