Source file fileSig.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
(**************************************************************************)
(*                                                                        *)
(*   Typerex Libraries                                                    *)
(*                                                                        *)
(*   Copyright 2011-2017 OCamlPro SAS                                     *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

(** Signatures exported by other modules *)

open [@ocaml.warning "-33"] EzCompat

(** This module provides functions to read and write complete files
    from and to strings, or working on file lines. *)
module type CONTENT_OPERATIONS = sig

  type in_file
  (** The type of a file from which we read:
      [in_channel] for [FileChannel], [string] for [FileString] and
      [FileGen.t] for [FileGen].
    *)

  type out_file
  (** The type of a file to which we write:
      [out_channel] for [FileChannel], [string] for [FileString] and
      [FileGen.t] for [FileGen].
 *)

(*

   Converting file contents to string, back and forth.

*)

  (** [read_file file] returns the full content of [file]. If the file
     is opened, it is opened in binary mode, no conversion is
     applied.*)
  val read_file : in_file -> string

  (** [write_file file content] creates file [file] with content
     [content].  If the file is opened, it is opened in binary mode,
     no conversion is applied.*)
  val write_file : out_file -> string -> unit

  (** [read_subfile file pos len] returns a string containing [len]
     bytes read from file [file] at pos [pos]. If the file is opened,
     it is opened in binary mode. Raises [End_of_file] if the file is
     too short.  *)
  val read_subfile : in_file -> int -> int -> string

(*

   Converting file contents to lines, back and forth.

*)

  (** [read_lines file] returns the content of [file] as an array of
     lines. If the file is opened, it is opened in text mode. *)
  val read_lines :  in_file -> string array

  (** [read_lines_to_list file] returns the content of [file] as a
     list of lines. If the file is opened, it is opened in text
     mode. *)
  val read_lines_to_list :  in_file -> string list

  (** [write_lines file lines] creates the file [file] from an array of
     lines, using [FileChannel.output_line] for each line. *)
  val write_lines :  out_file -> string array -> unit

  (** [write_lines file lines] creates the file [file] from a list of
     lines, using [FileChannel.output_line] for each line. *)
  val write_lines_of_list :  out_file -> string list -> unit

  (** [read_sublines file pos len] returns at most [len] lines of the
     file [file], starting at line [pos]. It differs from [read_subfile]
     in that it will not raise any exception if the file is too
     short. Note that it reads the file from beginning everytimes. *)
  val read_sublines : in_file -> int -> int -> string array

  (** Same as [read_sublines], but returns a list of strings. *)
  val read_sublines_to_list : in_file -> int -> int -> string list



  (*

   Iterators on chunks and lines.

  *)


  (** [iter_blocks f file] reads the content of file [file], and calls
     [f buffer len] on each chunk. The [buffer] is reused, and only
     the first [len] bytes are from the file. Chunks have a maximal
     size of 32768.  *)
  val iter_blocks : (Bytes.t -> int -> unit) -> in_file -> unit

  (** [iter_lines f file] calls [f line] on all the lines [line] of
   the file [file]. *)
  val iter_lines : (string -> unit) -> in_file -> unit

  (** [iteri_lines f file] calls [f line_num line] on every line [line]
     of the file [file], with [line_num] the line number, starting with
     line 0. *)
  val iteri_lines : (int -> string -> unit) -> in_file -> unit


  (** [copy_file src dst] copy all the content remaining in file [src]
     to file [dst]. *)
  val copy_file : in_file -> out_file -> unit


  (**/**)
  (* Obsolete functions *)
  val string_of_file : in_file -> string
  val lines_of_file : in_file -> string array
  val file_of_string : out_file -> string -> unit
  val string_of_subfile : in_file -> int -> int -> string
  val file_of_lines : out_file -> string array -> unit

  (* Optimized functions, not documented *)
  val read_lines_to_revlist :  in_file -> string list
  (**/**)

end


(** This modules provides functions to create directories, read, iter
   or remove directories, recursively or not.*)
module type DIRECTORY_OPERATIONS = sig

  type t
    (** Type of a filename. It is [string] in [FileString] and
        [FileGen.t] in [FileGen]. *)

  type selector
    (** Type of the value used to create filters when iterating on
        files and directories. *)

  exception NotADirectory of t
      (** This exception is raised when one of the following functions is
          called with a non-directory argument *)

  (** [make_dir ?mode ?p filename] creates a directory [filename], if it
      does not already exist. It fails with [NotADirectory] if the file
      already exists, but is not a directory.
      The [mode] argument is the Unix permissions (0o755 by default).
      The [p] argument controls whether parents directories should be
      created as well, if they don't exist, instead of failing. *)
  val make_dir : ?mode:int -> ?p:bool -> t -> unit

  (* selector to iter only on one directory *)
  val onedir : selector

  (* selector to iter recursively on a directory *)
  val recdir : selector

  (** [remove_dir ?all filename] removes directory [filename], or
     complains the [NotADirectory] if it does not exist. The [all]
     argument controls whether the function should recursively remove
     all files and sub-directories included as well. If [glob] is
     specified, it is called to select files to remove based on their
     basename, and the directories are not deleted even if [all] is
     [true].*)
  val remove_dir : ?all:bool -> ?glob:string -> t -> unit

  (** [select ?deep ?dft ?glob ?filter_rec ?filter_fun ?follow_links
     ?error ()] creates a selctor to customize a file iterator.

      The [deep] and [dft] arguments controls whether function should
     recurse (false by default) in sub-directories.  If [deep] is
     [true], and [~dft] is not specified, the files are listed in
     breadth-first mode ([a,b,a/x,b/x,a/x/y] for example).  If [~dft]
     is [`Before], the files are listed in depth-first mode, and the
     ancestors are before their children. If [~dft] is [`After], the
     are after their children.

      The [glob], [path] and [ignore] arguments can be used to filter
     the files with regular expressions. [glob] is used to choose the
     files to keep based on their basenames, it is not used for the
     recursion itself. [path] is used to choose the files to keep
     based on the path, it is not used for the recursion
     itself. [ignore] is used to choose the files to ignore, based on
     their paths, both during the iteration and the recursion.

      The [kinds] argument can be used to restrict the files to the
     ones of a particular kind.

      The [filter] argument is used to select the files to keep.
     [filter for_rec path] should return [true] if the file is ok,
     [false] otherwise. [for_rec] is [true] if [filter] is called to
     check a directory for recursion, [false] if it is called for the
     iteration.

      The [follow_links] argument is used to decide if a link to
     directory should be followed (when [deep] is also set).

      The [error] argument is called when an error occurs, with [error
     exn filename]. The default behavior is to ignore errors.  *)
  val select :
    ?deep:bool ->
    ?dft:[ `After | `Before ] ->
    ?glob:string -> (* check on basenames, not for recursion *)
    ?path:string -> (* check on paths, not for recursion *)
    ?ignore:string -> (* ignore these paths, always *)
    ?kinds: Unix.file_kind list ->
    ?filter:(bool -> string -> bool) ->
    ?follow_links:bool ->
    ?error:(exn -> t -> unit) -> unit -> selector

  (** [read_dir ?select dir] returns the files contained in the
     directory [dir], recursively. Filenames start from the root of
     the given directory, i.e. a full filename would be [dir / file].

     In a directory, files are sorted in lexicographical order of
     their names. *)
  val read_dir : ?select:selector -> t -> string array

  (** Same as [read_dir], but returns a list instead of an array *)
  val read_dir_to_list : ?select:selector -> t -> string list

  (** Same as [read_dir], but calls a function on every file and
     directory. It is not equivalent to using [read_dir] and then
     itering on the result, as [iter_dir] the function is called
     during the traversal, not after, so concurrent modifications to
     the directory might become visible.  *)
  val iter_dir :
    ?select:selector -> t -> f:(string -> unit) -> unit

  (** [iterator ?select dir] creates an iterator on directory [dir].
      The iterator is a function that returns [None] when finished,
      or [Some file] with the next file to iter on.
  *)
  val iterator : ?select:selector -> t -> (unit -> string option)

  (** [make_select f] transforms [f], a function that takes an
     optional selector, into a function that builds the selector on
     the fly.

      For example:
      let () =
         EzFile.make_select EzFile.iter_dir ~deep:true
            ~f:(fun path -> ...) dirname

      which is equivalent to:
      let () =
         let select = EzFile.select ~deep:true () in
         EzFile.iter_dir ~select
            ~f:(fun path -> ...) dirname
  *)

  val make_select :
    (?select:selector -> t -> 'a) ->
    ?deep:bool ->
    ?dft:[ `After | `Before ] ->
    ?glob:string -> (* check on basenames, not for recursion *)
    ?path:string -> (* check on paths, not for recursion *)
    ?ignore:string -> (* ignore these paths, always *)
    ?kinds: Unix.file_kind list ->
    ?filter:(bool -> string -> bool) ->
    ?follow_links:bool ->
    ?error:(exn -> t -> unit) ->
    t ->
    'a

  (** [mkdir filename mode] simply creates the directory [filename] with
      permissions [mode]. *)
  val mkdir : t -> int -> unit

  (** [readdir filename] returns the files contained in directory
     [filename] as an array of strings. The strings are sorted in
     lexicographical order. *)
  val readdir : t -> string array

  (** [rmdir filename] removes directory [filename], or fails if it
    does not exist or is not a directory. *)
  val rmdir : t -> unit

(**/**)
  val safe_mkdir : t -> unit
(**/**)

end

module type FILENAME_OPERATIONS = sig

  type t

(*

Operations on filenames

*)

  val concat : t -> t -> t
  val is_absolute : t -> bool
  val is_relative : t -> bool
  val is_implicit : t -> bool

  val add_suffix : t -> string -> t
  val check_suffix : t -> string -> bool

  (* [cut_extension file] returns a pair [ basename, extensions ],
     where [extensions] is the string of all extensions in lowercase
     (without the leading dot), and [basename] is the file basename
     without the extensions.  *)
  val cut_extension : t -> string * string

  (* [cut_extensions file] returns a pair [ basename, extensions ],
     where [extensions] is the list of extensions in lowercase
     (without dots), and [basename] is the file basename without the
     extensions.  *)
  val cut_extensions : t -> string * string list

  val basename : t -> string
  val dirname : t -> t
  val add_path : t -> string -> t
  val add_basename : t -> string -> t
  val add_basenames : t -> string list -> t

  val current_dir_name : t

(*

  Standard operations on files

*)


  val open_in : t -> in_channel
  val open_out : t -> out_channel
  val open_in_bin : t -> in_channel
  val open_out_bin : t -> out_channel
  val open_fd :
    t -> MinUnix.open_flag list -> MinUnix.file_perm -> MinUnix.file_descr
  val temp_file : t -> string -> t

  val with_in : t -> (in_channel -> unit) -> unit
  val with_in_bin : t -> (in_channel -> unit) -> unit
  val with_out : t -> (out_channel -> unit) -> unit
  val with_out_bin : t -> (out_channel -> unit) -> unit

  val exists : t -> bool
  val getcwd : unit -> t
  val size : t -> int
  val is_directory : t -> bool
  val is_link : t -> bool

  val remove : t -> unit
  val rename : t -> t -> unit

  val stat : t -> MinUnix.stats
  val lstat : t -> MinUnix.stats


  module OP : sig
    (* concatenate ('/' must be the only file separator in the string) *)
    val (//) : t -> string -> t
  end

(*

Copying files

*)

(*
  (* [safe_mkdir dirname] creates a directory [dirname], potentially
     creating any parent directory. A [~mode] argument can be
     provided, otherwise it is assumed to be 0o755. [safe_mkdir] can
     fail for wrong permissions, or if a directory name is already
     used by another kind of files.*)
  val safe_mkdir : ?mode:int -> t -> unit
*)

  (* [copy_rec src dst] creates [dst] as a copy of [src], even
     if [src] is a directory. *)
  val copy_rec : t -> t -> unit

  (* [uncopy_rec src dst] removes from [dst] everything that has the
     same name as in [src]. Can be seen as the inverse operation of
     [copy_rec]. *)
  val uncopy_rec : t -> t -> unit

(** [find_in_path path filename] searches a file in a list of directories. *)
  val find_in_path : string list -> string -> t


end

module type FILE_OPERATIONS = sig

  include FILENAME_OPERATIONS

  include (CONTENT_OPERATIONS with type in_file := t and type out_file := t)
  include (DIRECTORY_OPERATIONS with type t := t)

end