From 4e75a84439eac7227b00ddc2580fd157bc32bb38 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sun, 3 Apr 2016 03:57:11 +0200 Subject: [PATCH] LIB: remove more occurences of FilePath --- src/HSFM/FileSystem/FileOperations.hs | 17 ++++++++++------- src/HSFM/FileSystem/FileType.hs | 26 +++++++++++++------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/HSFM/FileSystem/FileOperations.hs b/src/HSFM/FileSystem/FileOperations.hs index 93150f2..6b2b11f 100644 --- a/src/HSFM/FileSystem/FileOperations.hs +++ b/src/HSFM/FileSystem/FileOperations.hs @@ -48,6 +48,7 @@ import Foreign.C.Error import HPath ( Path + , Abs , Fn ) import qualified HPath as P @@ -234,14 +235,16 @@ recreateSymlink _ _ _ = throw $ InvalidOperation "wrong input type" -- |TODO: handle EAGAIN exception for non-blocking IO -- |Low-level function to copy a given file to the given path. The fileMode -- is preserved. The file is always overwritten if accessible. -copyFile' :: FilePath -> FilePath -> IO () +copyFile' :: Path Abs -> Path Abs -> IO () copyFile' from to = do - fromFstatus <- getSymbolicLinkStatus from - fromContent <- BS.readFile from - fd <- System.Posix.IO.createFile to + let from' = P.fromAbs from + to' = P.fromAbs to + fromFstatus <- getSymbolicLinkStatus from' + fromContent <- BS.readFile from' + fd <- System.Posix.IO.createFile to' (System.Posix.Files.fileMode fromFstatus) closeFd fd - BS.writeFile to fromContent + BS.writeFile to' fromContent -- |Copies the given file to the given file destination, overwriting it. @@ -257,7 +260,7 @@ overwriteFile from@(_ :/ RegFile {}) let from' = fullPath from to' = fullPath to throwSameFile from' to' - copyFile' (P.fromAbs from') (P.fromAbs to') + copyFile' from' to' overwriteFile _ _ = throw $ InvalidOperation "wrong input type" @@ -277,7 +280,7 @@ copyFileToDir cm from@(_ :/ RegFile fn _) case cm of Strict -> throwFileDoesExist to' _ -> return () - copyFile' (P.fromAbs from') (P.fromAbs to') + copyFile' from' to' copyFileToDir _ _ _ = throw $ InvalidOperation "wrong input type" diff --git a/src/HSFM/FileSystem/FileType.hs b/src/HSFM/FileSystem/FileType.hs index e99fafd..d4bf82f 100644 --- a/src/HSFM/FileSystem/FileType.hs +++ b/src/HSFM/FileSystem/FileType.hs @@ -45,10 +45,6 @@ import Data.List ( isPrefixOf ) -import Data.Maybe - ( - catMaybes - ) import Data.Time.Clock.POSIX ( POSIXTime @@ -581,22 +577,26 @@ getContents (ADirOrSym af) = readDirectoryContents (fullPath af) getContents _ = return [] -getDirsFiles' :: (FilePath -> [FilePath] -> [FilePath]) +getDirsFiles' :: (Path Fn -> [Path Fn] -> [Path Fn]) -> Path Abs -> IO [Path Fn] getDirsFiles' filterf fp = do dirstream <- PFD.openDirStream . P.toFilePath $ fp - let mdirs :: [FilePath] -> IO [FilePath] + let mdirs :: [Path Fn] -> IO [Path Fn] mdirs dirs = do -- make sure we close the directory stream in case of errors + -- TODO: more explicit error handling? + -- both the parsing and readin the stream can fail! dir <- onException (PFD.readDirStream dirstream) (PFD.closeDirStream dirstream) - if dir == "" - then return dirs - else mdirs (dir `filterf` dirs) + case dir of + "" -> return dirs + _ -> do + pdir <- P.parseFn dir + mdirs $ pdir `filterf` dirs dirs <- mdirs [] PFD.closeDirStream dirstream - return $ catMaybes (fmap P.parseFn dirs) + return dirs -- |Get all files of a given directory and return them as a List. @@ -611,9 +611,9 @@ getDirsFiles :: Path Abs -> IO [Path Fn] getDirsFiles = getDirsFiles' insert where insert dir dirs = case dir of - "." -> dirs - ".." -> dirs - _ -> dir : dirs + (Path ".") -> dirs + (Path "..") -> dirs + _ -> dir : dirs -- |Gets all file information.