2020-10-28 18:53:25 +00:00
|
|
|
{- absolute and relative path manipulation
|
|
|
|
-
|
2021-05-07 17:25:59 +00:00
|
|
|
- Copyright 2010-2021 Joey Hess <id@joeyh.name>
|
2020-10-28 18:53:25 +00:00
|
|
|
-
|
|
|
|
- License: BSD-2-clause
|
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
|
|
|
|
|
|
|
module Utility.Path.AbsRel (
|
|
|
|
absPathFrom,
|
|
|
|
absPath,
|
|
|
|
relPathCwdToFile,
|
|
|
|
relPathDirToFile,
|
|
|
|
relPathDirToFileAbs,
|
|
|
|
relHome,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import System.FilePath.ByteString
|
2021-05-07 17:25:59 +00:00
|
|
|
import qualified Data.ByteString as B
|
2020-10-28 18:53:25 +00:00
|
|
|
import Control.Applicative
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Utility.Path
|
|
|
|
import Utility.UserInfo
|
|
|
|
import Utility.FileSystemEncoding
|
2023-10-26 17:59:33 +00:00
|
|
|
import qualified Utility.RawFilePath as R
|
2020-10-28 18:53:25 +00:00
|
|
|
|
|
|
|
{- Makes a path absolute.
|
|
|
|
-
|
|
|
|
- Also simplifies it using simplifyPath.
|
|
|
|
-
|
|
|
|
- The first parameter is a base directory (ie, the cwd) to use if the path
|
2023-03-14 02:39:16 +00:00
|
|
|
- is not already absolute, and should itself be absolute.
|
2020-10-28 18:53:25 +00:00
|
|
|
-
|
|
|
|
- Does not attempt to deal with edge cases or ensure security with
|
|
|
|
- untrusted inputs.
|
|
|
|
-}
|
|
|
|
absPathFrom :: RawFilePath -> RawFilePath -> RawFilePath
|
|
|
|
absPathFrom dir path = simplifyPath (combine dir path)
|
|
|
|
|
|
|
|
{- Converts a filename into an absolute path.
|
|
|
|
-
|
|
|
|
- Also simplifies it using simplifyPath.
|
|
|
|
-
|
|
|
|
- Unlike Directory.canonicalizePath, this does not require the path
|
|
|
|
- already exists. -}
|
|
|
|
absPath :: RawFilePath -> IO RawFilePath
|
|
|
|
absPath file
|
|
|
|
-- Avoid unncessarily 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
|
2023-10-26 17:59:33 +00:00
|
|
|
cwd <- R.getCurrentDirectory
|
2020-10-28 18:53:25 +00:00
|
|
|
return $ absPathFrom cwd file
|
|
|
|
|
2021-05-07 16:57:54 +00:00
|
|
|
{- Constructs the minimal relative path from the CWD to a file.
|
2020-10-28 18:53:25 +00:00
|
|
|
-
|
|
|
|
- For example, assuming CWD is /tmp/foo/bar:
|
|
|
|
- relPathCwdToFile "/tmp/foo" == ".."
|
|
|
|
- relPathCwdToFile "/tmp/foo/bar" == ""
|
2021-05-07 16:57:54 +00:00
|
|
|
- relPathCwdToFile "../bar/baz" == "baz"
|
2020-10-28 18:53:25 +00:00
|
|
|
-}
|
|
|
|
relPathCwdToFile :: RawFilePath -> IO RawFilePath
|
2021-05-07 17:25:59 +00:00
|
|
|
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
|
|
|
|
| otherwise = do
|
2023-10-26 17:59:33 +00:00
|
|
|
c <- R.getCurrentDirectory
|
2021-05-07 17:25:59 +00:00
|
|
|
relPathDirToFile c f
|
2020-10-28 18:53:25 +00:00
|
|
|
|
2021-05-07 16:57:54 +00:00
|
|
|
{- Constructs a minimal relative path from a directory to a file. -}
|
2020-10-28 18:53:25 +00:00
|
|
|
relPathDirToFile :: RawFilePath -> RawFilePath -> IO RawFilePath
|
|
|
|
relPathDirToFile from to = relPathDirToFileAbs <$> absPath from <*> absPath to
|
|
|
|
|
|
|
|
{- Converts paths in the home directory to use ~/ -}
|
|
|
|
relHome :: FilePath -> IO String
|
|
|
|
relHome path = do
|
|
|
|
let path' = toRawFilePath path
|
|
|
|
home <- toRawFilePath <$> myHomeDir
|
|
|
|
return $ if dirContains home path'
|
|
|
|
then fromRawFilePath ("~/" <> relPathDirToFileAbs home path')
|
|
|
|
else path
|