{-# LANGUAGE CPP #-}
module System.Path(
splitExt, absNormPath, secureAbsNormPath,
recurseDir, recurseDirStat, recursiveRemove,
bracketCWD,
mktmpdir, brackettmpdir, brackettmpdirCWD
)
where
import Data.List.Utils ( startswith, alwaysElemRIndex )
#if !(defined(mingw32_HOST_OS) || defined(mingw32_TARGET_OS) || defined(__MINGW32__))
import System.Directory
( getCurrentDirectory, removeFile, setCurrentDirectory )
import System.Posix.Directory ( createDirectory )
import System.Posix.Temp ( mkstemp )
#else
import System.Directory
import System.IO ( openTempFile )
#endif
import Control.Exception ( finally )
import System.FilePath ( pathSeparator )
import System.IO ( hClose )
import System.IO.HVFS.Utils
( SystemFS(SystemFS), recurseDir, recurseDirStat, recursiveRemove )
import System.Path.NameManip
( normalise_path, absolute_path_by, guess_dotdot )
splitExt :: String -> (String, String)
splitExt :: String -> (String, String)
splitExt String
path =
let dotindex :: Int
dotindex = forall a. Eq a => a -> [a] -> Int
alwaysElemRIndex Char
'.' String
path
slashindex :: Int
slashindex = forall a. Eq a => a -> [a] -> Int
alwaysElemRIndex Char
pathSeparator String
path
in
if Int
dotindex forall a. Ord a => a -> a -> Bool
<= Int
slashindex
then (String
path, String
"")
else ((forall a. Int -> [a] -> [a]
take Int
dotindex String
path), (forall a. Int -> [a] -> [a]
drop Int
dotindex String
path))
absNormPath :: String
-> String
-> Maybe String
absNormPath :: String -> String -> Maybe String
absNormPath String
base String
thepath =
let path :: String
path = String -> String -> String
absolute_path_by String
base String
thepath
in case String -> Maybe String
guess_dotdot (String -> String
normalise_path String
path) of
Just String
"." -> forall a. a -> Maybe a
Just [Char
pathSeparator]
Maybe String
x -> Maybe String
x
secureAbsNormPath :: String
-> String
-> Maybe String
secureAbsNormPath :: String -> String -> Maybe String
secureAbsNormPath String
base String
s = do String
p <- String -> String -> Maybe String
absNormPath String
base String
s
if forall a. Eq a => [a] -> [a] -> Bool
startswith String
base String
p
then forall (m :: * -> *) a. Monad m => a -> m a
return String
p
else forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
""
mktmpdir :: String -> IO String
#if !(defined(mingw32_HOST_OS) || defined(mingw32_TARGET_OS) || defined(__MINGW32__))
mktmpdir :: String -> IO String
mktmpdir String
x =
do (String, Handle)
y <- String -> IO (String, Handle)
mkstemp String
x
let (String
dirname, Handle
h) = (String, Handle)
y
Handle -> IO ()
hClose Handle
h
String -> IO ()
removeFile String
dirname
String -> FileMode -> IO ()
createDirectory String
dirname FileMode
0o700
forall (m :: * -> *) a. Monad m => a -> m a
return String
dirname
#else
mktmpdir x =
do (fp, h) <- openTempFile "" x
hClose h
removeFile fp
createDirectory fp
return fp
#endif
brackettmpdir :: String -> (String -> IO a) -> IO a
brackettmpdir :: forall a. String -> (String -> IO a) -> IO a
brackettmpdir String
x String -> IO a
action = do String
tmpdir <- String -> IO String
mktmpdir String
x
forall a b. IO a -> IO b -> IO a
finally (String -> IO a
action String
tmpdir)
(forall a. HVFS a => a -> String -> IO ()
recursiveRemove SystemFS
SystemFS String
tmpdir)
bracketCWD :: FilePath -> IO a -> IO a
bracketCWD :: forall a. String -> IO a -> IO a
bracketCWD String
fp IO a
action =
do String
oldcwd <- IO String
getCurrentDirectory
String -> IO ()
setCurrentDirectory String
fp
forall a b. IO a -> IO b -> IO a
finally IO a
action (String -> IO ()
setCurrentDirectory String
oldcwd)
brackettmpdirCWD :: String -> IO a -> IO a
brackettmpdirCWD :: forall a. String -> IO a -> IO a
brackettmpdirCWD String
template IO a
action =
forall a. String -> (String -> IO a) -> IO a
brackettmpdir String
template (\String
newdir -> forall a. String -> IO a -> IO a
bracketCWD String
newdir IO a
action)