123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152(*
* Copyright (c) 2022-2022 Tarides <contact@tarides.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)openImportmoduletypeS=sig(** Low level IO abstraction. A typical implementation is unix. This
abstraction is meant to be dead simple. Not a lot of documentation is
required.
It is not resistant to race condictions. There should not be concurrent
modifications of the files.
These functions are essentially invoking the underlying functions from
{!Unix} directly; there is no buffering for example. *)typet(** {1 Errors} *)typemisc_error[@@derivingirmin](** An abstract error type that contains the IO-backend specific errors. (e.g.
[Unix.error]) *)typecreate_error=[`Io_miscofmisc_error|`File_existsofstring]typeopen_error=[`Io_miscofmisc_error|`No_such_file_or_directory|`Not_a_file]typeread_error=[`Io_miscofmisc_error|`Read_out_of_bounds|`Closed|`Invalid_argument]typewrite_error=[`Io_miscofmisc_error|`Ro_not_allowed|`Closed]typeclose_error=[`Io_miscofmisc_error|`Double_close]typemkdir_error=[`Io_miscofmisc_error|`File_existsofstring|`No_such_file_or_directory|`Invalid_parent_directory](** {1 Safe Functions}
None of the functions in this section raise exceptions. They may however
perform effects that are always continued.
{2 Life Cycle} *)valcreate:path:string->overwrite:bool->(t,[>create_error])resultvalopen_:path:string->readonly:bool->(t,[>open_error])resultvalclose:t->(unit,[>close_error])result(** {2 Write Functions} *)valwrite_string:t->off:int63->string->(unit,[>write_error])result(** [write_string t ~off s] writes [s] at [offset] in [t]. *)valfsync:t->(unit,[>write_error])result(** [fsync t] persists to the file system the effects of previous [create] or
write. *)valmove_file:src:string->dst:string->(unit,[>`Sys_errorofstring])resultvalcopy_file:src:string->dst:string->(unit,[>`Sys_errorofstring])resultvalmkdir:string->(unit,[>mkdir_error])resultvalunlink:string->(unit,[>`Sys_errorofstring])result(** {2 Read Functions} *)valread_to_string:t->off:int63->len:int->(string,[>read_error])result(** [read_to_string t ~off ~len] are the [len] bytes of [t] at [off]. *)valread_all_to_string:t->(string,[>`Io_miscofmisc_error|`Closed])result(** [read_to_string t] is the contents full contents of the file.
The individual pages are not guaranteed to be read atomically. *)valread_size:t->(int63,[>read_error])result(** [read_size t] is the number of bytes of the file handled by [t].
This function is expensive in the unix implementation because it performs
syscalls. *)valsize_of_path:string->(int63,[>`Io_miscofmisc_error|`No_such_file_or_directory|`Not_a_file])resultvalclassify_path:string->[>`File|`Directory|`No_such_file_or_directory|`Other](** {1 MISC.} *)valreadonly:t->boolvalpath:t->stringvalpage_size:int(** {1 Unsafe Functions}
These functions are equivalents to exising safe ones, but using exceptions
instead of the result monad for performances reasons. *)valread_exn:t->off:int63->len:int->bytes->unit(** [read_exn t ~off ~len b] reads the [len] bytes of [t] at [off] to [b].
Raises [Errors.Pack_error] and [Errors.RO_not_allowed].
Also raises backend-specific exceptions (e.g. [Unix.Unix_error] for the
unix backend). *)valwrite_exn:t->off:int63->len:int->string->unit(** [write_exn t ~off ~len b] writes the first [len] bytes pf [b] to [t] at
offset [off].
Raises [Errors.Pack_error] and [Errors.RO_not_allowed].
Also raises backend-specific exceptions (e.g. [Unix.Unix_error] for the
unix backend). *)valraise_misc_error:misc_error->'avalcatch_misc_error:(unit->'a)->('a,[>`Io_miscofmisc_error])resultendmoduletypeSigs=sigmoduletypeS=SmoduleUnix:Swithtypemisc_error=Unix.error*string*stringend