HORIZON HASKELLDocslts/ghc-10.0.xfc4e5982026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 10.0.0.20260917 · lts/ghc-10.0.x · fc4e598 · 2026-10-11

Modulestreamly-core-0.3.1Haskell2010

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.

This module has APIs that are equivalent to or can emulate all or most of the filepath package APIs. It has some differences from the filepath package:

  • Empty paths are not allowed. Paths are validated before construction.

  • The default Path type itself affords considerable safety regarding the distinction of rooted or non-rooted paths, it also allows distinguishing directory and file paths.

  • It is designed to provide flexible typing to provide compile time safety for rootednon-rooted paths and filedir paths. The Path type is just part of that typed path ecosystem. Though the default Path type itself should be enough for most cases.

  • It leverages the streamly array module for most of the heavy lifting, it is a thin wrapper on top of that, improving maintainability as well as providing better performance. We can have pinned and unpinned paths, also provide lower level operations for certain cases to interact more efficinetly with low level code.

  • 2 types
  • 1 class
  • 57 values

Setup

0 declarations

To execute the code examples provided in this module in ghci, please run the following commands first.

Example8 expressions
:m:set -XQuasiQuotesimport Control.Exception (SomeException, evaluate, try)import Data.Either (Either, isLeft)import Data.Maybe (isNothing, isJust)import qualified Streamly.Data.Array as Arrayimport qualified Streamly.Data.Stream as Streamimport qualified Streamly.Unicode.Stream as Unicode

For APIs that have not been released yet.

Example2 expressions
import Streamly.Internal.FileSystem.PosixPath (PosixPath, path)import qualified Streamly.Internal.FileSystem.PosixPath as Path

Utilities:

Example1 expression
fails x = isLeft <$> (try (evaluate x) :: IO (Either SomeException String))

Type

1 declaration
newtypenewtype PosixPath
#

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.

Constructors

Instances9IsPath, …

Conversions

2 declarations
classclass IsPath a b where
#

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

    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

    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.

  • toPath :: b -> a

    Convert a well-typed path to the base path type. Never fails.

Instances18IsPath, …
valueadapt :: (MonadThrow m, IsPath PosixPath a, IsPath PosixPath b) => a -> m b
#

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.

Conversion to OsWord

2 declarations
valuecharToWord :: Char -> Word8
#

Unsafe, truncates the Char to Word8 on Posix and Word16 on Windows.

Validation

2 declarations
valuevalidatePath :: MonadThrow m => Array Word8 -> m ()
#

Checks whether the filepath is valid; i.e., whether the operating system permits such a path for listing or creating files. These validations are operating system specific and file system independent. Throws an exception with a detailed explanation if the path is invalid.

Example1 expression
isValid = isJust . Path.validatePath . Path.encodeString

Validations:

Example2 expressions
isValid ""FalseisValid "\0"False

Other than these there may be maximum path component length and maximum path length restrictions enforced by the OS as well as the filesystem which we do not validate.

Construction

8 declarations
valuefromArray :: MonadThrow m => Array Word8 -> m PosixPath
#

Convert an encoded array of Word8 into a value of type PosixPath. The path is validated using validatePath.

Each Word8 should be encoded such that:

  • The input does not contain a NUL word.

  • Values from 1-128 are assumed to be ASCII characters.

Apart from the above, there are no restrictions on the encoding.

To bypass path validation checks, use unsafeFromArray.

Throws InvalidPath if validatePath fails on the resulting path.

Like fromString but a streaming operation.

Example1 expression
fromString = Path.fromChars . Stream.fromList

We do not sanitize the path i.e. we do not remove duplicate separators, redundant . segments, trailing separators etc because that would require unnecessary checks and modifications to the path which may not be used ever for any useful purpose, it is only needed for path equality and can be done during the equality check.

Unicode normalization is not done. If normalization is needed the user can normalize it and then use the fromArray API.

valueencodeString :: [Char] -> Array Word8
#

Create an array from a path string using strict UTF-8 encoding. The path is not validated, therefore, it may not be valid according to validatePath.

Same as toArray . unsafeFromString.

Statically Verified String Literals

1 declaration

Quasiquoters.

Statically Verified Strings

1 declaration

Template Haskell expression splices.

valuepathE :: String -> Q Exp
#

