Streamly.Internal.FileSystem.PosixPath
This module implements a PosixPath type representing a file system path for
Posix operating systems. The only assumption about the encoding of the
path is that it maps the characters /
and .
to Word8
representing their ASCII values. Operations are provided to encode and
decode using UTF-8 encoding.
Type
A type representing file system paths on Posix.
A PosixPath is validated before construction unless unsafe constructors are
used to create it. For validations performed by the safe construction
methods see the fromChars
function.
Note that in some cases the file system may perform unicode normalization on paths (e.g. Apple HFS), it may cause surprising results as the path used by the user may not have the same bytes as later returned by the file system.
Instances
Conversions
class IsPath a b where Source #
If the type a b
is a member of IsPath
it means we know how to convert
the type b
to and from the base type a
.
Methods
unsafeFromPath :: a -> b Source #
Like fromPath
but does not check the properties of Path
. The user
is responsible to maintain the invariants enforced by the type b
otherwise surprising behavior may result.
This operation provides performance and simplicity when we know that the properties of the path are already verified, for example, when we get the path from the file system or from the OS APIs.
fromPath :: MonadThrow m => a -> m b Source #
Convert a base path type to other forms of well-typed paths. It may fail if the path does not satisfy the properties of the target type.
Convert a well-typed path to the base path type. Never fails.
Instances
adapt :: (MonadThrow m, IsPath PosixPath a, IsPath PosixPath b) => a -> m b Source #
Convert a path type to another path type. This operation may fail with a
PathException
when converting a less restrictive path type to a more
restrictive one. This can be used to upgrade or downgrade type safety.
Validation
validatePath :: MonadThrow m => PosixPath -> m () Source #
validatePath' :: MonadThrow m => PosixPath -> m () Source #
isValidPath :: PosixPath -> Bool Source #
Construction
fromChunk :: (MonadThrow m, IsPath PosixPath a) => Array Word8 -> m a Source #
See fromChars
for failure cases.
unsafeFromChunk :: IsPath PosixPath a => Array Word8 -> a Source #
Unsafe: The user is responsible to make sure that the cases mentioned in PosixPath are satisfied.
fromChars :: (MonadThrow m, IsPath PosixPath a) => Stream Identity Char -> m a Source #
Encode a Unicode string to PosixPath using strict UTF-8 encoding. Fails with
InvalidPath
exception if:
- the stream is empty, should have at least one char
- the stream contains null characters
- the stream contains invalid unicode characters
fromString :: (MonadThrow m, IsPath PosixPath a) => [Char] -> m a Source #
See fromChars.
>>>
fromString = Path.fromChars . Stream.fromList
Statically Verified String Literals
Quasiquoters.
path :: QuasiQuoter Source #
Generates a PosixPath type from a quoted literal.
>>>
Path.toString ([path|/usr/bin|] :: PosixPath)
"/usr/bin"
Statically Verified Strings
Template Haskell expression splices.
Elimination
toChars :: (Monad m, IsPath PosixPath a) => a -> Stream m Char Source #
Decode the path to a stream of Unicode chars using strict UTF-8 decoding.
toChars_ :: (Monad m, IsPath PosixPath a) => a -> Stream m Char Source #
Decode the path to a stream of Unicode chars using lax UTF-8 decoding.
toString :: IsPath PosixPath a => a -> [Char] Source #
Decode the path to a Unicode string using strict UTF-8 decoding.
toString_ :: IsPath PosixPath a => a -> [Char] Source #
Decode the path to a Unicode string using strict UTF-8 decoding.
Separators
dropTrailingSeparators :: PosixPath -> PosixPath Source #
If the path is //
the result is /
. If it is a//
then the result is
a
.
Tests
isRooted :: PosixPath -> Bool Source #
A path that is attached to a root e.g. "x" or ".x" are rooted paths. "/" is considered an absolute root and "." as a dynamic root. ".." is not considered a root, "..x" or "xy" are not rooted paths.
- An absolute rooted path:
/
starting from file system root - A dynamic rooted path:
./
relative to current directory
isBranch :: PosixPath -> Bool Source #
A path that is not attached to a root e.g. a/b/c
or ../b/c
.
>>>
isBranch = not . Path.isRooted
Joining
append :: PosixPath -> PosixPath -> PosixPath Source #
Append a PosixPath to another. Fails if the second path refers to a rooted
path and not a branch. Use unsafeAppend
to avoid failure if you know it is
ok to append the path or use the typesafe Streamly.FileSystem.OS_PATH.Seg
module.
>>>
Path.toString $ Path.append [path|/usr|] [path|bin|]
"/usr/bin"
append' :: PosixPath -> PosixPath -> PosixPath Source #
A stricter version of append
which requires the first path to be a
directory like path i.e. with a trailing separator.
appendCString :: PosixPath -> CString -> IO PosixPath Source #
Append a separator and a CString to the Array.
TODO: This always appends a separator and does not perform any other checks like append.
appendCString' :: PosixPath -> CString -> IO PosixPath Source #
Append a separator and a CString to the Array.
TODO: This always appends a separator and does not perform any other checks like append.
Splitting
Equality
Options for path comparison operation. By default path comparison uses a strict criteria for equality. The following options are provided to control the strictness.
Constructors
EqCfg | |
Fields
|