2020-10-28 21:25:59 +00:00
|
|
|
{- Portability shim for basic operations on RawFilePaths.
|
2019-12-06 18:17:48 +00:00
|
|
|
-
|
|
|
|
- On unix, this makes syscalls using RawFilesPaths as efficiently as
|
|
|
|
- possible.
|
|
|
|
-
|
|
|
|
- On Windows, filenames are in unicode, so RawFilePaths have to be
|
|
|
|
- decoded. So this library will work, but less efficiently than using
|
|
|
|
- FilePath would.
|
2019-01-14 19:19:20 +00:00
|
|
|
-
|
2020-10-28 20:03:45 +00:00
|
|
|
- Copyright 2019-2020 Joey Hess <id@joeyh.name>
|
2019-01-14 19:19:20 +00:00
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Utility.RawFilePath (
|
|
|
|
RawFilePath,
|
|
|
|
readSymbolicLink,
|
2020-10-28 20:03:45 +00:00
|
|
|
createSymbolicLink,
|
2020-10-29 14:33:12 +00:00
|
|
|
createLink,
|
|
|
|
removeLink,
|
2019-12-06 18:44:42 +00:00
|
|
|
getFileStatus,
|
2019-12-06 19:37:12 +00:00
|
|
|
getSymbolicLinkStatus,
|
2019-12-11 18:12:22 +00:00
|
|
|
doesPathExist,
|
2020-10-28 20:03:45 +00:00
|
|
|
getCurrentDirectory,
|
2020-10-28 21:25:59 +00:00
|
|
|
createDirectory,
|
2019-01-14 19:19:20 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
#ifndef mingw32_HOST_OS
|
2019-12-06 18:44:42 +00:00
|
|
|
import Utility.FileSystemEncoding (RawFilePath)
|
2019-01-14 19:19:20 +00:00
|
|
|
import System.Posix.Files.ByteString
|
2020-10-28 21:25:59 +00:00
|
|
|
import qualified System.Posix.Directory.ByteString as D
|
2019-12-11 18:12:22 +00:00
|
|
|
|
2020-10-29 16:02:46 +00:00
|
|
|
-- | Checks if a file or directory exists. Note that a dangling symlink
|
2020-07-20 01:31:06 +00:00
|
|
|
-- will be false.
|
2019-12-11 18:12:22 +00:00
|
|
|
doesPathExist :: RawFilePath -> IO Bool
|
|
|
|
doesPathExist = fileExist
|
|
|
|
|
2020-10-28 20:03:45 +00:00
|
|
|
getCurrentDirectory :: IO RawFilePath
|
2020-10-28 21:25:59 +00:00
|
|
|
getCurrentDirectory = D.getWorkingDirectory
|
|
|
|
|
|
|
|
createDirectory :: RawFilePath -> IO ()
|
|
|
|
createDirectory p = D.createDirectory p 0o777
|
2020-10-28 20:03:45 +00:00
|
|
|
|
2019-01-14 19:19:20 +00:00
|
|
|
#else
|
|
|
|
import qualified Data.ByteString as B
|
2020-01-01 16:24:31 +00:00
|
|
|
import System.PosixCompat (FileStatus)
|
2019-12-06 18:17:48 +00:00
|
|
|
import qualified System.PosixCompat as P
|
2019-12-11 18:12:22 +00:00
|
|
|
import qualified System.Directory as D
|
2019-12-06 18:17:48 +00:00
|
|
|
import Utility.FileSystemEncoding
|
2019-01-14 19:19:20 +00:00
|
|
|
|
|
|
|
readSymbolicLink :: RawFilePath -> IO RawFilePath
|
2019-12-06 18:17:48 +00:00
|
|
|
readSymbolicLink f = toRawFilePath <$> P.readSymbolicLink (fromRawFilePath f)
|
2019-12-06 18:44:42 +00:00
|
|
|
|
2020-10-28 20:03:45 +00:00
|
|
|
createSymbolicLink :: RawFilePath -> RawFilePath -> IO ()
|
|
|
|
createSymbolicLink a b = P.createSymbolicLink
|
|
|
|
(fromRawFilePath a)
|
|
|
|
(fromRawFilePath b)
|
|
|
|
|
2020-10-29 14:33:12 +00:00
|
|
|
createLink :: RawFilePath -> RawFilePath -> IO ()
|
|
|
|
createLink a b = P.createLink
|
|
|
|
(fromRawFilePath a)
|
|
|
|
(fromRawFilePath b)
|
|
|
|
|
|
|
|
removeLink :: RawFilePath -> IO ()
|
|
|
|
removeLink = P.removeLink . fromRawFilePath
|
|
|
|
|
2019-12-06 18:44:42 +00:00
|
|
|
getFileStatus :: RawFilePath -> IO FileStatus
|
|
|
|
getFileStatus = P.getFileStatus . fromRawFilePath
|
2019-12-06 19:37:12 +00:00
|
|
|
|
|
|
|
getSymbolicLinkStatus :: RawFilePath -> IO FileStatus
|
|
|
|
getSymbolicLinkStatus = P.getSymbolicLinkStatus . fromRawFilePath
|
2019-12-11 18:12:22 +00:00
|
|
|
|
|
|
|
doesPathExist :: RawFilePath -> IO Bool
|
|
|
|
doesPathExist = D.doesPathExist . fromRawFilePath
|
2020-10-28 20:03:45 +00:00
|
|
|
|
|
|
|
getCurrentDirectory :: IO RawFilePath
|
|
|
|
getCurrentDirectory = toRawFilePath <$> D.getCurrentDirectory
|
2020-10-28 21:25:59 +00:00
|
|
|
|
|
|
|
createDirectory :: RawFilePath -> IO ()
|
|
|
|
createDirectory = D.createDirectory . fromRawFilePath
|
2019-01-14 19:19:20 +00:00
|
|
|
#endif
|