Generates a Haskell expression of type PosixPath from a String. Equivalent to using fromString on the string passed.

Elimination

7 declarations
valueasCString :: PosixPath -> (CString -> IO a) -> IO a
#

Use the path as a pinned CString. Useful for using a PosixPath in system calls on Posix.

Separators

3 declarations
valueseparator :: Word8
#

The primary path separator word: / on POSIX and \ on Windows. Windows also supports / as a valid separator. Use isSeparator to check for any valid path separator.

valueisSeparator :: Word8 -> Bool
#

On POSIX, only / is a path separator, whereas on Windows both / and \ are valid separators.

Dir or non-dir paths

3 declarations

Remove all trailing path separators from the given Path.

Instead of this operation you may want to use eqPath with ignoreTrailingSeparators option.

This operation is careful not to alter the semantic meaning of the path. For example, on Windows:

  • Dropping the separator from "C:/" would change the meaning of the path from referring to the root of the C: drive to the current directory on C:.

  • If a path ends with a separator immediately after a colon (e.g., "C:/"), the separator will not be removed.

If the input path is invalid, the behavior is not fully guaranteed:

  • The separator may still be dropped.

  • In some cases, dropping the separator may make an invalid path valid (e.g., "C:\\" or "C:\/").

This operation may convert a path that implicitly refers to a directory into one that does not.

Typically, if the path is dir//, the result is dir. Special cases include:

  • On POSIX: dropping from "//" yields "/".

  • On Windows: dropping from "C://" results in "C:/".

Examples:

Example2 expressions
f = Path.toString . Path.dropTrailingSeparators . Path.fromString_f "./""."
> f "//"  -- On POSIX

"/"

Returns True if the path ends with a trailing separator.

This typically indicates that the path is a directory, though this is not guaranteed in all cases.

Example:

Example1 expression
Path.hasTrailingSeparator (Path.fromString_ "foo/")True
Example1 expression
Path.hasTrailingSeparator (Path.fromString_ "foo")False

Add a trailing path separator to a path if it doesn't already have one.

Instead of this operation you may want to use eqPath with ignoreTrailingSeparators option.

This function avoids modifying the path if doing so would change its meaning or make it invalid. For example, on Windows:

  • Adding a separator to "C:" would change it from referring to the current directory on the C: drive to the root directory.

  • Adding a separator to "\" could turn it into a UNC share path, which may not be intended.

  • If the path ends with a colon (e.g., "C:"), a separator is not added.

This operation typically makes the path behave like an implicit directory path.

Path Segment Types

2 declarations
valueisRooted :: PosixPath -> Bool
#

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 "x/y" are not rooted paths.

Example1 expression
isRooted = Path.isRooted . Path.fromString_
Example4 expressions
isRooted "/"TrueisRooted "/x"TrueisRooted "."TrueisRooted "./x"True
valueisUnrooted :: PosixPath -> Bool
#

A path that is not attached to a root e.g. a/b/c or ../b/c.

Example1 expression
isUnrooted = not . Path.isRooted
Example1 expression
isUnrooted = Path.isUnrooted . Path.fromString_
Example4 expressions
isUnrooted "x"TrueisUnrooted "x/y"TrueisUnrooted ".."TrueisUnrooted "../x"True

Joining

7 declarations

Like join but does not check if the second path is rooted.

Example1 expression
f a b = Path.toString $ Path.unsafeJoin (Path.fromString_ a) (Path.fromString_ b)
Example4 expressions
f "x" "y""x/y"f "x/" "y""x/y"f "x" "/y""x/y"f "x/" "/y""x/y"

Append a separator and a CString to the Array. This is like unsafeJoin but always inserts a separator between the two paths even if the first path has a trailing separator or second path has a leading separator.

valuejoin :: PosixPath -> PosixPath -> PosixPath
#

Append a separator followed by another path to a PosixPath. Fails if the second path is a rooted path. Use unsafeJoin to avoid failure if you know it is ok to append the rooted path.

Example1 expression
f a b = Path.toString $ Path.join a b
Example3 expressions
f [path|/usr|] [path|bin|]"/usr/bin"f [path|/usr/|] [path|bin|]"/usr/bin"fails (f [path|/usr|] [path|/bin|])True

A stricter version of join which requires the first path to be a directory like path i.e. having a trailing separator.

