Safe Haskell | None |
---|---|
Language | Haskell2010 |
Darcs.Util.Path
Contents
- module Storage.Hashed.AnchoredPath
- data FileName
- fp2fn :: FilePath -> FileName
- fn2fp :: FileName -> FilePath
- fn2ps :: FileName -> ByteString
- ps2fn :: ByteString -> FileName
- niceps2fn :: ByteString -> FileName
- fn2niceps :: FileName -> ByteString
- breakOnDir :: FileName -> Maybe (FileName, FileName)
- normPath :: FileName -> FileName
- ownName :: FileName -> FileName
- superName :: FileName -> FileName
- movedirfilename :: FileName -> FileName -> FileName -> FileName
- encodeWhite :: FilePath -> String
- decodeWhite :: String -> FilePath
- isParentOrEqOf :: FileName -> FileName -> Bool
- data AbsolutePath
- makeAbsolute :: AbsolutePath -> FilePath -> AbsolutePath
- ioAbsolute :: FilePath -> IO AbsolutePath
- rootDirectory :: AbsolutePath
- data AbsolutePathOrStd
- makeAbsoluteOrStd :: AbsolutePath -> String -> AbsolutePathOrStd
- ioAbsoluteOrStd :: String -> IO AbsolutePathOrStd
- useAbsoluteOrStd :: (AbsolutePath -> a) -> a -> AbsolutePathOrStd -> a
- stdOut :: AbsolutePathOrStd
- data AbsoluteOrRemotePath
- ioAbsoluteOrRemote :: String -> IO AbsoluteOrRemotePath
- isRemote :: AbsoluteOrRemotePath -> Bool
- data SubPath
- makeSubPathOf :: AbsolutePath -> AbsolutePath -> Maybe SubPath
- simpleSubPath :: FilePath -> Maybe SubPath
- isSubPathOf :: SubPath -> SubPath -> Bool
- floatSubPath :: SubPath -> AnchoredPath
- sp2fn :: SubPath -> FileName
- class FilePathOrURL a where
- class FilePathOrURL a => FilePathLike a where
- toFilePath :: a -> FilePath
- getCurrentDirectory :: IO AbsolutePath
- setCurrentDirectory :: FilePathLike p => p -> IO ()
- getUniquePathName :: Bool -> (FilePath -> String) -> (Int -> FilePath) -> IO FilePath
- doesPathExist :: FilePath -> IO Bool
- isMaliciousPath :: String -> Bool
- isMaliciousSubPath :: String -> Bool
- filterFilePaths :: [FilePath] -> AnchoredPath -> t -> Bool
- filterPaths :: [AnchoredPath] -> AnchoredPath -> t -> Bool
Documentation
module Storage.Hashed.AnchoredPath
data FileName
FileName is an abstract type intended to facilitate the input and output of unicode filenames.
fn2ps :: FileName -> ByteString
ps2fn :: ByteString -> FileName
niceps2fn :: ByteString -> FileName
fn2niceps :: FileName -> ByteString
breakOnDir :: FileName -> Maybe (FileName, FileName)
normPath :: FileName -> FileName
convert a path string into a sequence of directories strings "/", "." and ".." are generally interpreted as expected. Behaviour with too many '..' is to leave them.
Examples: Splitting: "aabbcc" -> ["aa","bb","cc"] Ignoring "." and extra "/": "aa.bb" -> ["aa","bb"] "aa//bb" -> ["aa","bb"] "aabb/" -> ["aa","bb"] Handling "..": "aa..bb/cc" -> ["bb","cc"] "aabb....cc" -> ["cc"] "aa..bb..cc" -> ["cc"] "../cc" -> ["..","cc"]
movedirfilename :: FileName -> FileName -> FileName -> FileName
encodeWhite :: FilePath -> String
encodeWhite
translates whitespace in filenames to a darcs-specific
format (numerical representation according to ord
surrounded by
backslashes). Note that backslashes are also escaped since they are used
in the encoding.
encodeWhite "hello there" == "hello\32\there" encodeWhite "hello\there" == "hello\92\there"
decodeWhite :: String -> FilePath
decodeWhite
interprets the Darcs-specific "encoded" filenames
produced by encodeWhite
decodeWhite "hello\32\there" == "hello there" decodeWhite "hello\92\there" == "hello\there" decodeWhite "hello\there" == error "malformed filename"
isParentOrEqOf :: FileName -> FileName -> Bool
AbsolutePath
data AbsolutePath
makeAbsolute :: AbsolutePath -> FilePath -> AbsolutePath
Take an absolute path and a string representing a (possibly relative) path and combine them into an absolute path. If the second argument is already absolute, then the first argument gets ignored. This function also takes care that the result is converted to Posix convention and normalized. Also, parent directories ("..") at the front of the string argument get canceled out against trailing directory parts of the absolute path argument.
Regarding the last point, someone more familiar with how these functions are used should verify that this is indeed necessary or at least useful.
ioAbsolute :: FilePath -> IO AbsolutePath
Interpret a possibly relative path wrt the current working directory.
The root directory as an absolute path.
AbsolutePathOrStd
data AbsolutePathOrStd
This is for situations where a string (e.g. a command line argument) may take the value "-" to mean stdin or stdout (which one depends on context) instead of a normal file path.
useAbsoluteOrStd :: (AbsolutePath -> a) -> a -> AbsolutePathOrStd -> a
Execute either the first or the second argument action, depending on
whether the given path is an AbsolutePath
or stdin/stdout.
AbsoluteOrRemotePath
data AbsoluteOrRemotePath
isRemote :: AbsoluteOrRemotePath -> Bool
SubPath
data SubPath
Paths which are relative to the local darcs repository and normalized. Note: These are understood not to have the dot in front.
Instances
makeSubPathOf :: AbsolutePath -> AbsolutePath -> Maybe SubPath
Make the second path relative to the first, if possible
simpleSubPath :: FilePath -> Maybe SubPath
isSubPathOf :: SubPath -> SubPath -> Bool
floatSubPath :: SubPath -> AnchoredPath
Transform a SubPath into an AnchoredPath.
Miscellaneous
class FilePathOrURL a where
Instances
class FilePathOrURL a => FilePathLike a where
Methods
toFilePath :: a -> FilePath
Instances
FilePathLike AbsolutePath | |
FilePathLike SubPath | |
FilePathLike FileName | |
CharLike c => FilePathLike [c] |
setCurrentDirectory :: FilePathLike p => p -> IO ()
getUniquePathName :: Bool -> (FilePath -> String) -> (Int -> FilePath) -> IO FilePath
Iteratively tries find first non-existing path generated by buildName, it feeds to buildName the number starting with -1. When it generates non-existing path and it isn't first, it displays the message created with buildMsg. Usually used for generation of the name like path_number when path already exist (e.g. darcs.net_0).
doesPathExist :: FilePath -> IO Bool
Check for malicious paths
isMaliciousPath :: String -> Bool
What is a malicious path?
A spoofed path is a malicious path.
- Darcs only creates explicitly relative paths (beginning with
"./"
), so any not explicitly relative path is surely spoofed. - Darcs normalizes paths so they never contain
"/../"
, so paths with"/../"
are surely spoofed.
A path to a darcs repository's meta data can modify "trusted" patches or
change safety defaults in that repository, so we check for paths
containing "/_darcs/"
which is the entry to darcs meta data.
To do?
- How about get repositories?
- Would it be worth adding a --semi-safe-paths option for allowing changes to certain preference files (_darcs/prefs/) in sub repositories'?
isMaliciousSubPath :: String -> Bool
Warning : this is less rigorous than isMaliciousPath but it's to allow for subpath representations that don't start with ./
Tree filtering.
filterFilePaths :: [FilePath] -> AnchoredPath -> t -> Bool
Same as filterPath
, but for ordinary FilePath
s (as opposed to
AnchoredPath).
filterPaths :: [AnchoredPath] -> AnchoredPath -> t -> Bool
Construct a filter from a list of AnchoredPaths, that will accept any path that is either a parent or a child of any of the listed paths, and discard everything else.