package eio
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=8ed5c13e6689f31c85dca5f12762d84b8cc0042a7b07d3e464df6eb4b72b3dfc
sha512=46e8f817f32c3316e7f35835a136ad177a295b3306351eb2efa2386482b0169a5b19ed2925b32da2a1f10d40f083fe3d588dd401908f9fec6e4a44cd68535204
doc/eio/Eio/Path/index.html
Module Eio.Path
Source
Accessing paths on a file-system.
A _ Path.t
represents a particular location in some filesystem. It is a pair of a base directory and a relative path from there.
Eio.Stdenv.cwd
provides access to the current working directory. For example:
let ( / ) = Eio.Path.( / )
let run dir =
Eio.Path.save ~create:(`Exclusive 0o600)
(dir / "output.txt") "the data"
let () =
Eio_main.run @@ fun env ->
run (Eio.Stdenv.cwd env)
It is normally not permitted to access anything above the base directory, even by following a symlink. The exception is Stdenv.fs
, which provides access to the whole file-system:
Eio.Path.load (fs / "/etc/passwd")
In Eio, the directory separator is always "/", even on Windows. Use native
to convert to a native path.
An OS directory FD and a path relative to it, for use with e.g. openat(2)
.
t / step
is t
with step
appended to t
's path, or replacing t
's path if step
is absolute:
(fd, "foo") / "bar" = (fd, "foo/bar")
(fd, "foo") / "/bar" = (fd, "/bar")
native t
returns a path that can be used to refer to t
with the host platform's native string-based file-system APIs, if available. This is intended for interoperability with non-Eio libraries.
This does not check for confinement (the resulting path might not be accessible via t
itself). Also, if a directory was opened with open_dir
and later renamed, this might use the old name.
Using strings as paths is not secure if components in the path can be replaced by symlinks while the path is being used. For example, if you try to write to "/home/mal/output.txt" just as mal replaces "output.txt" with a symlink to "/etc/passwd".
Like native
, but raise a suitable exception if the path is not a native path.
split t
returns Some (dir, basename)
, where basename
is the last path component in t
and dir
is t
without basename
.
dir / basename
refers to the same path as t
.
split t = None
if there is nothing to split.
For example:
split (root, "foo/bar") = Some ((root, "foo"), "bar")
split (root, "/foo/bar") = Some ((root, "/foo"), "bar")
split (root, "/foo/bar/baz") = Some ((root, "/foo/bar"), "baz")
split (root, "/foo/bar//baz/") = Some ((root, "/foo/bar"), "baz")
split (root, "bar") = Some ((root, ""), "bar")
split (root, ".") = Some ((root, ""), ".")
split (root, "") = None
split (root, "/") = None
Reading files
load t
returns the contents of the given file.
This is a convenience wrapper around with_open_in
.
open_in ~sw t
opens t
for reading.
Note: files are always opened in binary mode.
with_open_in
is like open_in
, but calls fn flow
with the new flow and closes it automatically when fn
returns (if it hasn't already been closed by then).
with_lines t fn
is a convenience function for streaming the lines of the file.
It uses Buf_read.lines
.
Writing files
save t data ~create
writes data
to t
.
This is a convenience wrapper around with_open_out
.
val open_out :
sw:Switch.t ->
?append:bool ->
create:Fs.create ->
_ t ->
File.rw_ty Resource.t
open_out ~sw t
opens t
for reading and writing.
Note: files are always opened in binary mode.
with_open_out
is like open_out
, but calls fn flow
with the new flow and closes it automatically when fn
returns (if it hasn't already been closed by then).
Directories
mkdir ~perm t
creates a new directory t
with permissions perm
.
mkdirs ~perm t
creates directory t
along with any missing ancestor directories, recursively.
All created directories get permissions perm
, but existing directories do not have their permissions changed.
open_dir ~sw t
opens t
.
This can be passed to functions to grant access only to the subtree t
.
with_open_dir
is like open_dir
, but calls fn dir
with the new directory and closes it automatically when fn
returns (if it hasn't already been closed by then).
read_dir t
reads directory entries for t
.
The entries are sorted using String.compare
.
Note: The special Unix entries "." and ".." are not included in the results.
Metadata
stat ~follow t
returns metadata about the file t
.
If t
is a symlink, the information returned is about the target if follow = true
, otherwise it is about the link itself.
kind ~follow t
is the type of t
, or `Not_found
if it doesn't exist.
is_file t
is true
if t
is a regular file, and false
if it doesn't exist or has a different type.
is_file t
is kind ~follow:true t = `Regular_file
.
is_directory t
is true
if t
is a directory, and false
if it doesn't exist or has a different type.
is_directory t
is kind ~follow:true t = `Directory
.
Other
unlink t
removes directory entry t
.
Note: this cannot be used to unlink directories. Use rmdir
for directories.
rmdir t
removes directory entry t
. This only works when the entry is itself a directory.
Note: this usually requires the directory to be empty.
rmtree t
removes t
(and its contents, recursively, if it's a directory).
rename old_t new_t
atomically unlinks old_t
and links it as new_t
.
If new_t
already exists, it is atomically replaced.
symlink ~link_to t
creates a symbolic link t
to link_to
.
t
is the symlink that is created and link_to
is the name used in the link. For example, this creates a "current" symlink pointing at "version-1.0":
Eio.Path.symlink (dir / "current") ~link_to:"version-1.0"