Follow links a bit more usefully.
--HG-- extra : convert_revision : 71391f687787d19d094c467eeded471042f4311a
This commit is contained in:
parent
c5ccd0a6ab
commit
1f5a65b56e
1 changed files with 23 additions and 8 deletions
|
@ -38,7 +38,8 @@ module System.FilePath.Find (
|
|||
, modificationTime
|
||||
, statusChangeTime
|
||||
|
||||
, linkTarget
|
||||
, readLink
|
||||
, followStatus
|
||||
|
||||
, (~~?)
|
||||
, (/~?)
|
||||
|
@ -60,7 +61,8 @@ import Control.Monad.State (State(..), evalState)
|
|||
import Data.Bits (Bits, (.&.))
|
||||
import Data.List (sort)
|
||||
import System.Directory (getDirectoryContents)
|
||||
import System.FilePath ((</>), takeDirectory, takeExtension, takeFileName)
|
||||
import System.FilePath ((</>), replaceFileName, takeDirectory, takeExtension,
|
||||
takeFileName)
|
||||
import System.FilePath.GlobPattern (GlobPattern, (~~), (/~))
|
||||
import System.IO (hPutStrLn, stderr)
|
||||
import System.IO.Unsafe (unsafeInterleaveIO, unsafePerformIO)
|
||||
|
@ -73,7 +75,11 @@ data FileInfo = FileInfo
|
|||
infoPath :: FilePath
|
||||
, infoDepth :: Int
|
||||
, infoStatus :: F.FileStatus
|
||||
}
|
||||
} deriving (Eq)
|
||||
|
||||
instance Eq F.FileStatus where
|
||||
a == b = F.deviceID a == F.deviceID b &&
|
||||
F.fileID a == F.fileID b
|
||||
|
||||
mkFI :: FilePath -> Int -> F.FileStatus -> FileInfo
|
||||
|
||||
|
@ -196,15 +202,24 @@ fileName = takeFileName `liftM` filePath
|
|||
directory :: FindClause FilePath
|
||||
directory = takeDirectory `liftM` filePath
|
||||
|
||||
linkTarget :: FindClause (Maybe FilePath)
|
||||
linkTarget = do
|
||||
withLink :: (FilePath -> IO a) -> FindClause (Maybe a)
|
||||
|
||||
withLink f = do
|
||||
path <- filePath
|
||||
st <- fileStatus
|
||||
return $ if F.isSymbolicLink st
|
||||
then unsafePerformIO $ E.handle (const (return Nothing))
|
||||
(Just `liftM` F.readSymbolicLink path)
|
||||
then unsafePerformIO $ E.handle (const (return Nothing)) $
|
||||
Just `liftM` f path
|
||||
else Nothing
|
||||
|
||||
readLink :: FindClause (Maybe FilePath)
|
||||
|
||||
readLink = withLink F.readSymbolicLink
|
||||
|
||||
followStatus :: FindClause (Maybe F.FileStatus)
|
||||
|
||||
followStatus = withLink F.getFileStatus
|
||||
|
||||
data FileType = BlockDevice
|
||||
| CharacterDevice
|
||||
| NamedPipe
|
||||
|
|
Loading…
Add table
Reference in a new issue