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.tnullrepresents a file on the OS that discards all writes and returns end of file on reads.
Existence
val exists : Fpath.t -> (bool, string) Stdlib.resultexists fileisOk trueiffileis a regular file in the file system andOk falseotherwise. Symbolic links are followed.
val must_exist : Fpath.t -> (unit, string) Stdlib.resultmust_exist fileisOk ()iffileis a regular file in the file system and an error otherwise. Symbolic links are followed.
val is_executable : Fpath.t -> boolis_executable fileistrueifffileexists and is executable.
Deleting and truncating
val delete : Fpath.t -> (bool, string) Stdlib.resultdelete filedeletes filefilefrom the file system. Iffileis 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.
val truncate : Fpath.t -> int -> (unit, string) Stdlib.resulttrunacte file sizetruncatesfiletosize.
Hard links
For symbolic links see Symbolic links.
val link : force:bool -> make_path:bool -> src:Fpath.t -> Fpath.t -> (unit, string) Stdlib.resultlink ~force ~src phard links file pathpto the filesrc.- 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).
- If
Reading
val read_with_fd : Fpath.t -> (Unix.file_descr -> 'b) -> ('b, string) Stdlib.resultread_with_ic file fopensfileas a file descriptorfdiand returnsOk (f ic). Iffileisdash,icisstdin. After the function returns (normally or via an exception raised byf),icis ensured to be closed, except if it isstdin. The function errors if openingfilefails.
val read_with_ic : Fpath.t -> (Stdlib.in_channel -> 'b) -> ('b, string) Stdlib.resultread_with_ic file fis exactly likeread_with_fdbut opens an OCaml input channel.
val read : Fpath.t -> (string, string) Stdlib.resultread fileisfile's content as a string. Iffileisdashthe contents ofstdinis read. Warning. The signature of this function limits files to be at mostSys.max_string_length in size. On 32-bit platforms this is only around16MB.
Writing and copying
val write_with_fd : ?atomic:bool -> ?mode:int -> force:bool -> make_path:bool -> Fpath.t -> (Unix.file_descr -> ('a, 'b) Stdlib.Pervasives.result) -> (('a, 'b) Stdlib.Pervasives.result, string) Stdlib.resultwrite_with_fd ~atomic ~mode ~force ~make_path file fopens an output file descriptorfdoto write tofileand returnsOk (f fdo). Iffileisdash,fdoisUnix.stdout. After the function returns (normally or via an exception)fdois ensured to be closed except if it isUnix.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).
- If
val write_with_oc : ?atomic:bool -> ?mode:int -> force:bool -> make_path:bool -> Fpath.t -> (Stdlib.out_channel -> ('a, 'b) Stdlib.Pervasives.result) -> (('a, 'b) Stdlib.Pervasives.result, string) Stdlib.resultwrite_with_oc ~atomic ~mode ~force ~make_path file foperates likewrite_with_fdbut opens an OCaml channel.
val write : ?atomic:bool -> ?mode:int -> force:bool -> make_path:bool -> Fpath.t -> string -> (unit, string) Stdlib.resultwrite ~atomic ~mode ~force ~make_path file soperates likewrite_with_fdbut directly writesstofile.
val copy : ?atomic:bool -> ?mode:int -> force:bool -> make_path:bool -> src:Fpath.t -> Fpath.t -> (unit, string) Stdlib.resultcopy ~atomic ~mode ~force ~path ~make_path ~src fileoperates likewrite_with_fdbut directly writes the content ofsrc(orstdinifsrcisdash) tofile.modedefaults to the permissions ofsrcif available and0o644otherwise.
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) Stdlib.resultwith_tmp_fd ~flags ~mode ~make_path ~dir ~name fopens an output file descriptorfdoto a temporary file and returnsOk (f fdo). After the function returns (normally or via an exception)fdois ensured to be closed and the temporary file is deleted.nameis used to construct the filename of the file, seetmp_namefor details. It defaults to"tmp-%s".diris the directory in which the temporary file is created. It defaults toDir.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) Stdlib.resultopen_tmp_fdis likewith_tmp_fdexcept 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 -> Stdlib.out_channel -> 'b) -> ('b, string) Stdlib.resultwith_tmp_ocis likewith_tmp_fdbut uses an OCaml output channel instead of a file decriptor.