more OsPath conversion (602/749)
Sponsored-by: Brock Spratlen
This commit is contained in:
parent
2d1db7986c
commit
a5d48edd94
25 changed files with 227 additions and 187 deletions
|
@ -123,13 +123,14 @@ instance ToFilePath FuzzDir where
|
|||
toFilePath (FuzzDir d) = d
|
||||
|
||||
isFuzzFile :: FilePath -> Bool
|
||||
isFuzzFile f = "fuzzfile_" `isPrefixOf` takeFileName f
|
||||
isFuzzFile f = "fuzzfile_" `isPrefixOf` fromOsPath (takeFileName (toOsPath f))
|
||||
|
||||
isFuzzDir :: FilePath -> Bool
|
||||
isFuzzDir d = "fuzzdir_" `isPrefixOf` d
|
||||
|
||||
mkFuzzFile :: FilePath -> [FuzzDir] -> FuzzFile
|
||||
mkFuzzFile file dirs = FuzzFile $ joinPath (map toFilePath dirs) </> ("fuzzfile_" ++ file)
|
||||
mkFuzzFile file dirs = FuzzFile $ fromOsPath $
|
||||
joinPath (map (toOsPath . toFilePath) dirs) </> toOsPath ("fuzzfile_" ++ file)
|
||||
|
||||
mkFuzzDir :: Int -> FuzzDir
|
||||
mkFuzzDir n = FuzzDir $ "fuzzdir_" ++ show n
|
||||
|
@ -175,15 +176,15 @@ instance Arbitrary FuzzAction where
|
|||
|
||||
runFuzzAction :: FuzzAction -> Annex ()
|
||||
runFuzzAction (FuzzAdd (FuzzFile f)) = do
|
||||
createWorkTreeDirectory (parentDir (toRawFilePath f))
|
||||
createWorkTreeDirectory (parentDir (toOsPath f))
|
||||
n <- liftIO (getStdRandom random :: IO Int)
|
||||
liftIO $ writeFile f $ show n ++ "\n"
|
||||
runFuzzAction (FuzzDelete (FuzzFile f)) = liftIO $
|
||||
removeWhenExistsWith R.removeLink (toRawFilePath f)
|
||||
removeWhenExistsWith removeFile (toOsPath f)
|
||||
runFuzzAction (FuzzMove (FuzzFile src) (FuzzFile dest)) = liftIO $
|
||||
R.rename (toRawFilePath src) (toRawFilePath dest)
|
||||
runFuzzAction (FuzzDeleteDir (FuzzDir d)) = liftIO $
|
||||
removeDirectoryRecursive d
|
||||
removeDirectoryRecursive (toOsPath d)
|
||||
runFuzzAction (FuzzMoveDir (FuzzDir src) (FuzzDir dest)) = liftIO $
|
||||
R.rename (toRawFilePath src) (toRawFilePath dest)
|
||||
runFuzzAction (FuzzPause d) = randomDelay d
|
||||
|
@ -210,7 +211,7 @@ genFuzzAction = do
|
|||
case md of
|
||||
Nothing -> genFuzzAction
|
||||
Just d -> do
|
||||
newd <- liftIO $ newDir (parentDir $ toRawFilePath $ toFilePath d)
|
||||
newd <- liftIO $ newDir (parentDir $ toOsPath $ toFilePath d)
|
||||
maybe genFuzzAction (return . FuzzMoveDir d) newd
|
||||
FuzzDeleteDir _ -> do
|
||||
d <- liftIO existingDir
|
||||
|
@ -221,7 +222,8 @@ existingFile :: Int -> FilePath -> IO (Maybe FuzzFile)
|
|||
existingFile 0 _ = return Nothing
|
||||
existingFile n top = do
|
||||
dir <- existingDirIncludingTop
|
||||
contents <- catchDefaultIO [] (getDirectoryContents dir)
|
||||
contents <- map fromOsPath
|
||||
<$> catchDefaultIO [] (getDirectoryContents (toOsPath dir))
|
||||
let files = filter isFuzzFile contents
|
||||
if null files
|
||||
then do
|
||||
|
@ -230,19 +232,21 @@ existingFile n top = do
|
|||
then return Nothing
|
||||
else do
|
||||
i <- getStdRandom $ randomR (0, length dirs - 1)
|
||||
existingFile (n - 1) (top </> dirs !! i)
|
||||
existingFile (n - 1) (fromOsPath (toOsPath top </> toOsPath (dirs !! i)))
|
||||
else do
|
||||
i <- getStdRandom $ randomR (0, length files - 1)
|
||||
return $ Just $ FuzzFile $ top </> dir </> files !! i
|
||||
return $ Just $ FuzzFile $ fromOsPath $
|
||||
toOsPath top </> toOsPath dir </> toOsPath (files !! i)
|
||||
|
||||
existingDirIncludingTop :: IO FilePath
|
||||
existingDirIncludingTop = do
|
||||
dirs <- filter isFuzzDir <$> getDirectoryContents "."
|
||||
dirs <- filter (isFuzzDir . fromOsPath)
|
||||
<$> getDirectoryContents (literalOsPath ".")
|
||||
if null dirs
|
||||
then return "."
|
||||
else do
|
||||
n <- getStdRandom $ randomR (0, length dirs)
|
||||
return $ ("." : dirs) !! n
|
||||
return $ fromOsPath $ (literalOsPath "." : dirs) !! n
|
||||
|
||||
existingDir :: IO (Maybe FuzzDir)
|
||||
existingDir = do
|
||||
|
@ -257,21 +261,21 @@ newFile = go (100 :: Int)
|
|||
go 0 = return Nothing
|
||||
go n = do
|
||||
f <- genFuzzFile
|
||||
ifM (doesnotexist (toFilePath f))
|
||||
ifM (doesnotexist (toOsPath (toFilePath f)))
|
||||
( return $ Just f
|
||||
, go (n - 1)
|
||||
)
|
||||
|
||||
newDir :: RawFilePath -> IO (Maybe FuzzDir)
|
||||
newDir :: OsPath -> IO (Maybe FuzzDir)
|
||||
newDir parent = go (100 :: Int)
|
||||
where
|
||||
go 0 = return Nothing
|
||||
go n = do
|
||||
(FuzzDir d) <- genFuzzDir
|
||||
ifM (doesnotexist (fromRawFilePath parent </> d))
|
||||
ifM (doesnotexist (parent </> toOsPath d))
|
||||
( return $ Just $ FuzzDir d
|
||||
, go (n - 1)
|
||||
)
|
||||
|
||||
doesnotexist :: FilePath -> IO Bool
|
||||
doesnotexist f = isNothing <$> catchMaybeIO (R.getSymbolicLinkStatus (toRawFilePath f))
|
||||
doesnotexist :: OsPath -> IO Bool
|
||||
doesnotexist f = isNothing <$> catchMaybeIO (R.getSymbolicLinkStatus (fromOsPath f))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue