Module Bos.OS.Dir

module Dir: sig .. end
Directory operations.


Existence, creation, deletion and contents


val exists : Fpath.t -> (bool, [> Rresult.R.msg ]) Bos.OS.result
exists dir is true if dir is a directory in the file system and false otherwise. Symbolic links are followed.
val must_exist : Fpath.t -> (Fpath.t, [> Rresult.R.msg ]) Bos.OS.result
must_exist dir is Ok dir if dir is a directory in the file system and an error otherwise. Symbolic links are followed.
val create : ?path:bool ->
?mode:int -> Fpath.t -> (bool, [> Rresult.R.msg ]) Bos.OS.result
create ~path ~mode dir creates, if needed, the directory dir with file permission mode (defaults 0o755 readable and traversable by everyone, writeable by the user). If path is true (default) intermediate directories are created with the same mode, otherwise missing intermediate directories lead to an error. The result is false if dir already exists.

Note. The mode of existing directories, including dir if this is the case is kept unchanged.

val delete : ?must_exist:bool ->
?recurse:bool -> Fpath.t -> (unit, [> Rresult.R.msg ]) Bos.OS.result
delete ~must_exist ~recurse dir deletes the directory dir. If must_exist is true (defaults to false) an error is returned if dir doesn't exist. If recurse is true (default to false) no error occurs if the directory is non-empty: its contents is recursively deleted first.
val contents : ?dotfiles:bool ->
?rel:bool -> Fpath.t -> (Fpath.t list, [> Rresult.R.msg ]) Bos.OS.result
contents ~dotfiles ~rel dir is the list of directories and files in dir. If rel is true (defaults to false) the resulting paths are relative to dir, otherwise they have dir prepended. See also Bos.OS.Dir.fold_contents. If dotfiles is false (default) elements that start with a . are omitted.
val fold_contents : ?err:'b Bos.OS.Path.fold_error ->
?dotfiles:bool ->
?elements:Bos.OS.Path.elements ->
?traverse:Bos.OS.Path.traverse ->
(Fpath.t -> 'a -> 'a) ->
'a -> Fpath.t -> ('a, [> Rresult.R.msg ]) Bos.OS.result
contents_fold err dotfiles elements traverse f acc d is:
contents d >>= Path.fold err dotfiles elements traverse f acc
For more details see Bos.OS.Path.fold.

User and current working directory


val user : unit -> (Fpath.t, [> Rresult.R.msg ]) Bos.OS.result
user () is the home directory of the user executing the process. Determined by consulting the passwd database with the user id of the process. If this fails or on Windows falls back to parse a path from the HOME environment variable.
val current : unit -> (Fpath.t, [> Rresult.R.msg ]) Bos.OS.result
current () is the current working directory. The resulting path is guaranteed to be absolute.
val set_current : Fpath.t -> (unit, [> Rresult.R.msg ]) Bos.OS.result
set_current dir sets the current working directory to dir.
val with_current : Fpath.t -> ('a -> 'b) -> 'a -> ('b, [> Rresult.R.msg ]) Bos.OS.result
with_current dir f v is f v with the current working directory bound to dir. After the function returns the current working directory is back to its initial value.

Temporary directories


type tmp_name_pat = (string -> string, Format.formatter, unit, string) Pervasives.format4 
The type for temporary directory name patterns. The string format is replaced by random characters.
val tmp : ?mode:int ->
?dir:Fpath.t ->
tmp_name_pat -> (Fpath.t, [> Rresult.R.msg ]) Bos.OS.result
tmp mode dir pat is a new empty directory in dir (defaults to Bos.OS.Dir.default_tmp) named according to pat and created with permissions mode (defaults to 0o700 only readable and writable by the user). The directory path and its content is deleted at the end of program execution using a Pervasives.at_exit handler.
val with_tmp : ?mode:int ->
?dir:Fpath.t ->
tmp_name_pat ->
(Fpath.t -> 'a -> 'b) -> 'a -> ('b, [> Rresult.R.msg ]) Bos.OS.result
with_tmp mode dir pat f v is a new empty directory in dir (defaults to Bos.OS.Dir.default_tmp) named according to pat and created with permissions mode (defaults to 0o700 only readable and writable by the user). Returns the value of f tmpdir v with tmpdir the directory path. After the function returns the directory path tmpdir and its content is deleted.

Default temporary directory


val default_tmp : unit -> Fpath.t
default_tmp () is the directory used as a default value for creating temporary files and directories. If Bos.OS.Dir.set_default_tmp hasn't been called this is:
val set_default_tmp : Fpath.t -> unit
set_default_tmp p sets the value returned by Bos.OS.Dir.default_tmp to p.