package b0
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=e9aa779e66c08fc763019f16d4706f465d16c05d6400b58fbd0313317ef33ddea51952e2b058db28e65f7ddb7012f328c8bf02d8f1da17bb543348541a2587f0
doc/b0.std/B0_std/Os/File/index.html
Module Os.FileSource
Regular file operations.
This module operates on regular files, most functions error if they are applied to other file kinds.
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.
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, iffileexisted and was deleted.Ok false, if the pathfiledid not exist on the file system.Error _in case of error and in particular iffileis a directory.
See also Path.delete.
trunacte file size truncates file to size.
Hard links
For symbolic links see Symbolic links.
link ~force ~src p hard links file path p to the file src.
- If
forceistrueandpexists an attempt to delete it is performed withFile.deletep. Ifforceisfalseandpexists the function errors. - 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).
Reading
read_with_ic file f opens file as a file descriptor fdi and returns Ok (f ic). If file is Fpath.dash, ic is stdin. After the function returns (normally or via an exception raised by f), ic is ensured to be closed, except if it is stdin. The function errors if opening file fails. Errors have the form Fmt.str "%s: %s" file err.
read_with_ic file f is exactly like read_with_fd but opens an OCaml input channel in binary mode.
read file is file's content as a string. If file is Fpath.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. Errors have the form Fmt.str "%s: %s" file err.
Writing and copying
val write_with_fd :
?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
Fpath.t ->
(Unix.file_descr -> ('a, 'b) result) ->
(('a, 'b) result, string) resultwrite_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 Fpath.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_pathistrueand the parent directory offiledoes not exist the whole path to the parent is created as needed with permission0o755(readable and traversable by everyone, writable by the user). - If
forceistrueandfileexists at call time as a regular file it tries to overwrite it, in all other cases the function errors iffileexists. modeare the permissions of the written file; they default to0o644, readable by everyone, writable by the user.- If
atomicistrue(default) and the function orferrorsfileis left untouched. To write atomically, a temporary filetin the parent directory offileis created. On write successtis renamed tofile; an operation which is more or less atomic. On errortis deleted andfileleft 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/syson Linux®). XXX An improvement would be to automatically disableatomicon nonUnix.file_kind.S_REGfiles 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) result) ->
(('a, 'b) result, string) resultwrite_with_oc ~atomic ~mode ~force ~make_path file f operates like write_with_fd but opens an OCaml channel in binary mode.
val write :
?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
Fpath.t ->
string ->
(unit, string) resultwrite ~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 ->
Fpath.t ->
dst:Fpath.t ->
(unit, string) resultcopy ~atomic ~mode ~force ~path ~make_path src ~dst:file operates like write_with_fd but directly writes the content of src (or stdin if src is Fpath.dash) to file. mode defaults to the permissions of src if available and 0o644 otherwise.
val copy_to_dir :
?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
?src_root:Fpath.t ->
Fpath.t ->
dir:Fpath.t ->
(unit, string) resultcopy ~force ~make_path src ~dir is copy ~force ~make_path src ~dst with dst equal to Fpath.reroot ~src_root ~dst_root:dir src and src_root defaulting to Fpath.parent src.
Temporary files
See also temporary paths.
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) resultwith_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.
nameis used to construct the filename of the file, seePath.tmp_namefor details. It defaults to"tmp-%s".diris the directory in which the temporary file is created. It defaults toB0_std.Os.Dir.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 written file; they default to0o600, only readable and writeable by the userflagsare 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) resultopen_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) resultwith_tmp_oc is like with_tmp_fd but uses an OCaml output channel instead of a file decriptor.