Module B0_std.Os.Dir

module Dir: sig .. end

Directory operations.

This module operates on directories, most functions error if they are applied to other file kinds.


Existence

val exists : B0_std.Fpath.t -> (bool, string) Stdlib.result

exists dir is Ok true if dir is a directory in the file system and Ok false otherwise. Symbolic links are followed.

val must_exist : B0_std.Fpath.t -> (unit, string) Stdlib.result

must_exist dir is Ok () if dir is a directory in the file system and an error otherwise. Symbolic links are followed.

Creating

val create : ?mode:int -> make_path:bool -> B0_std.Fpath.t -> (bool, string) Stdlib.result

create ~mode ~make_path dir creates the directory dir.

The result is:

Contents

val fold : ?rel:bool ->
?dotfiles:bool ->
?follow_symlinks:bool ->
?prune:(Unix.stats -> string -> B0_std.Fpath.t -> bool) ->
recurse:bool ->
(Unix.stats -> string -> B0_std.Fpath.t -> 'a -> 'a) ->
B0_std.Fpath.t -> 'a -> ('a, string) Stdlib.result

fold ~rel ~dotfiles ~follow_symlinks ~prune ~recurse f dir
        acc
folds f over the contents of dir starting with acc. If dir does not exist the function errors.

Fold order. The fold order is generally undefined. The only guarantee is that directory paths are folded over before their content.

Warning. Given the raciness of the POSIX file API it cannot be guaranteed that really all existing files will be folded over in presence of other processes.

val fold_files : ?rel:bool ->
?dotfiles:bool ->
?follow_symlinks:bool ->
?prune:(Unix.stats -> string -> B0_std.Fpath.t -> bool) ->
recurse:bool ->
(Unix.stats -> string -> B0_std.Fpath.t -> 'a -> 'a) ->
B0_std.Fpath.t -> 'a -> ('a, string) Stdlib.result

fold_files is like B0_std.Os.Dir.fold but f is only applied to non-directory files.

val fold_dirs : ?rel:bool ->
?dotfiles:bool ->
?follow_symlinks:bool ->
?prune:(Unix.stats -> string -> B0_std.Fpath.t -> bool) ->
recurse:bool ->
(Unix.stats -> string -> B0_std.Fpath.t -> 'a -> 'a) ->
B0_std.Fpath.t -> 'a -> ('a, string) Stdlib.result

fold_dirs is like B0_std.Os.Dir.fold but f is only applied to directory files.

val path_list : Unix.stats ->
string -> B0_std.Fpath.t -> B0_std.Fpath.t list -> B0_std.Fpath.t list

path_list is a folding function to get a (reverse w.r.t. folding order) list of paths.

Copying

val copy : ?rel:bool ->
?atomic:bool ->
?allow_hardlinks:bool ->
?follow_symlinks:bool ->
?prune:(Unix.stats -> string -> B0_std.Fpath.t -> bool) ->
make_path:bool ->
recurse:bool ->
src:B0_std.Fpath.t -> B0_std.Fpath.t -> (unit, string) Stdlib.result

copy ~rel ~atomic ~prune ~follow_symlinks ~make_path ~recurse
        ~src dst
copies the directory src to dst. File modes of src and its contents are preserved in dst. The function errors if dst exists.

Current working directory (cwd)

val cwd : unit -> (B0_std.Fpath.t, string) Stdlib.result

cwd () is the current working directory. The resulting path is guaranteed to be absolute.

val set_cwd : B0_std.Fpath.t -> (unit, string) Stdlib.result

set_cwd dir sets the current working directory to dir.

val with_cwd : B0_std.Fpath.t -> (unit -> 'a) -> ('a, string) Stdlib.result

with_cwd dir f is f () with the current working directory bound to dir. After the function returns the current working directory is back to its initial value.

Default temporary directory

val default_tmp : unit -> B0_std.Fpath.t

default_tmp () is a default directory that can be used as a default directory for creating temporary files and directories. If B0_std.Os.Dir.set_default_tmp hasn't been called this is:

val set_default_tmp : B0_std.Fpath.t -> unit

set_default_tmp p sets the value returned by B0_std.Os.Dir.default_tmp to p.

Temporary directories

val with_tmp : ?mode:int ->
?make_path:bool ->
?dir:B0_std.Fpath.t ->
?name:B0_std.Os.Path.tmp_name ->
(B0_std.Fpath.t -> 'a) -> ('a, string) Stdlib.result

with_tmp ~mode ~make_path ~dir ~name f creates a temporary empty directory t and returns Ok (f t). After the function returns (normally or via an exception) t and its content are deleted.

val tmp : ?mode:int ->
?make_path:bool ->
?dir:B0_std.Fpath.t ->
?name:B0_std.Os.Path.tmp_name ->
unit -> (B0_std.Fpath.t, string) Stdlib.result

tmp is like B0_std.Os.Dir.with_tmp except the directory and its content is only deleted at the end of program execution if the client doesn't do it before.

Base directories

The directories returned by these functions are not guaranteed to exist.

val user : unit -> (B0_std.Fpath.t, string) Stdlib.result

user () is the home directory of the user executing the process. Determined by consulting passwd database with the user if of the process. If this fails or on Windows falls back to parse a path from the HOME environment variables.

val config : unit -> (B0_std.Fpath.t, string) Stdlib.result

config () is the directory used to store user-specific program configurations. This is in order:

  1. If set the value of XDG_CONFIG_HOME.
  2. If set and on Windows® the value of %LOCALAPPDATA%.
  3. If user () is Ok home, Fpath.(home / ".config").
val data : unit -> (B0_std.Fpath.t, string) Stdlib.result

data () is the directory used to store user-specific program data. This is in order:

  1. If set the value of XDG_DATA_HOME.
  2. If set and on Windows® the value of %LOCALAPPDATA%.
  3. If user () is Ok home, Fpath.(home / ".local" / "share").
val cache : unit -> (B0_std.Fpath.t, string) Stdlib.result

cache () is the directory used to store user-specific non-essential data. This is in order:

  1. If set the value of XDG_CACHE_HOME.
  2. If set and on Windows® the value of %TEMP%
  3. If user () is Ok home, Fpath.(home / ".cache")
val runtime : unit -> (B0_std.Fpath.t, string) Stdlib.result

runtime () is the directory used to store user-specific runtime files. This is in order:

  1. If set the value of XDG_RUNTIME_HOME.
  2. The value of B0_std.Os.Dir.default_tmp.