Frama-C API - Filesystem
The set of functions in Filesystem are provided both as a convenient way to use Filepath.t
directly (without conversion) and to be safer variants than the standard library's or Unix library's.
They are safer in several ways:
- some of them are intended to never fail (
dir_exists
,remove_file
, etc.); - some of them return a
Result.t
(file_kind
,with_open_in
, etc.) which forces the caller to be careful about the possible errors; - the others will uniformly raise
Sys_error
- possibly converted fromUnix.Unix_error
- so the handling of exceptions is a bit lighter; - all the functions taking a file path as argument will check that the path is not empty and may raise
Invalid_argument
if it is not the case.
The module documentation should mention all the possible exceptions raised and the caller should always catch Sys_error
if needed. Empty file paths are considered a programming error, and the emptiness should be checked by the caller beforehand. Thus, the caller should not catch Invalid_argument
.
Error handling
type error = string * Filepath.t
type nonrec 'a result = ('a, error) Stdlib.result
File system
This type is used to determine the type of file a path refers to.
val file_kind : Filepath.t -> file_kind result
file_kind p
returns the file kind of the given path p
. On failure - for instance if the file does not exist - returns an error string.
val exists : Filepath.t -> bool
exists p
returns whether the path p
points to an existing file (of any kind) p
. Equivalent to Sys.file_exists
.
val file_exists : Filepath.t -> bool
file_exists p
returns whether the path points to an existing regular file. It is equivalent to file_kind p = Ok (File)
val dir_exists : Filepath.t -> bool
dir_exists p
returns whether the path points to an existing directory, It is equivalent to file_kind p = Ok (Directory)
val is_file : Filepath.t -> bool
is_file f
returns true
iff f
points to a regular file (or a symbolic link pointing to a file). Returns false
if any errors happen when stat
'ing the file.
val is_dir : Filepath.t -> bool
Equivalent to Sys.is_directory
.
val list_dir : Filepath.t -> string list
Contents of a directory.
val iter_dir : (string -> unit) -> Filepath.t -> unit
Iter through the contents of a directory.
val fold_dir : (string -> 'a -> 'a) -> Filepath.t -> 'a -> 'a
Fold over the contents of a directory.
val make_dir : ?parents:bool -> ?perm:int -> Filepath.t -> unit
make_dir ?parents ?perm filepath
creates directory filepath
with permission perm
(default is 0o755). If the directory already exists, this function does nothing. However, if the path points to an existing file that is not a directory, the function raises Sys_error
. If parents
is true (the default), recursively create parent directories if needed. Note that this function may create some of the parent directories and then fail to create the children, e.g. if perm
does not allow user execution of the created directory. This will leave the filesystem in a modified state before raising Sys_error
.
val remove_file : Filepath.t -> unit
Tries to delete a file and never fails.
val remove_dir : Filepath.t -> unit
Tries to delete a directory and never fails.
val rename : Filepath.t -> Filepath.t -> unit
rename source target
rename the file source
to target
. Equivalent to Sys.rename
.
Temporary files
See Temp_files
module for automatic removal of temp files at exit.
val temp_file : prefix:string -> suffix:string -> Filepath.t
Similar to Filename.temp_file
.
val temp_dir : prefix:string -> suffix:string -> Filepath.t
Similar to Filename.temp_dir
.
File comparison
val digest : Filepath.t -> string
digest p
computes the hash of a file p
using Stdlib.Digest.file
.
val same_digest : Filepath.t -> Filepath.t -> bool
same_digest p1 p2
compares the hashes of two files p1
and p2
using Stdlib.Digest.file
and returns true
if they have the same.
High level Input/Output
val copy_file : Filepath.t -> Filepath.t -> unit
copy_file source target
copies source file to target file.
val iter_lines : Filepath.t -> (string -> unit) -> unit
Iter over all text lines in the file
Low level file Input/Output
This type defines what action with_open_in
and with_open_out
must perform when the file to open does not exist.
This type define what action with_open_out
must perform when the file to open already exists.
type ('ch, 'a) safe_processor = ('ch -> 'a) -> 'a result
A safe_processor
helps to handle file operations while ensuring the file will be closed no matter what happens. It is a function that takes a file operation f
as a parameter, opens a file and calls the f
with the newly-created channel.
Same as safe_processor
but when a Sys_error
is raised, re-raise it after closing the file
val with_open_in : ?if_missing:action_if_missing -> ?binary:bool -> ?blocking:bool -> Filepath.t -> (Stdlib.in_channel, 'a) safe_processor
with_open_in path f
opens file path
for reading and calls f
with the newly-created input channel. The file is closed when f
returns or whenever a Sys_error
is thrown by f
.
val with_open_in_exn : ?if_missing:action_if_missing -> ?binary:bool -> ?blocking:bool -> Filepath.t -> (Stdlib.in_channel, 'a) exn_processor
Same as with_open_in
but raises Sys_error
instead of returning Error
.
val with_open_out : ?if_missing:action_if_missing -> ?if_exists:action_if_exists -> ?binary:bool -> ?blocking:bool -> Filepath.t -> (Stdlib.out_channel, 'a) safe_processor
with_open_out path f
calls f
with a new output channel on the file path
opened for writing. The file is closed when f
returns or whenever a Sys_error
is thrown by f
.
val with_open_out_exn : ?if_missing:action_if_missing -> ?if_exists:action_if_exists -> ?binary:bool -> ?blocking:bool -> Filepath.t -> (Stdlib.out_channel, 'a) exn_processor
Same as with_open_out
but raises Sys_error
instead of returning Error
.
val with_formatter : Filepath.t -> (Stdlib.Format.formatter, 'a) safe_processor
with_formatter path f
calls f
with a formatter writing to the file path
. The file is closed and the formatter is flushed when f
returns or whenever a Sys_error
is thrown by f
.
val with_formatter_exn : Filepath.t -> (Stdlib.Format.formatter, 'a) exn_processor
Same as with_formatter
but raises Sys_error
instead of returning Error
.
module Compressed : sig ... end
module Operators : sig ... end
Opening this module allows to use shorter syntax to deal with files.