continue conversion

Add Utility.OsString, with a special case for length.
This commit is contained in:
Joey Hess 2025-01-23 11:46:35 -04:00
parent c3c8870752
commit 12660314f1
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
6 changed files with 69 additions and 37 deletions

View file

@ -24,8 +24,8 @@ import Prelude
import Utility.Path
import Utility.UserInfo
import Utility.FileSystemEncoding
import qualified Utility.RawFilePath as R
import Utility.OsPath
import Utility.SystemDirectory
{- Makes a path absolute.
-
@ -37,7 +37,7 @@ import qualified Utility.RawFilePath as R
- Does not attempt to deal with edge cases or ensure security with
- untrusted inputs.
-}
absPathFrom :: RawFilePath -> RawFilePath -> RawFilePath
absPathFrom :: OsPath -> OsPath -> OsPath
absPathFrom dir path = simplifyPath (combine dir path)
{- Converts a filename into an absolute path.
@ -46,14 +46,14 @@ absPathFrom dir path = simplifyPath (combine dir path)
-
- Unlike Directory.canonicalizePath, this does not require the path
- already exists. -}
absPath :: RawFilePath -> IO RawFilePath
absPath :: OsPath -> IO OsPath
absPath file
-- Avoid unnecessarily getting the current directory when the path
-- is already absolute. absPathFrom uses simplifyPath
-- so also used here for consistency.
| isAbsolute file = return $ simplifyPath file
| otherwise = do
cwd <- R.getCurrentDirectory
cwd <- getCurrentDirectory
return $ absPathFrom cwd file
{- Constructs the minimal relative path from the CWD to a file.
@ -63,24 +63,23 @@ absPath file
- relPathCwdToFile "/tmp/foo/bar" == ""
- relPathCwdToFile "../bar/baz" == "baz"
-}
relPathCwdToFile :: RawFilePath -> IO RawFilePath
relPathCwdToFile :: OsPath -> IO OsPath
relPathCwdToFile f
-- Optimisation: Avoid doing any IO when the path is relative
-- and does not contain any ".." component.
| isRelative f && not (".." `B.isInfixOf` f) = return f
| isRelative f && not (".." `B.isInfixOf` fromOsPath f) = return f
| otherwise = do
c <- R.getCurrentDirectory
c <- getCurrentDirectory
relPathDirToFile c f
{- Constructs a minimal relative path from a directory to a file. -}
relPathDirToFile :: RawFilePath -> RawFilePath -> IO RawFilePath
relPathDirToFile :: OsPath -> OsPath -> IO OsPath
relPathDirToFile from to = relPathDirToFileAbs <$> absPath from <*> absPath to
{- Converts paths in the home directory to use ~/ -}
relHome :: FilePath -> IO String
relHome :: OsPath -> IO String
relHome path = do
let path' = toRawFilePath path
home <- toRawFilePath <$> myHomeDir
return $ if dirContains home path'
then fromRawFilePath ("~/" <> relPathDirToFileAbs home path')
else path
home <- toOsPath <$> myHomeDir
return $ if dirContains home path
then fromOsPath (literalOsPath "~/" <> relPathDirToFileAbs home path)
else fromOsPath path