| 1 |
module Benchmark.FileSystem |
| 2 |
( |
| 3 |
copy, |
| 4 |
removeTreeIfExists, |
| 5 |
exists, |
| 6 |
getTemporaryDirectory, |
| 7 |
module Filesystem, |
| 8 |
module Filesystem.Path.CurrentOS |
| 9 |
) |
| 10 |
where |
| 11 |
|
| 12 |
import Benchmark.Prelude hiding (stripPrefix, last) |
| 13 |
import Filesystem.Path.CurrentOS |
| 14 |
import Filesystem |
| 15 |
import qualified System.Directory as Directory |
| 16 |
import Debug.Trace |
| 17 |
import qualified Data.List as List |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
removeTreeIfExists :: FilePath -> IO () |
| 22 |
removeTreeIfExists path = removeTree path `catch` \e -> case e of |
| 23 |
_ | isDoesNotExistError e -> return () |
| 24 |
| otherwise -> throwIO e |
| 25 |
|
| 26 |
exists :: FilePath -> IO Bool |
| 27 |
exists path = do |
| 28 |
isDir <- isDirectory path |
| 29 |
isFile <- isFile path |
| 30 |
return $ isDir || isFile |
| 31 |
|
| 32 |
getTemporaryDirectory :: IO FilePath |
| 33 |
getTemporaryDirectory = |
| 34 |
Directory.getTemporaryDirectory >>= return . decodeString |
| 35 |
|
| 36 |
copy :: FilePath -> FilePath -> IO () |
| 37 |
copy from to = do |
| 38 |
isDir <- isDirectory from |
| 39 |
if isDir |
| 40 |
then copyDirectory from to |
| 41 |
else copyFile from to |
| 42 |
|
| 43 |
copyDirectory :: FilePath -> FilePath -> IO () |
| 44 |
copyDirectory path path' = do |
| 45 |
members <- listDirectory path |
| 46 |
let members' = do |
| 47 |
member <- members |
| 48 |
let relative = |
| 49 |
fromMaybe (error "Unexpectedly empty member path") $ |
| 50 |
last member |
| 51 |
return $ path' <> relative |
| 52 |
sequence_ $ zipWith copy members members' |
| 53 |
|
| 54 |
last :: FilePath -> Maybe FilePath |
| 55 |
last p = case splitDirectories p of |
| 56 |
[] -> Nothing |
| 57 |
l -> Just $ List.last l |