OsPath build flag no longer depends on filepath-bytestring

However, filepath-bytestring is still in Setup-Depends.
That's because Utility.OsPath uses it when not built with OsPath.
It would be maybe possible to make Utility.OsPath fall back to using
filepath, and eliminate that dependency too, but it would mean either
wrapping all of System.FilePath's functions, or using `type OsPath = FilePath`

Annex.Import uses ifdefs to avoid converting back to FilePath when not
on windows. On windows it's a bit slower due to that conversion.
Utility.Path.Windows.convertToWindowsNativeNamespace got a bit
slower too, but not really worth optimising I think.

Note that importing Utility.FileSystemEncoding at the same time as
System.Posix.ByteString will result in conflicting definitions for
RawFilePath. filepath-bytestring avoids that by importing RawFilePath
from System.Posix.ByteString, but that's not possible in
Utility.FileSystemEncoding, since Setup-Depends does not include unix.
This turned out not to affect any code in git-annex though.

Sponsored-by: Leon Schuermann
This commit is contained in:
Joey Hess 2025-02-10 16:25:31 -04:00
parent ce697aa8ae
commit 2ff716be30
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
13 changed files with 81 additions and 68 deletions

View file

@ -23,7 +23,6 @@ module Utility.FileSystemEncoding (
import qualified GHC.IO.Encoding as Encoding
import System.IO
import System.FilePath.ByteString (RawFilePath, encodeFilePath, decodeFilePath)
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
#ifdef mingw32_HOST_OS
@ -37,6 +36,9 @@ import Data.Char
import Data.List
#endif
-- | A literal file path
type RawFilePath = S.ByteString
{- Makes all subsequent Handles that are opened, as well as stdio Handles,
- use the filesystem encoding, instead of the encoding of the current
- locale.
@ -90,9 +92,7 @@ encodeBL = L8.fromString
decodeBS :: S.ByteString -> FilePath
#ifndef mingw32_HOST_OS
-- This does the same thing as System.FilePath.ByteString.decodeFilePath,
-- with an identical implementation. However, older versions of that library
-- truncated at NUL, which this must not do, because it may end up used on
-- something other than a unix filepath.
-- with an identical implementation.
{-# NOINLINE decodeBS #-}
decodeBS b = unsafePerformIO $ do
enc <- Encoding.getFileSystemEncoding
@ -104,9 +104,7 @@ decodeBS = S8.toString
encodeBS :: FilePath -> S.ByteString
#ifndef mingw32_HOST_OS
-- This does the same thing as System.FilePath.ByteString.encodeFilePath,
-- with an identical implementation. However, older versions of that library
-- truncated at NUL, which this must not do, because it may end up used on
-- something other than a unix filepath.
-- with an identical implementation.
{-# NOINLINE encodeBS #-}
encodeBS f = unsafePerformIO $ do
enc <- Encoding.getFileSystemEncoding
@ -116,10 +114,10 @@ encodeBS = S8.fromString
#endif
fromRawFilePath :: RawFilePath -> FilePath
fromRawFilePath = decodeFilePath
fromRawFilePath = decodeBS
toRawFilePath :: FilePath -> RawFilePath
toRawFilePath = encodeFilePath
toRawFilePath = encodeBS
{- Truncates a FilePath to the given number of bytes (or less),
- as represented on disk.

View file

@ -14,7 +14,8 @@ module Utility.OpenFd where
import System.Posix.IO.ByteString
import System.Posix.Types
import System.FilePath.ByteString (RawFilePath)
import Utility.RawFilePath
openFdWithMode :: RawFilePath -> OpenMode -> Maybe FileMode -> OpenFileFlags -> IO Fd
#if MIN_VERSION_unix(2,8,0)

View file

@ -31,7 +31,7 @@ import qualified Data.ByteString.Lazy as L
import System.OsPath as X hiding (OsPath, OsString, pack, unpack, unsafeFromChar)
import System.OsPath
import "os-string" System.OsString.Internal.Types
import qualified System.FilePath.ByteString as PB
import qualified System.FilePath as PS
#if defined(mingw32_HOST_OS)
import GHC.IO (unsafePerformIO)
import System.OsString.Encoding.Internal (cWcharsToChars_UCS2)
@ -100,7 +100,7 @@ bytesFromOsPath = getPosixString . getOsString
{- For some reason not included in System.OsPath -}
getSearchPath :: IO [OsPath]
getSearchPath = map toOsPath <$> PB.getSearchPath
getSearchPath = map toOsPath <$> PS.getSearchPath
{- Used for string constants. Note that when using OverloadedStrings,
- the IsString instance for ShortByteString only works properly with

View file

@ -15,9 +15,10 @@ module Utility.Path.Windows (
import Utility.Path
import Utility.OsPath
import Utility.SystemDirectory
import Utility.FileSystemEncoding
import qualified Data.ByteString as B
import qualified System.FilePath.Windows.ByteString as P
import qualified System.FilePath.Windows as WinPath
{- Convert a filepath to use Windows's native namespace.
- This avoids filesystem length limits.
@ -36,9 +37,9 @@ convertToWindowsNativeNamespace f
-- Make absolute because any '.' and '..' in the path
-- will not be resolved once it's converted.
cwd <- getCurrentDirectory
let p = fromOsPath (simplifyPath (combine cwd (toOsPath f)))
let p = simplifyPath (combine cwd (toOsPath f))
-- Normalize slashes.
let p' = P.normalise p
let p' = encodeBS $ WinPath.normalise $ fromOsPath p
return (win32_file_namespace <> p')
where
win32_dev_namespace = "\\\\.\\"

View file

@ -14,10 +14,11 @@ module Utility.Touch (
#if ! defined(mingw32_HOST_OS)
import System.FilePath.ByteString (RawFilePath)
import System.Posix.Files.ByteString
import Data.Time.Clock.POSIX
import Utility.RawFilePath
{- Changes the access and modification times of an existing file.
Can follow symlinks, or not. -}
touchBoth :: RawFilePath -> POSIXTime -> POSIXTime -> Bool -> IO ()