Module B0_std.Os.File

module File: sig .. end
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 : B0_std.Fpath.t
null represents a file on the OS that discards all writes and returns end of file on reads.
val dash : B0_std.Fpath.t
dash is "-". This value is used by B0_std.Os.File.read and B0_std.Os.File.write to respectively denote stdin and stdout.

Existence


val exists : B0_std.Fpath.t -> (bool, string) Pervasives.result
exists file is Ok true if file is a regular file in the file system and Ok false otherwise. Symbolic links are followed.
val must_exist : B0_std.Fpath.t -> (unit, string) Pervasives.result
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 : B0_std.Fpath.t -> bool
is_executable file is true iff file exists and is executable.

Deleting and truncating


val delete : B0_std.Fpath.t -> (bool, string) Pervasives.result
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: See also B0_std.Os.Path.delete.
val truncate : B0_std.Fpath.t -> int -> (unit, string) Pervasives.result
trunacte file size truncates file to size.

Hard links

For symbolic links see Symbolic links.

val link : force:bool ->
make_path:bool ->
src:B0_std.Fpath.t -> B0_std.Fpath.t -> (unit, string) Pervasives.result
link ~force ~src p hard links file path p to the file src.

Reading


val read_with_fd : B0_std.Fpath.t -> (Unix.file_descr -> 'b) -> ('b, string) Pervasives.result
read_with_ic file f opens file as a file descriptor fdi and returns Ok (f ic). If file is B0_std.Os.File.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.
val read_with_ic : B0_std.Fpath.t ->
(Pervasives.in_channel -> 'b) -> ('b, string) Pervasives.result
read_with_ic file f is exactly like B0_std.Os.File.read_with_fd but opens an OCaml input channel.
val read : B0_std.Fpath.t -> (string, string) Pervasives.result
read file is file's content as a string. If file is B0_std.Os.File.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 ->
B0_std.Fpath.t ->
(Unix.file_descr -> ('a, 'b) Pervasives.result) ->
(('a, 'b) Pervasives.result, string) Pervasives.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 B0_std.Os.File.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.
val write_with_oc : ?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
B0_std.Fpath.t ->
(Pervasives.out_channel -> ('a, 'b) Pervasives.result) ->
(('a, 'b) Pervasives.result, string) Pervasives.result
write_with_oc ~atomic ~mode ~force ~make_path file f operates like B0_std.Os.File.write_with_fd but opens an OCaml channel.
val write : ?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
B0_std.Fpath.t -> string -> (unit, string) Pervasives.result
write ~atomic ~mode ~force ~make_path file s operates like B0_std.Os.File.write_with_fd but directly writes s to file.
val copy : ?atomic:bool ->
?mode:int ->
force:bool ->
make_path:bool ->
src:B0_std.Fpath.t -> B0_std.Fpath.t -> (unit, string) Pervasives.result
copy ~atomic ~mode ~force ~path ~make_path ~src file operates like B0_std.Os.File.write_with_fd but directly writes the content of src (or stdin if src is B0_std.Os.File.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:B0_std.Fpath.t ->
?name:B0_std.Os.Path.tmp_name ->
(B0_std.Fpath.t -> Unix.file_descr -> 'b) -> ('b, string) Pervasives.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.
val open_tmp_fd : ?flags:Unix.open_flag list ->
?mode:int ->
?make_path:bool ->
?dir:B0_std.Fpath.t ->
?name:B0_std.Os.Path.tmp_name ->
unit -> (B0_std.Fpath.t * Unix.file_descr, string) Pervasives.result
open_tmp_fd is like B0_std.Os.File.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:B0_std.Fpath.t ->
?name:B0_std.Os.Path.tmp_name ->
(B0_std.Fpath.t -> Pervasives.out_channel -> 'b) ->
('b, string) Pervasives.result
with_tmp_oc is like B0_std.Os.File.with_tmp_fd but uses an OCaml output channel instead of a file decriptor.