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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
let error_msgf fmt = Fmt.kstr (fun msg -> Error (`Msg msg)) fmt
let msgf fmt = Fmt.kstr (fun msg -> `Msg msg) fmt
let src = Logs.Src.create "mfat.fancy"
let ( let* ) = Result.bind
module Log = (val Logs.src_log src : Logs.LOG)
module S = Set.Make (String)
module M = Map.Make (String)
let of_fpath ~where path =
if Fpath.is_root path then ""
else
let segs = Fpath.segs path in
let trim = List.drop_while (( = ) "") in
let segs = trim segs in
let segs = List.rev (trim (List.rev segs)) in
match segs with
| [] -> Fmt.failwith "%s: empty path: %a" where Fpath.pp path
| segs when List.exists (( = ) "") segs ->
Fmt.failwith "%s: empty segment: %a" where Fpath.pp path
| segs -> String.concat "/" segs
module Pat = struct
let err_malformed_pat = Fmt.str "malformed named string pattern: %S"
type lexeme = Lit of string | Var of string
type t = lexeme list
type parse_state = S_lit | S_dollar | S_var
let of_string s =
let b = Buffer.create 255 in
let flush b =
let s = Buffer.contents b in
Buffer.clear b; s
in
let err () = Error (`Msg (err_malformed_pat s)) in
let push_lit b acc =
if Buffer.length b <> 0 then Lit (flush b) :: acc else acc
in
let max_i = String.length s - 1 in
let rec loop acc state i =
if i > max_i then
if state <> S_lit then err () else Ok (List.rev (push_lit b acc))
else
match state with
| S_lit ->
begin match s.[i] with
| '$' -> loop acc S_dollar (i + 1)
| c ->
Buffer.add_char b c;
loop acc S_lit (i + 1)
end
| S_dollar ->
begin match s.[i] with
| '$' ->
Buffer.add_char b '$';
loop acc S_lit (i + 1)
| '(' -> loop (push_lit b acc) S_var (i + 1)
| _ -> err ()
end
| S_var ->
begin match s.[i] with
| ')' -> loop (Var (flush b) :: acc) S_lit (i + 1)
| ',' -> err ()
| c ->
Buffer.add_char b c;
loop acc S_var (i + 1)
end
in
loop [] S_lit 0
let v s = match of_string s with Ok v -> v | Error (`Msg e) -> invalid_arg e
let to_string p =
let b = Buffer.create 255 in
let add = function
| Lit l ->
let max_i = String.length l - 1 in
let rec loop start i =
if i > max_i then Buffer.add_substring b l start (i - start)
else if l.[i] <> '$' then loop start (i + 1)
else begin
Buffer.add_substring b l start (i - start + 1);
Buffer.add_char b '$';
let next = i + 1 in
loop next next
end
in
loop 0 0
| Var v -> Buffer.(add_string b "$("; add_string b v; add_char b ')')
in
List.iter add p; Buffer.contents b
type defs = string M.t
let _subst ?(undef = fun _ -> None) defs p =
let subst acc = function
| Lit _ as l -> l :: acc
| Var v as var -> (
match M.find v defs with
| Some lit -> Lit lit :: acc
| None -> (
match undef v with
| Some lit -> Lit lit :: acc
| None -> var :: acc))
in
List.(rev (fold_left subst [] p))
let _format ?(undef = fun _ -> "") defs p =
let b = Buffer.create 255 in
let add = function
| Lit l -> Buffer.add_string b l
| Var v ->
begin match M.find v defs with
| Some s -> Buffer.add_string b s
| None -> Buffer.add_string b (undef v)
end
in
List.iter add p; Buffer.contents b
let match_literal pos s lit =
let l_len = String.length lit in
let s_len = String.length s - pos in
if l_len > s_len then None
else
try
for i = 0 to l_len - 1 do
if lit.[i] <> s.[pos + i] then raise Exit
done;
Some (pos + l_len)
with Exit -> None
let with_index_range ?(first = 0) ?last str =
let str_len = String.length str in
let max_idx = str_len - 1 in
let last =
match last with
| None -> max_idx
| Some last -> if last > max_idx then max_idx else last
in
let first = if first < 0 then 0 else first in
if first > max_idx || last < 0 || first > last then ""
else if first = 0 && last = max_idx then str
else String.sub str first (last + 1 - first)
let match_pat ~env pos s pat =
let init, no_env =
match env with
| None -> (Some M.empty, true)
| Some _ as init -> (init, false)
in
let rec loop pos = function
| [] -> if pos = String.length s then init else None
| Lit lit :: p ->
begin match match_literal pos s lit with
| None -> None
| Some pos -> loop pos p
end
| Var n :: p ->
let rec try_match next_pos =
if next_pos < pos then None
else
match loop next_pos p with
| None -> try_match (next_pos - 1)
| Some m as r ->
if no_env then r
else
let first = pos and last = next_pos - 1 in
let str = with_index_range s ~first ~last in
Some (M.add n str m)
in
try_match (String.length s)
in
loop pos pat
let matches p s = match_pat ~env:None 0 s p <> None
let query ?(init = M.empty) p s = match_pat ~env:(Some init) 0 s p
end
module Make (Blk : Mfat.BLOCK) = struct
include Mfat.Make (Blk)
let file_exists t filepath =
let filepath = of_fpath ~where:"file_exists" filepath in
match stat t filepath with
| Ok { Mfat.is_dir= false; _ } -> Ok true
| Ok _ -> Ok false
| Error _ -> Ok false
let dir_exists t dirpath =
let dirpath = of_fpath ~where:"dir_exists" dirpath in
match stat t dirpath with
| Ok { Mfat.is_dir= true; _ } -> Ok true
| Ok _ -> Ok false
| Error _ -> Ok false
let exists t path =
let path = of_fpath ~where:"exists" path in
match stat t path with Ok _ -> Ok true | Error _ -> Ok false
let file_must_exist t filepath =
let filepath' = of_fpath ~where:"file_must_exist" filepath in
match stat t filepath' with
| Ok { Mfat.is_dir= false; _ } -> Ok filepath
| Ok _ -> error_msgf "%a: Not a file" Fpath.pp filepath
| Error (`Msg msg) ->
error_msgf "file %a must exist: %s" Fpath.pp filepath msg
let dir_must_exist t dirpath =
let dirpath' = of_fpath ~where:"dir_must_exist" dirpath in
match stat t dirpath' with
| Ok { Mfat.is_dir= true; _ } -> Ok dirpath
| Ok _ -> error_msgf "%a: Not a directory" Fpath.pp dirpath
| Error (`Msg msg) ->
error_msgf "directory %a must exist: %s" Fpath.pp dirpath msg
let must_exist t path =
let path' = of_fpath ~where:"must_exist" path in
match stat t path' with
| Ok { Mfat.is_dir= true; _ } -> Ok (Fpath.to_dir_path path)
| Ok _ -> Ok path
| Error (`Msg msg) -> error_msgf "%a must exist: %s" Fpath.pp path msg
let delete_file t ?(must_exist = false) filepath =
let filepath' = of_fpath ~where:"delete_file" filepath in
match stat t filepath' with
| Ok { Mfat.is_dir= false; _ } ->
let on_error (`Msg msg) =
Log.err (fun m ->
m "Unexpected error while removing %a: %s" Fpath.pp filepath msg)
in
let result = remove t filepath' in
Result.iter_error on_error result;
Ok ()
| Ok { Mfat.is_dir= true; _ } ->
error_msgf "%a is not a file" Fpath.pp filepath
| Error _ when must_exist ->
error_msgf "%a does not exist" Fpath.pp filepath
| Error _ -> Ok ()
let delete_dir t ?must_exist:(must = false) ?(recurse = false) dirpath =
let rec delete_files to_rmdir dirs =
match dirs with
| [] -> Ok to_rmdir
| dir :: todo ->
let delete_dir_files dirs entry =
match (dirs, entry) with
| (Error _ as err), _ -> err
| Ok dirs, { Mfat.is_dir= true; name; _ } ->
Ok (Fpath.(to_dir_path (dir / name)) :: dirs)
| Ok dirs, { name; _ } ->
let filepath = Fpath.(dir / name) in
let filepath' = of_fpath ~where:"delete_dir" filepath in
let* () =
remove t filepath'
|> Result.map_error @@ fun _ ->
msgf "Impossible to remove %a" Fpath.pp filepath
in
Ok dirs
in
let* entries =
ls t (of_fpath ~where:"delete_dir" dir)
|> Result.map_error @@ fun _ ->
msgf "Impossible to get entries from %a" Fpath.pp
(Fpath.to_dir_path dir)
in
let* dirs = List.fold_left delete_dir_files (Ok []) entries in
delete_files (dir :: to_rmdir) (List.rev_append dirs todo)
in
let rec delete_dirs = function
| [] -> Ok ()
| dir :: dirs ->
let* () =
remove t (of_fpath ~where:"delete_dir" dir)
|> Result.map_error @@ fun _ ->
msgf "Impossible to remote %a" Fpath.pp (Fpath.to_dir_path dir)
in
delete_dirs dirs
in
let delete recurse dir =
if not recurse then remove t (of_fpath ~where:"delete_dir" dir)
else
let* rmdirs = delete_files [] [ dir ] in
delete_dirs rmdirs
in
let* dirpath = if must then dir_must_exist t dirpath else Ok dirpath in
delete recurse dirpath
let delete t ?(must_exist = false) ?(recurse = false) path =
let path' = of_fpath ~where:"delete" path in
match stat t path' with
| Ok { Mfat.is_dir= true; _ } -> delete_dir t ~must_exist ~recurse path
| Ok _ -> delete_file t ~must_exist path
| Error _ when not must_exist -> Ok ()
| Error _ -> error_msgf "%a does not exist" Fpath.pp path
let stat t path =
match stat t (of_fpath ~where:"stat" path) with
| Ok _ as value -> value
| Error _ -> error_msgf "%a does not exist" Fpath.pp path
type 'a error =
Fpath.t
-> ('a, [ `Msg of string ]) result
-> (unit, [ `Msg of string ]) result
type elements =
[ `Any
| `Files
| `Dirs
| `Sat of blk Mfat.t -> Fpath.t -> (bool, [ `Msg of string ]) result ]
type traverse =
[ `Any
| `None
| `Sat of blk Mfat.t -> Fpath.t -> (bool, [ `Msg of string ]) result ]
exception Fold_error of [ `Msg of string ]
let errfn err fn ~backup =
fun t path ->
match fn t path with
| Ok v -> v
| Error _ as value -> (
match err path value with
| Ok () -> backup
| Error err -> raise (Fold_error err))
let err_predicate_fn err fn t = errfn err fn t ~backup:false
let do_traverse_fn err = function
| `Any -> fun _ _ -> true
| `None -> fun _ _ -> false
| `Sat sat -> err_predicate_fn err sat
let is_element_fn err = function
| `Any -> err_predicate_fn err exists
| `Files -> err_predicate_fn err file_exists
| `Dirs -> err_predicate_fn err dir_exists
| `Sat sat -> err_predicate_fn err sat
let is_dir_fn err =
let is_dir t path =
match stat t path with
| Ok { Mfat.is_dir= true; _ } -> Ok true
| Ok _ -> Ok false
| Error _ -> error_msgf "Impossible to describe %a" Fpath.pp path
in
err_predicate_fn err is_dir
let readdir_fn err =
let readdir t d =
match ls t (of_fpath ~where:"readdir" d) with
| Ok entries ->
let fn { Mfat.name; _ } =
if name = "." || name = ".." then None else Some name
in
let names = List.filter_map fn entries in
Ok names
| Error _ as err -> err
in
errfn err readdir ~backup:[]
let is_dotfile bname = String.length bname > 0 && bname.[0] = '.'
let log path = function
| Ok _ -> assert false
| Error (`Msg msg) ->
Log.err (fun m -> m "Mfat.fold (%a): %s" Fpath.pp path msg);
Ok ()
let fold ?(err = log) ?(dotfiles = false) ?(elements = `Any)
?(traverse = `Any) t fn acc paths =
try
let do_traverse = do_traverse_fn err traverse in
let is_element = is_element_fn err elements in
let is_dir = is_dir_fn err in
let readdir = readdir_fn err in
let process_path path (acc, to_traverse) =
let acc = if is_element t path then fn path acc else acc in
let to_traverse =
if is_dir t path && do_traverse t path then path :: to_traverse
else to_traverse
in
(acc, to_traverse)
in
let dir_child d acc bname =
if (not dotfiles) && is_dotfile bname then acc
else process_path Fpath.(d / bname) acc
in
let rec go acc = function
| (d :: ds) :: up ->
let childs = readdir t d in
let acc, to_traverse =
List.fold_left (dir_child d) (acc, []) childs
in
go acc (to_traverse :: ds :: up)
| [] :: [] -> acc
| [] :: up -> go acc up
| _ -> assert false
in
let init acc path =
let base = Fpath.(basename (normalize path)) in
if (not dotfiles) && is_dotfile base then acc else process_path path acc
in
let acc, to_traverse = List.fold_left init (acc, []) paths in
Ok (go acc (to_traverse :: []))
with Fold_error err -> Error err
module File = struct
let exists t path = file_exists t path
let must_exist t filepath = file_must_exist t filepath
let delete t ?must_exist path = delete_file t ?must_exist path
let read t path =
let path' = of_fpath ~where:"File.read" path in
let* chunks = to_seq t path' in
let buf = Buffer.create 4096 in
Seq.iter (Buffer.add_string buf) chunks;
Ok (Buffer.contents buf)
let read_lines t path =
let path' = of_fpath ~where:"File.read_lines" path in
let* chunks = to_seq t path' in
let buf = Buffer.create 4096 in
Seq.iter (Buffer.add_string buf) chunks;
Ok (String.split_on_char '\n' (Buffer.contents buf))
let fold_lines f acc t path =
let path' = of_fpath ~where:"File.fold_lines" path in
let* chunks = to_seq t path' in
let buf = Buffer.create 4096 in
Seq.iter (Buffer.add_string buf) chunks;
let lines = String.split_on_char '\n' (Buffer.contents buf) in
Ok (List.fold_left (fun a l -> f l a) acc lines)
let write t path content =
let path' = of_fpath ~where:"File.write" path in
write t path' content
let write_lines t path lines =
let content = String.concat "\n" lines in
write t path content
end
module Dir = struct
let exists t path = dir_exists t path
let must_exist t path = dir_must_exist t path
let create t ?(path = true) dir =
let segs = Fpath.segs dir in
let segs = List.filter (fun str -> str <> "") segs in
if path then begin
let rec go current created = function
| [] -> Ok created
| seg :: rest ->
let next =
match current with
| None -> Fpath.v seg
| Some p -> Fpath.(p / seg)
in
let* is_dir = dir_exists t next in
if is_dir then go (Some next) created rest
else
let* is_file = file_exists t next in
if is_file then error_msgf "%a: not a directory" Fpath.pp next
else
let* () = mkdir t (of_fpath ~where:"Dir.create" next) in
go (Some next) true rest
in
go None false segs
end
else begin
let* is_dir = dir_exists t dir in
if is_dir then Ok false
else
let* is_file = file_exists t dir in
if is_file then error_msgf "%a: not a directory" Fpath.pp dir
else
let* () = mkdir t (of_fpath ~where:"Dir.create" dir) in
Ok true
end
let delete t ?must_exist ?recurse path =
delete_dir t ?must_exist ?recurse path
let contents t ?(rel = false) dir =
let dir' = of_fpath ~where:"Dir.contents" dir in
let* entries = ls t dir' in
let to_fpath entry =
let name = entry.Mfat.name in
let child = Fpath.v name in
if entry.Mfat.is_dir then
if rel then Fpath.to_dir_path child
else Fpath.(to_dir_path (dir // child))
else if rel then child
else Fpath.(dir // child)
in
Ok (List.map to_fpath entries)
let fold_contents ?err ?dotfiles ?elements ?traverse t f acc dir =
let* paths = contents t ~rel:false dir in
fold ?err ?dotfiles ?elements ?traverse t f acc paths
end
let matches t ?(dotfiles = false) pat =
let parent = Fpath.parent pat in
let base = Fpath.basename pat in
match Pat.of_string base with
| Error _ as e -> e
| Ok pat ->
let* entries = Dir.contents t ~rel:false parent in
let entries =
if not dotfiles then
List.filter
(fun p ->
let b = Fpath.basename p in
String.length b = 0 || b.[0] <> '.')
entries
else entries
in
let matching =
List.filter
(fun p ->
let b = Fpath.basename (Fpath.rem_empty_seg p) in
Pat.matches pat b)
entries
in
Ok matching
let query t ?(dotfiles = false) ?(init = M.empty) pat =
let parent = Fpath.parent pat in
let base = Fpath.basename pat in
match Pat.of_string base with
| Error _ as e -> e
| Ok pat ->
let* entries = Dir.contents t ~rel:false parent in
let entries =
if not dotfiles then
let fn path =
let bname = Fpath.basename path in
String.length bname = 0 || bname.[0] <> '.'
in
List.filter fn entries
else entries
in
let results =
let fn path =
let bname = Fpath.basename (Fpath.rem_empty_seg path) in
match Pat.query ~init pat bname with
| Some defs -> Some (path, defs)
| None -> None
in
List.filter_map fn entries
in
Ok results
end