Module Os.Path
File system path operations.
These functions operate on files and directories equally. Specific function operating on either kind of path are in the File
and Dir
modules.
Existence
val exists : Fpath.t -> (bool, string) Stdlib.result
exists p
isOk true
ifp
exists in the file system andOk false
otherwise. Symbolic links are followed.
val must_exist : Fpath.t -> (unit, string) Stdlib.result
must_exist p
isOk ()
ifp
exists in the file system and an error otherwise. Symbolic links are followed.
Deleting and renaming
val delete : recurse:bool -> Fpath.t -> (bool, string) Stdlib.result
delete ~recurse p
deletesp
from the file system. Ifp
is a symbolic link this only deletes the link, not the linked object. Ifrecurse
istrue
andp
is a non-empty directory, no error occurs, its contents is recursively deleted. The result is:Ok true
, ifp
existed and was deleted.Ok false
, if the pathp
did not exist on the file system.Error _
in case of error, in particular ifp
is a non-empty directory andrecurse
isfalse
.
See also
File.delete
.
val rename : force:bool -> make_path:bool -> src:Fpath.t -> Fpath.t -> (unit, string) Stdlib.result
rename ~force ~make_path ~src dst
renamessrc
todst
.- If
force
istrue
anddst
exists it tries to delete it usingFile.delete
dst
. Ifforce
isfalse
anddst
exists the function errors. - If
make_path
istrue
and the parent directory ofdst
does not exist the whole path to the parent is created as needed with permission0o755
(readable and traversable by everyone, writable by the user).
- If
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.result
copy ~make_path ~recurse ~src dst
copies the file or file hierarchy rooted atsrc
todst
. The function errors ifdst
exists. The semantics and arguments correspond to those of Copying, except this function also works ifsrc
is not a directory. Note thatprune
is never called onsrc
itself FIXME is that a good idea ? also FIXME this should error ifsrc
is a directory andrecurse
is false.See also Copying and
Os.File.copy
.
File mode and stat
See also File.is_executable
.
val get_mode : Fpath.t -> (int, string) Stdlib.result
get_mode p
is the file mode ofp
. Symbolic links are followed.
val set_mode : Fpath.t -> int -> (unit, string) Stdlib.result
set_mode file p
sets the file mode offile
top
. Symbolic links are followed.
val stat : Fpath.t -> (Unix.stats, string) Stdlib.result
stat p
isp
's file information. Symbolic links are followed.
Symbolic links
For hard links see Hard links.
val symlink : force:bool -> make_path:bool -> src:Fpath.t -> Fpath.t -> (unit, string) Stdlib.result
symlink ~force ~src p
symbolically linkssrc
top
.- If
force
istrue
andp
exists it tries to delete it usingFile.delete
p
. Ifforce
isfalse
andp
exists the function errors. - If
make_path
istrue
and the parent directory offile
does not exist the whole path to the parent is created as needed with permission0o755
(readable and traversable by everyone, writable by the user).
- If
val symlink_link : Fpath.t -> (Fpath.t, string) Stdlib.result
symlink_link p
isOk l
ifp
is a symbolic link tol
.
val symlink_stat : Fpath.t -> (Unix.stats, string) Stdlib.result
symlink_stat p
is likestat
but ifp
is a symlink returns information about the link itself. Ifp
is not a symlink then this isstat
.
Temporary paths
type tmp_name
= (string -> string, unit, string) Stdlib.format
The type for temporary file name patterns. The string format is replaced by random hexadecimal US-ASCII characters.
val tmp : ?make_path:bool -> ?dir:Fpath.t -> ?name:tmp_name -> unit -> (Fpath.t, string) Stdlib.result
tmp ~make_path ~dir name ()
is a file system path indir
that did not exist when the name was found. It may exist once the function returns though, prefer temporary files and directories creation functions to guarantee the creation of the temporary objects.name
is used to construct the filename of the file, seetmp_name
for details. It defaults to"tmp-%s"
.dir
is the directory in which the temporary file is created. It defaults toOs.Dir.default_tmp
()
.- If
make_path
istrue
(default) anddir
does not exist the whole path to it is created as needed with permission0o755
(readable and traversable by everyone, writable by the user).