Example1 expression
f a b = Path.toString $ Path.joinDir a b
Example1 expression
fails $ f [path|/usr|] [path|bin|]True

Join paths by path separator. Does not check if the paths being appended are rooted or branches. Note that splitting and joining may not give exactly the original path but an equivalent path.

Unimplemented

Splitting

6 declarations

Note: you can use unsafeJoin as a replacement for the joinDrive function in the filepath package.

If a path is rooted then separate the root and the remaining path, otherwise return Nothing. The non-root part is guaranteed to NOT start with a separator.

Some filepath package equivalent idioms:

Example6 expressions
splitDrive = Path.splitRootjoinDrive = Path.unsafeJointakeDrive = fmap fst . Path.splitRootdropDrive x = Path.splitRoot x >>= sndhasDrive = isJust . Path.splitRootisDrive = isNothing . dropDrive
Example2 expressions
toList (a,b) = (Path.toString a, fmap Path.toString b)split = fmap toList . Path.splitRoot . Path.fromString_
Example1 expression
split "/"Just ("/",Nothing)
Example1 expression
split "."Just (".",Nothing)
Example1 expression
split "./"Just ("./",Nothing)
Example1 expression
split "/home"Just ("/",Just "home")
Example1 expression
split "//"Just ("//",Nothing)
Example1 expression
split "./home"Just ("./",Just "home")
Example1 expression
split "home"Nothing
valuesplitPath :: Monad m => PosixPath -> Stream m PosixPath
#

Split the path components keeping separators between path components attached to the dir part. Redundant separators are removed, only the first one is kept. Separators are not added either e.g. "." and ".." may not have trailing separators if the original path does not.

Example1 expression
split = Stream.toList . fmap Path.toString . Path.splitPath . Path.fromString_
Example1 expression
split "."["."]
Example1 expression
split "././"["./"]
Example1 expression
split "./a/b/."["./","a/","b/"]
Example1 expression
split ".."[".."]
Example1 expression
split "../"["../"]
Example1 expression
split "a/.."["a/",".."]
Example1 expression
split "/"["/"]
Example1 expression
split "//"["/"]
Example1 expression
split "/x"["/","x"]
Example1 expression
split "/./x/"["/","x/"]
Example1 expression
split "/x/./y"["/","x/","y"]
Example1 expression
split "/x/../y"["/","x/","../","y"]
Example1 expression
split "/x///y"["/","x/","y"]
Example1 expression
split "/x/\\y"["/","x/","\\y"]
valuesplitPath_ :: Monad m => PosixPath -> Stream m PosixPath
#

Split a path into components separated by the path separator. "." components in the path are ignored except when in the leading position. Trailing separators in non-root components are dropped.

Example1 expression
split = Stream.toList . fmap Path.toString . Path.splitPath_ . Path.fromString_
Example1 expression
split "."["."]
Example1 expression
split "././"["."]
Example1 expression
split ".//"["."]
Example1 expression
split "//"["/"]
Example1 expression
split "//x/y/"["/","x","y"]
Example1 expression
split "./a"[".","a"]
Example1 expression
split "a/."["a"]
Example1 expression
split "/"["/"]
Example1 expression
split "/x"["/","x"]
Example1 expression
split "/./x/"["/","x"]
Example1 expression
split "/x/./y"["/","x","y"]
Example1 expression
split "/x/../y"["/","x","..","y"]
Example1 expression
split "/x///y"["/","x","y"]
Example1 expression
split "/x/\\y"["/","x","\\y"]

If the path does not look like a directory then return Just (Maybe dir, file) otherwise return Nothing. The path is not a directory if:

  • the path does not end with a separator

  • the path does not end with a . or .. component

Splits a single component path into Just (Nothing, path) if it does not look like a dir.

Example2 expressions
toList (a,b) = (fmap Path.toString a, Path.toString b)split = fmap toList . Path.splitFile . Path.fromString_
Example1 expression
split "/"Nothing
Example1 expression
split "."Nothing
Example1 expression
split "/."Nothing
Example1 expression
split ".."Nothing
> split "//" -- Posix

Nothing

Example1 expression
split "/home"Just (Just "/","home")
Example1 expression
split "./home"Just (Just "./","home")
Example1 expression
split "home"Just (Nothing,"home")
Example1 expression
split "x/"Nothing
Example1 expression
split "x/y"Just (Just "x/","y")
Example1 expression
split "x//y"Just (Just "x//","y")
Example1 expression
split "x/./y"Just (Just "x/./","y")

