2011-08-22 16:14:12 -04:00
|
|
|
{- path manipulation
|
|
|
|
-
|
addurl --preserve-filename and a few related changes
* addurl --preserve-filename: New option, uses server-provided filename
without any sanitization, but with some security checking.
Not yet implemented for remotes other than the web.
* addurl, importfeed: Avoid adding filenames with leading '.', instead
it will be replaced with '_'.
This might be considered a security fix, but a CVE seems unwattanted.
It was possible for addurl to create a dotfile, which could change
behavior of some program. It was also possible for a web server to say
the file name was ".git" or "foo/.git". That would not overrwrite the
.git directory, but would cause addurl to fail; of course git won't
add "foo/.git".
sanitizeFilePath is too opinionated to remain in Utility, so moved it.
The changes to mkSafeFilePath are because it used sanitizeFilePath.
In particular:
isDrive will never succeed, because "c:" gets munged to "c_"
".." gets sanitized now
".git" gets sanitized now
It will never be null, because sanitizeFilePath keeps the length
the same, and splitDirectories never returns a null path.
Also, on the off chance a web server suggests a filename of "",
ignore that, rather than trying to save to such a filename, which would
fail in some way.
2020-05-08 16:09:29 -04:00
|
|
|
- Copyright 2010-2020 Joey Hess <id@joeyh.name>
|
2011-08-22 16:14:12 -04:00
|
|
|
-
|
2014-05-10 11:01:27 -03:00
|
|
|
- License: BSD-2-clause
|
2011-08-22 16:14:12 -04:00
|
|
|
-}
|
2013-04-14 12:51:05 -04:00
|
|
|
|
2020-10-28 14:18:09 -04:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2017-12-31 16:08:31 -04:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-05-10 16:31:50 -04:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
2011-08-22 16:14:12 -04:00
|
|
|
|
2019-11-21 15:38:06 -04:00
|
|
|
module Utility.Path (
|
|
|
|
simplifyPath,
|
|
|
|
parentDir,
|
|
|
|
upFrom,
|
|
|
|
dirContains,
|
|
|
|
segmentPaths,
|
2020-09-14 16:49:33 -04:00
|
|
|
segmentPaths',
|
2019-11-21 15:38:06 -04:00
|
|
|
runSegmentPaths,
|
2020-09-14 16:49:33 -04:00
|
|
|
runSegmentPaths',
|
2019-11-21 15:38:06 -04:00
|
|
|
inPath,
|
|
|
|
searchPath,
|
|
|
|
dotfile,
|
|
|
|
splitShortExtensions,
|
2020-10-28 14:53:25 -04:00
|
|
|
relPathDirToFileAbs,
|
2019-11-21 15:38:06 -04:00
|
|
|
) where
|
2011-08-22 16:14:12 -04:00
|
|
|
|
2020-10-28 14:18:09 -04:00
|
|
|
import System.FilePath.ByteString
|
|
|
|
import qualified System.FilePath as P
|
|
|
|
import qualified Data.ByteString as B
|
2011-08-22 16:14:12 -04:00
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
2011-08-25 00:28:55 -04:00
|
|
|
import Control.Applicative
|
2015-05-10 16:19:56 -04:00
|
|
|
import Prelude
|
2011-10-16 00:31:25 -04:00
|
|
|
|
|
|
|
import Utility.Monad
|
2020-03-05 13:52:30 -04:00
|
|
|
import Utility.SystemDirectory
|
2011-08-22 16:14:12 -04:00
|
|
|
|
2020-11-10 11:21:03 -04:00
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
import Data.Char
|
|
|
|
import Utility.FileSystemEncoding
|
|
|
|
#endif
|
|
|
|
|
2015-08-04 14:58:21 -04:00
|
|
|
{- Simplifies a path, removing any "." component, collapsing "dir/..",
|
|
|
|
- and removing the trailing path separator.
|
2014-02-07 17:10:51 -04:00
|
|
|
-
|
|
|
|
- On Windows, preserves whichever style of path separator might be used in
|
2020-10-28 14:18:09 -04:00
|
|
|
- the input RawFilePaths. This is done because some programs in Windows
|
2014-02-07 17:10:51 -04:00
|
|
|
- demand a particular path separator -- and which one actually varies!
|
|
|
|
-
|
|
|
|
- This does not guarantee that two paths that refer to the same location,
|
|
|
|
- and are both relative to the same location (or both absolute) will
|
2020-10-28 14:18:09 -04:00
|
|
|
- yeild the same result. Run both through normalise from System.RawFilePath
|
2014-02-07 17:10:51 -04:00
|
|
|
- to ensure that.
|
|
|
|
-}
|
2020-10-28 14:18:09 -04:00
|
|
|
simplifyPath :: RawFilePath -> RawFilePath
|
2014-02-07 17:10:51 -04:00
|
|
|
simplifyPath path = dropTrailingPathSeparator $
|
|
|
|
joinDrive drive $ joinPath $ norm [] $ splitPath path'
|
|
|
|
where
|
|
|
|
(drive, path') = splitDrive path
|
|
|
|
|
|
|
|
norm c [] = reverse c
|
|
|
|
norm c (p:ps)
|
2015-08-04 14:58:21 -04:00
|
|
|
| p' == ".." && not (null c) && dropTrailingPathSeparator (c !! 0) /= ".." =
|
|
|
|
norm (drop 1 c) ps
|
2014-02-07 17:10:51 -04:00
|
|
|
| p' == "." = norm c ps
|
|
|
|
| otherwise = norm (p:c) ps
|
|
|
|
where
|
|
|
|
p' = dropTrailingPathSeparator p
|
|
|
|
|
2015-01-09 14:26:52 -04:00
|
|
|
{- takeDirectory "foo/bar/" is "foo/bar". This instead yields "foo" -}
|
2020-10-28 14:18:09 -04:00
|
|
|
parentDir :: RawFilePath -> RawFilePath
|
2015-01-09 14:26:52 -04:00
|
|
|
parentDir = takeDirectory . dropTrailingPathSeparator
|
|
|
|
|
|
|
|
{- Just the parent directory of a path, or Nothing if the path has no
|
2020-05-11 15:03:35 -04:00
|
|
|
- parent (ie for "/" or "." or "foo") -}
|
2020-10-28 14:18:09 -04:00
|
|
|
upFrom :: RawFilePath -> Maybe RawFilePath
|
2015-01-09 14:26:52 -04:00
|
|
|
upFrom dir
|
2015-01-20 17:23:23 -04:00
|
|
|
| length dirs < 2 = Nothing
|
2020-10-28 14:18:09 -04:00
|
|
|
| otherwise = Just $ joinDrive drive $
|
|
|
|
B.intercalate (B.singleton pathSeparator) $ init dirs
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
2017-05-15 23:32:17 -04:00
|
|
|
-- on Unix, the drive will be "/" when the dir is absolute,
|
|
|
|
-- otherwise ""
|
2013-05-12 13:58:46 -05:00
|
|
|
(drive, path) = splitDrive dir
|
2020-10-28 14:18:09 -04:00
|
|
|
dirs = filter (not . B.null) $ B.splitWith isPathSeparator path
|
2011-08-22 16:14:12 -04:00
|
|
|
|
2020-10-28 14:18:09 -04:00
|
|
|
{- Checks if the first RawFilePath is, or could be said to contain the second.
|
2011-08-22 16:14:12 -04:00
|
|
|
- For example, "foo/" contains "foo/bar". Also, "foo", "./foo", "foo/" etc
|
|
|
|
- are all equivilant.
|
|
|
|
-}
|
2020-10-28 14:18:09 -04:00
|
|
|
dirContains :: RawFilePath -> RawFilePath -> Bool
|
2019-12-20 18:01:29 -04:00
|
|
|
dirContains a b = a == b
|
|
|
|
|| a' == b'
|
2020-10-28 14:18:09 -04:00
|
|
|
|| (addTrailingPathSeparator a') `B.isPrefixOf` b'
|
2019-12-20 18:01:29 -04:00
|
|
|
|| a' == "." && normalise ("." </> b') == b'
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
|
|
|
a' = norm a
|
|
|
|
b' = norm b
|
2014-02-07 17:10:51 -04:00
|
|
|
norm = normalise . simplifyPath
|
2011-08-22 16:14:12 -04:00
|
|
|
|
2012-11-25 17:54:08 -04:00
|
|
|
{- Given an original list of paths, and an expanded list derived from it,
|
2015-04-02 01:44:32 -04:00
|
|
|
- which may be arbitrarily reordered, generates a list of lists, where
|
|
|
|
- each sublist corresponds to one of the original paths.
|
|
|
|
-
|
|
|
|
- When the original path is a directory, any items in the expanded list
|
|
|
|
- that are contained in that directory will appear in its segment.
|
|
|
|
-
|
|
|
|
- The order of the original list of paths is attempted to be preserved in
|
|
|
|
- the order of the returned segments. However, doing so has a O^NM
|
|
|
|
- growth factor. So, if the original list has more than 100 paths on it,
|
|
|
|
- we stop preserving ordering at that point. Presumably a user passing
|
|
|
|
- that many paths in doesn't care too much about order of the later ones.
|
2011-09-19 01:37:04 -04:00
|
|
|
-}
|
2020-07-10 13:54:52 -04:00
|
|
|
segmentPaths :: (a -> RawFilePath) -> [RawFilePath] -> [a] -> [[a]]
|
2020-09-14 16:49:33 -04:00
|
|
|
segmentPaths = segmentPaths' (\_ r -> r)
|
|
|
|
|
|
|
|
segmentPaths' :: (Maybe RawFilePath -> a -> r) -> (a -> RawFilePath) -> [RawFilePath] -> [a] -> [[r]]
|
|
|
|
segmentPaths' f _ [] new = [map (f Nothing) new]
|
|
|
|
segmentPaths' f _ [i] new = [map (f (Just i)) new] -- optimisation
|
|
|
|
segmentPaths' f c (i:is) new =
|
|
|
|
map (f (Just i)) found : segmentPaths' f c is rest
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
2020-09-14 16:49:33 -04:00
|
|
|
(found, rest) = if length is < 100
|
|
|
|
then partition ini new
|
|
|
|
else break (not . ini) new
|
2020-10-28 14:18:09 -04:00
|
|
|
ini p = i `dirContains` c p
|
2011-09-19 01:37:04 -04:00
|
|
|
|
2012-11-25 17:54:08 -04:00
|
|
|
{- This assumes that it's cheaper to call segmentPaths on the result,
|
|
|
|
- than it would be to run the action separately with each path. In
|
|
|
|
- the case of git file list commands, that assumption tends to hold.
|
2011-09-19 01:37:04 -04:00
|
|
|
-}
|
2020-10-13 16:31:01 -04:00
|
|
|
runSegmentPaths :: (a -> RawFilePath) -> ([RawFilePath] -> IO [a]) -> [RawFilePath] -> IO [[a]]
|
|
|
|
runSegmentPaths c a paths = segmentPaths c paths <$> a paths
|
2011-10-16 00:31:25 -04:00
|
|
|
|
2020-10-13 16:31:01 -04:00
|
|
|
runSegmentPaths' :: (Maybe RawFilePath -> a -> r) -> (a -> RawFilePath) -> ([RawFilePath] -> IO [a]) -> [RawFilePath] -> IO [[r]]
|
|
|
|
runSegmentPaths' si c a paths = segmentPaths' si c paths <$> a paths
|
2020-09-14 16:49:33 -04:00
|
|
|
|
2012-12-14 15:52:44 -04:00
|
|
|
{- Checks if a command is available in PATH.
|
|
|
|
-
|
|
|
|
- The command may be fully-qualified, in which case, this succeeds as
|
|
|
|
- long as it exists. -}
|
2011-10-16 00:31:25 -04:00
|
|
|
inPath :: String -> IO Bool
|
2012-12-14 15:52:44 -04:00
|
|
|
inPath command = isJust <$> searchPath command
|
|
|
|
|
|
|
|
{- Finds a command in PATH and returns the full path to it.
|
|
|
|
-
|
|
|
|
- The command may be fully qualified already, in which case it will
|
|
|
|
- be returned if it exists.
|
2017-03-08 15:56:55 -04:00
|
|
|
-
|
|
|
|
- Note that this will find commands in PATH that are not executable.
|
2012-12-14 15:52:44 -04:00
|
|
|
-}
|
|
|
|
searchPath :: String -> IO (Maybe FilePath)
|
|
|
|
searchPath command
|
2020-10-28 14:18:09 -04:00
|
|
|
| P.isAbsolute command = check command
|
|
|
|
| otherwise = P.getSearchPath >>= getM indir
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
2020-10-28 14:18:09 -04:00
|
|
|
indir d = check $ d P.</> command
|
2013-07-06 00:48:47 -04:00
|
|
|
check f = firstM doesFileExist
|
2013-08-02 12:27:32 -04:00
|
|
|
#ifdef mingw32_HOST_OS
|
2013-07-06 00:48:47 -04:00
|
|
|
[f, f ++ ".exe"]
|
|
|
|
#else
|
|
|
|
[f]
|
|
|
|
#endif
|
2012-01-03 00:09:09 -04:00
|
|
|
|
|
|
|
{- Checks if a filename is a unix dotfile. All files inside dotdirs
|
|
|
|
- count as dotfiles. -}
|
2020-10-28 14:18:09 -04:00
|
|
|
dotfile :: RawFilePath -> Bool
|
2012-01-03 00:09:09 -04:00
|
|
|
dotfile file
|
|
|
|
| f == "." = False
|
|
|
|
| f == ".." = False
|
|
|
|
| f == "" = False
|
2020-10-28 14:18:09 -04:00
|
|
|
| otherwise = "." `B.isPrefixOf` f || dotfile (takeDirectory file)
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
|
|
|
f = takeFileName file
|
2013-05-14 13:24:15 -04:00
|
|
|
|
2020-10-28 14:18:09 -04:00
|
|
|
{- Similar to splitExtensions, but knows that some things in RawFilePaths
|
2014-02-16 17:39:54 -04:00
|
|
|
- after a dot are too long to be extensions. -}
|
2020-10-28 14:18:09 -04:00
|
|
|
splitShortExtensions :: RawFilePath -> (RawFilePath, [B.ByteString])
|
2014-02-16 17:39:54 -04:00
|
|
|
splitShortExtensions = splitShortExtensions' 5 -- enough for ".jpeg"
|
2020-10-28 14:18:09 -04:00
|
|
|
splitShortExtensions' :: Int -> RawFilePath -> (RawFilePath, [B.ByteString])
|
2014-02-16 17:39:54 -04:00
|
|
|
splitShortExtensions' maxextension = go []
|
|
|
|
where
|
|
|
|
go c f
|
2020-10-28 14:18:09 -04:00
|
|
|
| len > 0 && len <= maxextension && not (B.null base) =
|
2014-02-16 17:39:54 -04:00
|
|
|
go (ext:c) base
|
|
|
|
| otherwise = (f, c)
|
|
|
|
where
|
|
|
|
(base, ext) = splitExtension f
|
2020-10-28 14:18:09 -04:00
|
|
|
len = B.length ext
|
2020-10-28 14:53:25 -04:00
|
|
|
|
|
|
|
{- This requires the first path to be absolute, and the
|
|
|
|
- second path cannot contain ../ or ./
|
|
|
|
-
|
|
|
|
- On Windows, if the paths are on different drives,
|
|
|
|
- a relative path is not possible and the path is simply
|
|
|
|
- returned as-is.
|
|
|
|
-}
|
|
|
|
relPathDirToFileAbs :: RawFilePath -> RawFilePath -> RawFilePath
|
|
|
|
relPathDirToFileAbs from to
|
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
| normdrive from /= normdrive to = to
|
|
|
|
#endif
|
|
|
|
| otherwise = joinPath $ dotdots ++ uncommon
|
|
|
|
where
|
|
|
|
pfrom = sp from
|
|
|
|
pto = sp to
|
|
|
|
sp = map dropTrailingPathSeparator . splitPath . dropDrive
|
|
|
|
common = map fst $ takeWhile same $ zip pfrom pto
|
|
|
|
same (c,d) = c == d
|
|
|
|
uncommon = drop numcommon pto
|
|
|
|
dotdots = replicate (length pfrom - numcommon) ".."
|
|
|
|
numcommon = length common
|
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
normdrive = map toLower . takeWhile (/= ':') . fromRawFilePath . takeDrive
|
|
|
|
#endif
|