path (ex_stdlib v0.3.0)

View Source

Path 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

path/0

-type path() :: string() | binary().

path_type/0

-type path_type() :: absolute | relative | volumerelative.

relative_to_opt/0

-type relative_to_opt() :: force | {force, boolean()}.

Functions

absname(Path)

-spec absname(path()) -> path().

Converts the given path to an absolute one, relative to the current working directory. Unlike expand/1, . and .. are not expanded.

absname(Path, Dir)

-spec absname(path(), path()) -> path().

Converts the given path to an absolute one, relative to Dir.

basename(Path)

-spec basename(path()) -> path().

Returns the last component of the path.

basename(Path, Ext)

-spec basename(path(), path()) -> path().

Returns the last component of the path with the given extension removed.

dirname(Path)

-spec dirname(path()) -> path().

Returns the directory part of the given path.

expand(Path)

-spec expand(path()) -> 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.

expand(Path, RelativeTo)

-spec expand(path(), path()) -> path().

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.

extname(Path)

-spec extname(path()) -> path().

Returns the extension of the last component of the path, including the dot, or an empty path if there is none.

join(Rest)

-spec join([path()]) -> path().

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.

join(Left, Right)

-spec join(path(), path()) -> path().

Joins two paths. See join/1.

relative(Path)

-spec relative(path()) -> path().

Forces the path to be relative by removing its root.

path:relative("/usr/local/bin") is "usr/local/bin".

relative_to(Path, Cwd)

-spec relative_to(path(), path()) -> path().

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").

relative_to(Path, Cwd, Opts)

-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".

relative_to_cwd(Path)

-spec relative_to_cwd(path()) -> path().

Returns the path relative to the current working directory.

relative_to_cwd(Path, Opts)

-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.

rootname(Path)

-spec rootname(path()) -> path().

Returns the path with the extension removed.

rootname(Path, Ext)

-spec rootname(path(), path()) -> path().

Returns the path with the given extension removed, if it matches.

safe_join(Left, Right)

-spec safe_join(path(), path()) -> {ok, path()} | error.

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").

safe_relative(Path)

-spec safe_relative(path()) -> {ok, path()} | error.

Returns a relative path, sanitized against directory-traversal attacks, relative to the current working directory.

safe_relative(Path, RelativeTo)

-spec safe_relative(path(), path()) -> {ok, path()} | error.

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.

split(Path)

-spec split(path()) -> [path()].

Splits a path into its components. An empty path returns [].

type(Path)

-spec type(path()) -> path_type().

Returns the type of the given path: absolute, relative or volumerelative (Windows only).

wildcard(Pattern)

-spec wildcard(path()) -> [string()].

Returns a list of files matching the given wildcard pattern.

wildcard(Pattern, Dir)

-spec wildcard(path(), path()) -> [string()].

Returns a list of files matching the given wildcard pattern in the given directory.