Split the path into the last component and rest of the path. Treats the entire root or share name, if present, as the first component.

Example2 expressions
basename = snd . Path.splitLast -- Posix basenamedirname = fst . Path.splitLast -- Posix dirname

Unimplemented

Extension

Returns Just(filename, extension) if an extension is present otherwise returns Nothing.

A file name is considered to have an extension if the file name can be split into a non-empty filename followed by the extension separator "." followed by a non-empty extension with at least one character in addition to the extension separator. The shortest suffix obtained by this rule, starting with the extension separator, is returned as the extension and the remaining prefix part as the filename.

A directory name does not have an extension.

If you want a splitExtensions, you can use splitExtension until the extension returned is Nothing. dropExtensions, isExtensionOf can be implemented similarly.

Example2 expressions
toList (a,b) = (Path.toString a, Path.toString b)split = fmap toList . Path.splitExtension . Path.fromString_
Example1 expression
split "/"Nothing
Example1 expression
split "."Nothing
Example1 expression
split ".."Nothing
Example1 expression
split "x"Nothing
Example1 expression
split "/x"Nothing
Example1 expression
split "x/"Nothing
Example1 expression
split "./x"Nothing
Example1 expression
split "x/."Nothing
Example1 expression
split "x/y."Nothing
Example1 expression
split "/x.y"Just ("/x",".y")
Example1 expression
split "/x.y."Nothing
Example1 expression
split "/x.y.."Nothing
Example1 expression
split "x/.y"Nothing
Example1 expression
split ".x"Nothing
Example1 expression
split "x."Nothing
Example1 expression
split ".x.y"Just (".x",".y")
Example1 expression
split "x/y.z"Just ("x/y",".z")
Example1 expression
split "x.y.z"Just ("x.y",".z")
Example1 expression
split "x..y"Just ("x.",".y")
Example1 expression
split "..."Nothing
Example1 expression
split "..x"Just (".",".x")
Example1 expression
split "...x"Just ("..",".x")
Example1 expression
split "x/y.z/"Nothing
Example1 expression
split "x/y"Nothing

Drop the extension of a file if it has one.

Example1 expression
dropExtension p = maybe p fst $ Path.splitExtension p
Example1 expression
Path.toString $ Path.dropExtension [path|/home/user/file.txt|]"/home/user/file"

Add an extension to a file path. If a non-empty extension does not start with a leading dot then a dot is inserted, otherwise the extension is concatenated with the path.

It is an error to add an extension to a path with a trailing separator.

Unimplemented

Path View

Extracts the file name component (with extension) from a PosixPath, if present.

Example2 expressions
takeFileName = fmap snd . Path.splitFilereplaceDirectory p x = fmap (flip Path.join x) (takeFileName p)
Example2 expressions
fmap Path.toString $ Path.takeFileName [path|/home/user/file.txt|]Just "file.txt"fmap Path.toString $ Path.takeFileName [path|/home/user/|]Nothing

See splitFile for more examples.

Returns the parent directory of the given PosixPath, if any.

Example2 expressions
takeDirectory x = Path.splitFile x >>= fstreplaceFileName p x = fmap (flip Path.join x) (takeDirectory p)

To get an equivalent to takeDirectory from filepath use dropTrailingSeparators on the result.

Example2 expressions
fmap Path.toString $ Path.takeDirectory [path|/home/user/file.txt|]Just "/home/user/"fmap Path.toString $ Path.takeDirectory [path|file.txt|]Nothing

Take the extension of a file if it has one.

Example2 expressions
takeExtension = fmap snd . Path.splitExtensionhasExtension = isJust . Path.splitExtension
Example1 expression
fmap Path.toString $ Path.takeExtension [path|/home/user/file.txt|]Just ".txt"

See splitExtension for more examples.

Extracts the file name dropping the extension, if any, from a PosixPath.

Example1 expression
takeFileBase = fmap Path.dropExtension . Path.takeFileName
Example4 expressions
fmap Path.toString $ Path.takeFileBase [path|/home/user/file.txt|]Just "file"fmap Path.toString $ Path.takeFileBase [path|/home/user/file|]Just "file"fmap Path.toString $ Path.takeFileBase [path|/home/user/.txt|]Just ".txt"fmap Path.toString $ Path.takeFileBase [path|/home/user/|]Nothing

