package b0
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=dadde8cfa62be9dabd805fc190b415427b4699ffe3458c153e2f3f9cc5c9b9b4
md5=f96ac96fb0182f2b97dbe9ded452544b
doc/b0.std/B0_std/Os/File/index.html
Module Os.File
Regular file operations.
This module operates on regular files, most functions error if they are applied to other file kinds.
Famous file paths
val null : Fpath.t
null
represents a file on the OS that discards all writes and returns end of file on reads.
val dash : Fpath.t
Existence
exists file
is Ok true
if file
is a regular file in the file system and Ok false
otherwise. Symbolic links are followed.
must_exist file
is Ok ()
if file
is a regular file in the file system and an error otherwise. Symbolic links are followed.
val is_executable : Fpath.t -> bool
is_executable file
is true
iff file
exists and is executable.
Deleting and truncating
delete file
deletes file file
from the file system. If file
is a symbolic link this only deletes the link, not the linked file. The result is:
Ok true
, iffile
existed and was deleted.Ok false
, if the pathfile
did not exist on the file system.Error _
in case of error and in particular iffile
is a directory.
See also Path.delete
.
Hard links
For symbolic links see Symbolic links.
link ~force ~src p
hard links file path p
to the file src
.
- If
force
istrue
andp
exists an attempt to delete it is performed withFile.delete
p
. Ifforce
isfalse
andp
exists the function errors. - If
make_path
istrue
and the parent directory ofp
does not exist the whole path to the parent is created as needed with permission0o755
(readable and traversable by everyone, writable by the user).
Reading
val read_with_fd : Fpath.t -> (Unix.file_descr -> 'b) -> ('b, string) result
val read_with_ic : Fpath.t -> (in_channel -> 'b) -> ('b, string) result
read_with_ic file f
is exactly like read_with_fd
but opens an OCaml input channel.
read file
is file
's content as a string. If file
is dash
the contents of stdin
is read. Warning. The signature of this function limits files to be at most Sys.max_string_length
in size. On 32-bit platforms this is only around 16MB
.
Writing and copying
val write_with_fd :
?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
Fpath.t ->
(Unix.file_descr -> ('a, 'b) Pervasives.result) ->
(('a, 'b) Pervasives.result, string) result
write_with_fd ~atomic ~mode ~force ~make_path file f
opens an output file descriptor fdo
to write to file
and returns Ok (f fdo)
. If file
is dash
, fdo
is Unix.stdout
. After the function returns (normally or via an exception) fdo
is ensured to be closed except if it is Unix.stdout
.
- 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
force
istrue
andfile
exists at call time as a regular file it tries to overwrite it, in all other cases the function errors iffile
exists. mode
are the permissions of the written file; they default to0o644
, readable by everyone, writable by the user.- If
atomic
istrue
(default) and the function orf
errorsfile
is left untouched. To write atomically, a temporary filet
in the parent directory offile
is created. On write successt
is renamed tofile
; an operation which is more or less atomic. On errort
is deleted andfile
left intact. This means the user needs write permissions in the parent directory offile
, in practice this is almost always the case but fails for some directories (e.g. writing to/sys
on Linux®). XXX An improvement would be to automatically disableatomic
on nonUnix.file_kind.S_REG
files at the cost of astat(2)
.
val write_with_oc :
?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
Fpath.t ->
(out_channel -> ('a, 'b) Pervasives.result) ->
(('a, 'b) Pervasives.result, string) result
write_with_oc ~atomic ~mode ~force ~make_path file f
operates like write_with_fd
but opens an OCaml channel.
val write :
?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
Fpath.t ->
string ->
(unit, string) result
write ~atomic ~mode ~force ~make_path file s
operates like write_with_fd
but directly writes s
to file
.
val copy :
?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
src:Fpath.t ->
Fpath.t ->
(unit, string) result
copy ~atomic ~mode ~force ~path ~make_path ~src file
operates like write_with_fd
but directly writes the content of src
(or stdin
if src
is dash
) to file
. mode
defaults to the permissions of src
if available and 0o644
otherwise.
Temporary files
val with_tmp_fd :
?flags:Unix.open_flag list ->
?mode:int ->
?make_path:bool ->
?dir:Fpath.t ->
?name:Path.tmp_name ->
(Fpath.t -> Unix.file_descr -> 'b) ->
('b, string) result
with_tmp_fd ~flags ~mode ~make_path ~dir ~name f
opens an output file descriptor fdo
to a temporary file and returns Ok (f fdo)
. After the function returns (normally or via an exception) fdo
is ensured to be closed and the temporary file is deleted.
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 toDir.default_tmp()
.- If
make_path
istrue
(default) anddir
doesn't exist the whole path to it is created as needed with permission0o755
(readable and traversable by everyone, writable by the user). mode
are the permissions of the written file; they default to0o600
, only readable and writeable by the userflags
are the flags used to open the file. They default toUnix.[O_WRONLY; O_CREAT; O_EXCL; O_SHARE_DELETE; O_CLOEXEC]
val open_tmp_fd :
?flags:Unix.open_flag list ->
?mode:int ->
?make_path:bool ->
?dir:Fpath.t ->
?name:Path.tmp_name ->
unit ->
(Fpath.t * Unix.file_descr, string) result
open_tmp_fd
is like with_tmp_fd
except it is the client's duty to close the file descriptor and delete the file (if the file is not deleted it will be when the program exits).
val with_tmp_oc :
?flags:Unix.open_flag list ->
?mode:int ->
?make_path:bool ->
?dir:Fpath.t ->
?name:Path.tmp_name ->
(Fpath.t -> out_channel -> 'b) ->
('b, string) result
with_tmp_oc
is like with_tmp_fd
but uses an OCaml output channel instead of a file decriptor.