Module Os.Dir
Directory operations.
This module operates on directories, most functions error if they are applied to other file kinds.
Existence
val exists : Fpath.t -> (bool, string) Stdlib.resultexists dirisOk trueifdiris a directory in the file system andOk falseotherwise. Symbolic links are followed.
val must_exist : Fpath.t -> (unit, string) Stdlib.resultmust_exist dirisOk ()ifdiris a directory in the file system and an error otherwise. Symbolic links are followed.
Creating
val create : ?mode:int -> make_path:bool -> Fpath.t -> (bool, string) Stdlib.resultcreate ~mode ~make_path dircreates the directorydir.modeare the file permission ofdir. They default to0o755(readable and traversable by everyone, writeable by the user).- If
make_pathistrueand the parent directory ofpdoes not exist the whole path to the parent is created as needed with permission0o755(readable and traversable by everyone, writable by the user)
The result is:
Ok trueifdirdid not exist and was created.Ok falseifdirdid exist as (possibly a symlink to) a directory. In this case the mode ofdirand any other directory is kept unchanged.Error _otherwise and in particular ifdirexists as a non-directory.
Contents
val fold : ?rel:bool -> ?dotfiles:bool -> ?follow_symlinks:bool -> ?prune:(Unix.stats -> string -> Fpath.t -> bool) -> recurse:bool -> (Unix.stats -> string -> Fpath.t -> 'a -> 'a) -> Fpath.t -> 'a -> ('a, string) Stdlib.resultfold ~rel ~dotfiles ~follow_symlinks ~prune ~recurse f dir accfoldsfover the contents ofdirstarting withacc. Ifdirdoes not exist the function errors.f st name p accis called with each pathpfolded over withstits stat information,nameits filename andaccthe accumulator.- If
recurseistruesub-directoriesdirare folded over recursively moduloprune(see below). Ifrecurseis false only the direct contents ofdiris folded over. pruneis called only whenrecurseistrueasprune st dwithdany sub-directory to be folded over andstits stat information. If the result istruedand its contents are not folded over. Defaults tofun _ _ _ -> falsefollow_symlinksiftrue(default), symbolic links are followed. Iffalsesymbolic links are not followed and the stat information given topruneandfis given byPath.symlink_stat.- If
dotfilesisfalse(default) elements whose filename start with a.are not folded over - If
relisfalse(default) the paths given tofandprunehavedirprepended, iftruethey are relative todir.
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 -> Fpath.t -> bool) -> recurse:bool -> (Unix.stats -> string -> Fpath.t -> 'a -> 'a) -> Fpath.t -> 'a -> ('a, string) Stdlib.resultfold_filesis likefoldbutfis only applied to non-directory files.
val fold_dirs : ?rel:bool -> ?dotfiles:bool -> ?follow_symlinks:bool -> ?prune:(Unix.stats -> string -> Fpath.t -> bool) -> recurse:bool -> (Unix.stats -> string -> Fpath.t -> 'a -> 'a) -> Fpath.t -> 'a -> ('a, string) Stdlib.resultfold_dirsis likefoldbutfis only applied to directory files.
val path_list : Unix.stats -> string -> Fpath.t -> Fpath.t list -> Fpath.t listpath_listis 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 -> Fpath.t -> bool) -> make_path:bool -> recurse:bool -> src:Fpath.t -> Fpath.t -> (unit, string) Stdlib.resultcopy ~rel ~atomic ~prune ~follow_symlinks ~make_path ~recurse ~src dstcopies the directorysrctodst. File modes ofsrcand its contents are preserved indst. The function errors ifdstexists.- If
recurseistruesub-directories ofdirare also copied recursively, unless they arepruned (see below). Iffalseonly the files ofsrcare copied moduloprune. FIXME I think this is weird - If
make_pathistrueand the parent directory ofdstdoes not exist the whole path to the parent is created as needed with permission0o755(readable and traversable by everyone, writable by the user). prune st name pis called on each pathpto copy withstits stat information andnameits filename. If the function returnstruethe directory or file is not copied over. Defaults tofun _ _ _ -> false.- If
follow_symlinksistrue(default), symlinks are followed. Iffalsesymbolic links are not followed, the actual symlinks are copied and the stat information given topruneis given byOs.Path.symlink_stat. allow_hardlinksiftrue, tries to hard link files fromsrcat the destination, falling back to copying if that's not possible. Defaults tofalse.atomicif atomic istrueand the function errors thendstshould not exist. To write atomically, a temporary directorytin the parent directory ofdstis created. On copy successtis renamed todst. On errortis deleted anddstleft intact. This means the user needs write permissions in the parent directory ofdst, in practice this is almost always the case but fails for some directories (e.g. writing in/syson Linux®).- If
relisfalse(default) the paths given toprunehavesrcprepended. Iftruethey are relative tosrc.
- If
Current working directory (cwd)
val cwd : unit -> (Fpath.t, string) Stdlib.resultcwd ()is the current working directory. The resulting path is guaranteed to be absolute.
val set_cwd : Fpath.t -> (unit, string) Stdlib.resultset_cwd dirsets the current working directory todir.
val with_cwd : Fpath.t -> (unit -> 'a) -> ('a, string) Stdlib.resultwith_cwd dir fisf ()with the current working directory bound todir. After the function returns the current working directory is back to its initial value.
Default temporary directory
val default_tmp : unit -> Fpath.tdefault_tmp ()is a default directory that can be used as a default directory for creating temporary files and directories. Ifset_default_tmphasn't been called this is:- On POSIX, the value of the
TMPDIRenvironment variable orFpath.v "/tmp"if the variable is not set or empty. - On Windows, the value of the
TEMPenvironment variable orFpath.v "."if it is not set or empty.
- On POSIX, the value of the
val set_default_tmp : Fpath.t -> unitset_default_tmp psets the value returned bydefault_tmptop.
Temporary directories
val with_tmp : ?mode:int -> ?make_path:bool -> ?dir:Fpath.t -> ?name:Path.tmp_name -> (Fpath.t -> 'a) -> ('a, string) Stdlib.resultwith_tmp ~mode ~make_path ~dir ~name fcreates a temporary empty directorytand returns Ok (f t). After the function returns (normally or via an exception)tand its content are deleted.nameis used to construct the filename of the directory, seeFile.tmp_name for details. It defaults to"tmp-%s".diris the directory in which the temporary file is created. It defaults toDir.default_tmp ().- If
make_pathistrue(default) anddirdoesn't exist the whole path to it is created as needed with permission0o755(readable and traversable by everyone, writable by the user). modeare the permissions of the temporary directory; they default to0o700, only readable, writeable and traversable by the user
val tmp : ?mode:int -> ?make_path:bool -> ?dir:Fpath.t -> ?name:Path.tmp_name -> unit -> (Fpath.t, string) Stdlib.resulttmpis likewith_tmpexcept 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 -> (Fpath.t, string) Stdlib.resultuser ()is the home directory of the user executing the process. Determined by consultingpasswddatabase with the user if of the process. If this fails or on Windows falls back to parse a path from theHOMEenvironment variables.
val config : unit -> (Fpath.t, string) Stdlib.resultconfig ()is the directory used to store user-specific program configurations. This is in order:- If set the value of
XDG_CONFIG_HOME. - If set and on Windows® the value of
%LOCALAPPDATA%. - If
user ()isOk home,Fpath.(home / ".config").
- If set the value of
val data : unit -> (Fpath.t, string) Stdlib.resultdata ()is the directory used to store user-specific program data. This is in order:- If set the value of
XDG_DATA_HOME. - If set and on Windows® the value of
%LOCALAPPDATA%. - If
user ()isOk home,Fpath.(home / ".local" / "share").
- If set the value of
val cache : unit -> (Fpath.t, string) Stdlib.resultcache ()is the directory used to store user-specific non-essential data. This is in order:- If set the value of
XDG_CACHE_HOME. - If set and on Windows® the value of
%TEMP% - If
user ()isOk home,Fpath.(home / ".cache")
- If set the value of
val runtime : unit -> (Fpath.t, string) Stdlib.resultruntime ()is the directory used to store user-specific runtime files. This is in order:- If set the value of
XDG_RUNTIME_HOME. - The value of
default_tmp.
- If set the value of