See splitFile for more examples.

Equality

7 declarations
datadata EqCfg
#

Options for path comparison operation. By default path comparison uses a strict criteria for equality. The following options are provided to control the strictness.

The default configuration is as follows:

Example1 expression
:{defaultMod = ignoreTrailingSeparators False           . ignoreCase False           . allowRelativeEquality False:}

When set to False (default):

Example2 expressions
cfg = Path.ignoreTrailingSeparators Falseeq a b = Path.eqPath cfg (Path.fromString_ a) (Path.fromString_ b)
Example1 expression
eq "x/"  "x"False

When set to True:

Example2 expressions
cfg = Path.ignoreTrailingSeparators Trueeq a b = Path.eqPath cfg (Path.fromString_ a) (Path.fromString_ b)
Example1 expression
eq "x/"  "x"True

Default: False

Allow relative paths to be treated as equal. When this is False relative paths will never match even if they are literally equal e.g. "./x" will not match "./x" because the meaning of "." in both cases could be different depending on what the user meant by current directory in each case.

When set to False (default):

Example5 expressions
cfg = Path.allowRelativeEquality Falseeq a b = Path.eqPath cfg (Path.fromString_ a) (Path.fromString_ b)eq "."  "."Falseeq "./x"  "./x"Falseeq "./x"  "x"False

When set to False (default):

Example5 expressions
cfg = Path.allowRelativeEquality Trueeq a b = Path.eqPath cfg (Path.fromString_ a) (Path.fromString_ b)eq "."  "."Trueeq "./x"  "./x"Trueeq "./x"  "x"True
Example1 expression
eq "./x"  "././x"True

Default: False

valueeqPath :: (EqCfg -> EqCfg) -> PosixPath -> PosixPath -> Bool
#

Checks whether two paths are logically equal. This function takes a configuration modifier to customize the notion of equality. For using the default configuration pass id as the modifier. For details about the defaults, see EqCfg.

eqPath performs some normalizations on the paths before comparing them, specifically it drops redundant path separators between path segments and redundant "/./" components between segments.

Default config options use strict equality, for strict equality both the paths must be absolute or both must be path segments without a leading root component (e.g. x/y). Also, both must be files or both must be directories.

In addition to the default config options, the following equality semantics are used:

  • An absolute path and a path relative to "." may be equal depending on the meaning of ".", however this routine treats them as unequal, it does not resolve the "." to a concrete path.

  • Two paths having ".." components may be equal after processing the ".." components even if we determined them to be unequal. However, if we determined them to be equal then they must be equal.

Using default config with case sensitive comparision, if eqPath returns equal then the paths are definitely equal, if it returns unequal then the paths may still be equal under some relaxed equality criterion.

Example1 expression
:{ eq a b = Path.eqPath id (Path.fromString_ a) (Path.fromString_ b):}
Example2 expressions
eq "x"  "x"Trueeq ".."  ".."True

Non-trailing separators and non-leading "." segments are ignored:

Example4 expressions
eq "/x"  "//x"Trueeq "x//y"  "x/y"Trueeq "x/./y"  "x/y"Trueeq "x/y/."  "x/y"True

Leading dot, relative paths are not equal by default:

Example3 expressions
eq "."  "."Falseeq "./x"  "./x"Falseeq "./x"  "x"False

Trailing separators are significant by default:

Example1 expression
eq "x/"  "x"False

Match is case sensitive by default:

Example1 expression
eq "x"  "X"False

Check two paths for byte level equality. This is the most strict path equality check.

Example1 expression
eqPath a b = Path.eqPathBytes (Path.fromString_ a) (Path.fromString_ b)
Example1 expression
eqPath "x//y"  "x//y"True
Example1 expression
eqPath "x//y"  "x/y"False
Example1 expression
eqPath "x/./y"  "x/y"False
Example1 expression
eqPath "x\\y" "x/y"False
Example1 expression
eqPath "./file"  "file"False
Example1 expression
eqPath "file/"  "file"False
valuenormalize :: EqCfg -> PosixPath -> PosixPath
#

Convert the path to an equivalent but standard format for reliable comparison. This can be implemented if required. Usually, the equality operations should be enough and this may not be needed.

Unimplemented