path (ex_stdlib v0.3.0)
View SourcePath manipulation.
This module provides functions for manipulating file system paths, inspired by Elixir's Path module. Paths may be given as strings or binaries; like the filename module, functions return the same type they were given (a binary for a binary path, a string otherwise).
Examples:
<<"/foo/baz">> = path:expand(<<"/foo/bar/../baz">>),
"path/to/file" = path:join(["path", "to", "file"]),
"file.txt" = path:basename("/path/to/file.txt"),
{ok, "a/c"} = path:safe_join("a", "b/../c").Functions that expand . and .. (expand/1, relative_to/2...) do not touch the file system, so they assume there are no symlinks between the paths. Use safe_relative/2 when the path comes from an untrusted source.
Summary
Functions
Converts the given path to an absolute one, relative to the current working directory. Unlike expand/1, . and .. are not expanded.
Converts the given path to an absolute one, relative to Dir.
Returns the last component of the path.
Returns the last component of the path with the given extension removed.
Returns the directory part of the given path.
Converts the path to an absolute one, expanding any . and .. components and a leading ~. A relative path is expanded against the current working directory.
Expands the path relative to RelativeTo, expanding any . and .. components and a leading ~. If the path is absolute, RelativeTo is ignored. RelativeTo is itself expanded first.
Returns the extension of the last component of the path, including the dot, or an empty path if there is none.
Joins a list of paths.
Joins two paths. See join/1.
Forces the path to be relative by removing its root.
Returns the direct relative path from Path in relation to Cwd.
Like relative_to/2. With the force option, absolute paths are traversed up with .. so that a relative path is always returned: path:relative_to("/usr/foo", "/usr/local", [force]) is "../foo".
Returns the path relative to the current working directory.
Returns the path relative to the current working directory, with the same options as relative_to/3. If the working directory cannot be retrieved, the path is returned unchanged.
Returns the path with the extension removed.
Returns the path with the given extension removed, if it matches.
Safely joins two paths.
Returns a relative path, sanitized against directory-traversal attacks, relative to the current working directory.
Returns a relative path that is protected from directory-traversal attacks.
Splits a path into its components. An empty path returns [].
Returns the type of the given path: absolute, relative or volumerelative (Windows only).
Returns a list of files matching the given wildcard pattern.
Returns a list of files matching the given wildcard pattern in the given directory.
Types
Functions
Converts the given path to an absolute one, relative to the current working directory. Unlike expand/1, . and .. are not expanded.
Converts the given path to an absolute one, relative to Dir.
Returns the last component of the path.
Returns the last component of the path with the given extension removed.
Returns the directory part of the given path.
Converts the path to an absolute one, expanding any . and .. components and a leading ~. A relative path is expanded against the current working directory.
Environment variables are not expanded.
Expands the path relative to RelativeTo, expanding any . and .. components and a leading ~. If the path is absolute, RelativeTo is ignored. RelativeTo is itself expanded first.
Returns the extension of the last component of the path, including the dot, or an empty path if there is none.
Joins a list of paths.
Like Elixir (and unlike filename:join/1), an absolute path on the right does not discard what is on its left: path:join(["a", "/b"]) is "a/b". The result is a binary if the first element is a binary.
Joins two paths. See join/1.
Forces the path to be relative by removing its root.
path:relative("/usr/local/bin") is "usr/local/bin".
Returns the direct relative path from Path in relation to Cwd.
. and .. are expanded. If both paths are absolute, a relative path is returned when Path is under Cwd, otherwise Path is returned unchanged. If both are relative, a relative path is always returned. If only one is absolute, Path is returned with dots expanded.
"foo" = path:relative_to("/usr/local/foo", "/usr/local"),
"/usr/local/foo" = path:relative_to("/usr/local/foo", "/etc"),
"../foo/bar" = path:relative_to("tmp/foo/bar", "tmp/bat").
-spec relative_to(path(), path(), [relative_to_opt()]) -> path().
Like relative_to/2. With the force option, absolute paths are traversed up with .. so that a relative path is always returned: path:relative_to("/usr/foo", "/usr/local", [force]) is "../foo".
Returns the path relative to the current working directory.
-spec relative_to_cwd(path(), [relative_to_opt()]) -> path().
Returns the path relative to the current working directory, with the same options as relative_to/3. If the working directory cannot be retrieved, the path is returned unchanged.
Returns the path with the extension removed.
Returns the path with the given extension removed, if it matches.
Safely joins two paths.
Returns {ok, Path} if Right is safe to append to Left (see safe_relative/2), error otherwise.
{ok, "foo/bar"} = path:safe_join("foo", "bar"),
error = path:safe_join("foo", "../bar"),
error = path:safe_join("foo", "/bar").
Returns a relative path, sanitized against directory-traversal attacks, relative to the current working directory.
Returns a relative path that is protected from directory-traversal attacks.
. and .. components are expanded. Returns error if the path is absolute, if a .. would go above RelativeTo, or if a symbolic link in the path points above RelativeTo. This check is subject to time-of-check/time-of-use races if an attacker can change the file system.
Splits a path into its components. An empty path returns [].
Returns the type of the given path: absolute, relative or volumerelative (Windows only).
Returns a list of files matching the given wildcard pattern.
Returns a list of files matching the given wildcard pattern in the given directory.