Module B0.OS.File

module File: sig .. end
File operations.


Famous file paths


val null : B0.Fpath.t
null represents a file on the OS that discards all writes and returns end of file on reads.
val dash : B0.Fpath.t
dash is "-". This value is used by B0.OS.File.read and B0.OS.File.write to respectively denote stdin and stdout.

Existence and deletion


val exists : B0.Fpath.t -> bool B0.result
exists file is true if file is a regular in the file system and false otherwise. Symbolic links are followed.
val must_exist : B0.Fpath.t -> B0.Fpath.t B0.result
must_exist file is file if file is a regular file in the file system and an error otherwise. Symbolic links are followed.
val delete : ?must_exist:bool -> B0.Fpath.t -> unit B0.result
delete ~must_exist file deletes file file. If must_exist is true (defaults to false) an error is returned if file doesn't exist.

Hard links


val link : force:bool -> target:B0.Fpath.t -> B0.Fpath.t -> unit B0.result
link ~force ~target p hard links target to path p. If force is true and p exists it is unlinked first. FIXME should we rmdir -r if p is a directory ?

Executability


val is_executable : B0.Fpath.t -> bool
is_executable file is true iff file exists and is executable.

Input

Stdin. In the following functions if the path is B0.OS.File.dash, bytes are read from stdin.

val with_ic : B0.Fpath.t -> (Pervasives.in_channel -> 'a -> 'b) -> 'a -> 'b B0.result
with_ic file f v opens file as a channel ic and returns Ok (f ic v). After the function returns (normally or via an exception), ic is ensured to be closed. If file is B0.OS.File.dash, ic is Pervasives.stdin and not closed when the function returns. End_of_file exceptions raised by f are turned it into an error message.
val read : B0.Fpath.t -> string B0.result
read file is file's content as a string.

Output

The following applies to every function in this section:


val with_oc : ?mode:int ->
B0.Fpath.t ->
(Pervasives.out_channel -> 'a -> ('c, 'd) Pervasives.result) ->
'a -> ('c, 'd) Pervasives.result B0.result
with_oc file f v opens file as a channel oc and returns Ok (f oc v). After the function returns (normally or via an exception) oc is closed. file is not written if f returns an error. If file is B0.OS.File.dash, oc is Pervasives.stdout and not closed when the function returns.
val write : ?mode:int -> B0.Fpath.t -> string -> unit B0.result
write file content outputs content to file. If file is B0.OS.File.dash, writes to Pervasives.stdout. If an error is returned file is left untouched except if Pervasives.stdout is written.

Temporary files


val with_tmp_oc : ?flags:Unix.open_flag list ->
?mode:int ->
?ext:B0.Fpath.ext ->
B0.Fpath.t ->
(B0.Fpath.t -> Pervasives.out_channel -> 'a -> 'b) -> 'a -> 'b B0.result
with_tmp_oc ~flags ~mode ~ext p f v is Ok (f file oc v) with file a file path atomatically created and opened with permission mode (defaults to 0o600 only readable and writable by the user) on the output channel oc. After the function returns (normally or via an exception), oc is closed and file is deleted. The file name is constructed with strf "%a%06x%s" Fpath.pp f rand ext with rand a default number (ext defaults to .tmp). The client needs to make sure the directories to file exist.