LIB: cleanup
This commit is contained in:
parent
5b1c595703
commit
4b68bf759b
@ -225,11 +225,6 @@ data FileInfo = FileInfo {
|
|||||||
} deriving (Show, Eq, Ord)
|
} deriving (Show, Eq, Ord)
|
||||||
|
|
||||||
|
|
||||||
type UserIO a t = Path Abs -> IO a
|
|
||||||
|
|
||||||
type Builder a t = UserIO a t -> Path Abs -> IO [File a]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
------------------------------------
|
------------------------------------
|
||||||
@ -428,7 +423,7 @@ readWith ff p = do
|
|||||||
bd = P.dirname p
|
bd = P.dirname p
|
||||||
p' = P.toFilePath p
|
p' = P.toFilePath p
|
||||||
bd' <- P.canonicalizePath bd -- TODO: this will cause a dialog to pop up on error
|
bd' <- P.canonicalizePath bd -- TODO: this will cause a dialog to pop up on error
|
||||||
handleDT' bd' fn $ do
|
handleDT bd' fn $ do
|
||||||
fs <- PF.getSymbolicLinkStatus p'
|
fs <- PF.getSymbolicLinkStatus p'
|
||||||
fv <- ff p
|
fv <- ff p
|
||||||
file <- constructFile fs fv bd' fn
|
file <- constructFile fs fv bd' fn
|
||||||
@ -440,7 +435,7 @@ readWith ff p = do
|
|||||||
-- AnchoredFile
|
-- AnchoredFile
|
||||||
let fp = bd' P.</> fn'
|
let fp = bd' P.</> fn'
|
||||||
x <- PF.readSymbolicLink (P.fromAbs fp)
|
x <- PF.readSymbolicLink (P.fromAbs fp)
|
||||||
resolvedSyml <- handleDT' bd' fn' $ do
|
resolvedSyml <- handleDT bd' fn' $ do
|
||||||
-- watch out, we call </> from 'filepath' here, but it is safe
|
-- watch out, we call </> from 'filepath' here, but it is safe
|
||||||
-- TODO: could it happen that too many '..' lead
|
-- TODO: could it happen that too many '..' lead
|
||||||
-- to something like '/' after normalization?
|
-- to something like '/' after normalization?
|
||||||
@ -592,29 +587,6 @@ isSocketC _ = False
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
---- PATH CONVERSIONS ----
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- |Check whether the given file is a hidden file.
|
|
||||||
hiddenFile :: Path Fn -> Bool
|
|
||||||
hiddenFile (Path ".") = False
|
|
||||||
hiddenFile (Path "..") = False
|
|
||||||
hiddenFile (Path fn) = "." `isPrefixOf` fn
|
|
||||||
|
|
||||||
|
|
||||||
-- |Like `normalise` from System.FilePath but removes occurences of '..'.
|
|
||||||
-- Note that this sort of misbehaves if the path contains symlink
|
|
||||||
-- components.
|
|
||||||
normalize :: FilePath -> FilePath
|
|
||||||
normalize fp =
|
|
||||||
joinPath $ foldl' ff [] (splitDirectories . normalise $ fp)
|
|
||||||
where
|
|
||||||
ff ["/"] ".." = ["/"]
|
|
||||||
ff x ".." = initDef [] x
|
|
||||||
ff x y = x ++ [y]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
---- IO HELPERS: ----
|
---- IO HELPERS: ----
|
||||||
|
|
||||||
@ -693,35 +665,18 @@ getFileInfo fp = do
|
|||||||
(PF.statusChangeTimeHiRes fs)
|
(PF.statusChangeTimeHiRes fs)
|
||||||
|
|
||||||
|
|
||||||
-- |Gets the free variable. Returns Nothing if the constructor is of `Failed`.
|
|
||||||
getFreeVar :: File a -> Maybe a
|
|
||||||
getFreeVar (Dir _ d) = Just d
|
|
||||||
getFreeVar (RegFile _ d) = Just d
|
|
||||||
getFreeVar (SymLink _ d _ _) = Just d
|
|
||||||
getFreeVar (BlockDev _ d) = Just d
|
|
||||||
getFreeVar (CharDev _ d) = Just d
|
|
||||||
getFreeVar (NamedPipe _ d) = Just d
|
|
||||||
getFreeVar (Socket _ d) = Just d
|
|
||||||
getFreeVar _ = Nothing
|
|
||||||
|
|
||||||
|
|
||||||
---- FAILURE HELPERS: ----
|
---- FAILURE HELPERS: ----
|
||||||
|
|
||||||
|
|
||||||
-- handles an IO exception by returning a Failed constructor filled with that
|
-- Handles an IO exception by returning a Failed constructor filled with that
|
||||||
-- exception:
|
-- exception.
|
||||||
handleDT :: Path Fn -> IO (File a) -> IO (File a)
|
|
||||||
handleDT n = handle (return . Failed n)
|
|
||||||
|
|
||||||
|
|
||||||
-- handles an IO exception by returning a Failed constructor filled with that
|
|
||||||
-- exception:
|
|
||||||
-- TODO: only handle IO exceptions
|
-- TODO: only handle IO exceptions
|
||||||
handleDT' :: Path Abs
|
handleDT :: Path Abs
|
||||||
-> Path Fn
|
-> Path Fn
|
||||||
-> IO (AnchoredFile a)
|
-> IO (AnchoredFile a)
|
||||||
-> IO (AnchoredFile a)
|
-> IO (AnchoredFile a)
|
||||||
handleDT' bp n
|
handleDT bp n
|
||||||
= handle (\e -> return $ bp :/ Failed n e)
|
= handle (\e -> return $ bp :/ Failed n e)
|
||||||
|
|
||||||
|
|
||||||
@ -742,7 +697,7 @@ removeNonexistent = filter isOkConstructor
|
|||||||
|
|
||||||
|
|
||||||
-- |Checks if a symlink is broken by examining the constructor of the
|
-- |Checks if a symlink is broken by examining the constructor of the
|
||||||
-- symlink destination. This also follows the symlink chain.
|
-- symlink destination.
|
||||||
--
|
--
|
||||||
-- When called on a non-symlink, returns False.
|
-- When called on a non-symlink, returns False.
|
||||||
isBrokenSymlink :: File FileInfo -> Bool
|
isBrokenSymlink :: File FileInfo -> Bool
|
||||||
@ -753,9 +708,11 @@ isBrokenSymlink _ = False
|
|||||||
---- OTHER: ----
|
---- OTHER: ----
|
||||||
|
|
||||||
|
|
||||||
|
-- |Check whether the given file is a hidden file.
|
||||||
fullPath :: AnchoredFile a -> Path Abs
|
hiddenFile :: Path Fn -> Bool
|
||||||
fullPath (bp :/ f) = bp P.</> name f
|
hiddenFile (Path ".") = False
|
||||||
|
hiddenFile (Path "..") = False
|
||||||
|
hiddenFile (Path fn) = "." `isPrefixOf` fn
|
||||||
|
|
||||||
|
|
||||||
-- |Apply a function on the free variable. If there is no free variable
|
-- |Apply a function on the free variable. If there is no free variable
|
||||||
@ -764,12 +721,33 @@ fromFreeVar :: (Default d) => (a -> d) -> File a -> d
|
|||||||
fromFreeVar f df = maybeD f $ getFreeVar df
|
fromFreeVar f df = maybeD f $ getFreeVar df
|
||||||
|
|
||||||
|
|
||||||
|
-- |Gets the free variable. Returns Nothing if the constructor is of `Failed`.
|
||||||
|
getFreeVar :: File a -> Maybe a
|
||||||
|
getFreeVar (Dir _ d) = Just d
|
||||||
|
getFreeVar (RegFile _ d) = Just d
|
||||||
|
getFreeVar (SymLink _ d _ _) = Just d
|
||||||
|
getFreeVar (BlockDev _ d) = Just d
|
||||||
|
getFreeVar (CharDev _ d) = Just d
|
||||||
|
getFreeVar (NamedPipe _ d) = Just d
|
||||||
|
getFreeVar (Socket _ d) = Just d
|
||||||
|
getFreeVar _ = Nothing
|
||||||
|
|
||||||
|
|
||||||
|
-- |Get the full path of the file.
|
||||||
|
fullPath :: AnchoredFile a -> Path Abs
|
||||||
|
fullPath (bp :/ f) = bp P.</> name f
|
||||||
|
|
||||||
|
|
||||||
|
-- |Get the full path of the file, converted to a `FilePath`.
|
||||||
|
fullPathS :: AnchoredFile a -> FilePath
|
||||||
|
fullPathS = P.fromAbs . fullPath
|
||||||
|
|
||||||
|
|
||||||
-- |Pack the modification time into a string.
|
-- |Pack the modification time into a string.
|
||||||
packModTime :: File FileInfo
|
packModTime :: File FileInfo
|
||||||
-> String
|
-> String
|
||||||
packModTime = fromFreeVar
|
packModTime =
|
||||||
|
fromFreeVar
|
||||||
$ show . posixSecondsToUTCTime . realToFrac . modificationTime
|
$ show . posixSecondsToUTCTime . realToFrac . modificationTime
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user