Source file fs_simulation.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
open Plebeia
open Result_lwt.Infix
open Fs_impl
module Simulation = struct
module Name = Name
module Path = Path
module FsError = FsError
open FsError
type name = Name.t
module Map = Map.Make(struct type t = name let compare (x : name) y = compare x y end)
type model_tree =
| File of Value.t
| Dir of model_tree Map.t
type raw_cursor = Z of model_tree * (name * model_tree Map.t) list
type cursor = raw_cursor
type view = model_tree
type hash = string
let rec pp_model_tree_map ppf m =
Utils.Format.list ";@ " (fun ppf (k, v) -> Format.fprintf ppf "@[<2>%a=@,%a@]" Name.pp k pp_view v)
ppf (Map.bindings m)
and pp_view ppf = function
| File v -> Format.fprintf ppf "File %a" Value.pp v
| Dir m -> Format.fprintf ppf "Dir [ %a ]" pp_model_tree_map m
let pp_trail ppf trail =
Format.fprintf ppf "trail: @[%a@]"
(Utils.Format.list ";@ " (fun ppf (k, m) -> Format.fprintf ppf "(@[<2>%a:@ %a@])" Name.pp k pp_model_tree_map m)) trail
let pp_cursor ppf (Z (t, trail)) =
Format.fprintf ppf "%a - %a" pp_view t pp_trail trail
module Context = struct end
let empty _ = Z (Dir Map.empty, [])
let make c _ = c
let error_fs e = Error (FS_error e)
module Op = struct
include Monad.Make1(struct
type 'a t = cursor -> (cursor * 'a, Error.t) result
let return a c = Ok (c, a)
let bind (aop : 'a t) (f : 'a -> 'b t) : 'b t =
fun c ->
match aop c with
| Error e ->
Error e
| Ok (c, a) ->
f a c
end)
let fail e _ = error_fs e
let get_raw_cursor c = c
let raw_cursor c = Ok (c,c)
let context _ = assert false
let get_path (Z (_, trail)) = List.rev_map fst trail
let path (Z (_, trail) as c) =
Ok (c, List.rev_map fst trail)
let view (Z (v, _) as c) = Ok (c, v)
let chdir_parent = function
| Z (_, []) as c -> Ok (c, ())
| Z (v, (n,rest)::trail) ->
assert (not @@ Map.mem n rest);
Ok (Z (Dir (Map.add n v rest), trail), ())
let rec chdir_root = function
| Z (_, []) as c -> Ok (c, ())
| c -> chdir_parent c >>? fun (c,()) -> chdir_root c
let seek path0 : (cursor * view) t = fun c ->
let rec seek path (Z (v, trail) as c) =
match path with
| [] ->
Ok (c, (c, v))
| n :: path ->
match v with
| Dir kvs ->
(match Map.find_opt n kvs with
| None -> error_fs (No_such_file_or_directory ("seek", path0))
| Some v ->
seek path (Z (v, (n, Map.remove n kvs) :: trail)))
| File _ ->
error_fs (Is_file ("seek", get_path c))
in
seek path0 c
let seek' path0 : Path.t t = fun c ->
let rec seek path (Z (v, trail) as c) = match path with
| [] -> assert false
| [n] ->
Ok (c, [n])
| n :: path ->
match v w
| Dir kvs ->
(match Map.find_opt n kvs with
| None -> Ok (c, n::path)
| Some v ->
seek path (Z (v, (n, Map.remove n kvs) :: trail)))
| File _ ->
error_fs (Is_file ("seek'", get_path c))
in
seek path0 c
let chdir ?(dig=false) path0 : unit t = fun c ->
let rec seek path (Z (v, trail) as c) = match path with
| [] -> Ok (c, ())
| n :: path ->
match v with
| Dir kvs ->
(match Map.find_opt n kvs with
| None when dig ->
seek path (Z (Dir Map.empty, (n, kvs) :: trail))
| None -> error_fs (No_such_file_or_directory ("chdir", get_path c @ [n]))
| Some v -> seek path (Z (v, (n, Map.remove n kvs) :: trail)))
| File _ ->
error_fs (Is_file ("chdir", get_path c))
in
seek path0 c
let with_pushd f c =
let path0 = get_path c in
f c >>? fun (c,res) ->
match Path.is_prefix_of path0 (get_path c) with
| None ->
Format.eprintf "with_pushd: %a %a@."
Path.pp path0
Path.pp (get_path c);
assert false
| Some p ->
let rec loop c = function
| 0 -> Ok (c, res)
| n -> chdir_parent c >>? fun (c, ()) -> loop c (n-1)
in
loop c @@ List.length p
let get p c =
with_pushd (seek p) c
let cat path c =
get path c >>? function
| (c, (_c, File v)) -> Ok (c, v)
| (_, (_, Dir _)) -> error_fs (Is_directory ("cat", path))
let write path0 value c =
with_pushd (fun c ->
seek' path0 c >>? fun (c, p) ->
view c >>? fun (c,v) ->
let Z (_, trail) = c in
match v with
| Dir kvs ->
begin match p with
| [] -> assert false
| k::ks ->
match Map.find_opt k kvs with
| None ->
let rec f = function
| [] -> File value
| k::ks -> Dir (Map.singleton k (f ks))
in
Ok (Z (Dir (Map.add k (f ks) kvs), trail), ())
| Some (File _) when ks = [] ->
Ok (Z (Dir (Map.add k (File value) kvs), trail), ())
| Some (Dir _) when ks = [] ->
error_fs (Is_directory ("write", get_path c))
| Some (File _)
| Some (Dir _) ->
assert false
end
| File _ ->
error_fs (Is_file ("write", get_path c))) c
let unlink path check c =
with_pushd (fun c ->
seek' path c >>? fun (c, p) ->
match p with
| [] -> assert false
| [n] ->
let Z (v, trail) = c in
begin match v with
| File _ ->
error_fs (Is_file ("unlink", get_path c))
| Dir kvs ->
match Map.find_opt n kvs with
| None -> Ok (c, false)
| Some v ->
let v' = match v with
| File _ -> `Leaf v
| Dir _ -> `Bud v
in
check v' >>? fun () ->
let c = Z (Dir (Map.remove n kvs), trail) in
let rec loop = function
| Z (Dir kvs, (_,rest)::trail) when Map.is_empty kvs ->
loop (Z (Dir rest, trail))
| Z (Dir kvs, []) when Map.is_empty kvs -> Ok (c, true)
| c -> Ok (c, true)
in
loop c
end
| _::_ ->
Ok (c, false)) c
let set path c' c =
view c' >>? fun (_, v) ->
match v with
| Dir kvs when Map.is_empty kvs ->
unlink path (fun _ -> Ok ()) c >|? fun (c,_) -> (c, ())
| Dir _ | File _ ->
with_pushd (fun c ->
seek' path c >>? fun (c, p) ->
match c with
| Z (File _, _) ->
error_fs (Is_file ("set", get_path c))
| Z (Dir kvs, trail) ->
begin match p with
| [] -> assert false
| k::ks ->
let rec f = function
| [] -> v
| k::ks -> Dir (Map.singleton k (f ks))
in
let ent = f ks in
Ok (Z (Dir (Map.add k ent kvs), trail), ())
end) c
let copy from to_ : unit t = fun c ->
get from c >>? fun (c, (c_from, _v_from)) ->
set to_ c_from c
let rm ?(recursive=false) ?(ignore_error=false) path c =
let check = function
| `Bud _ when not recursive -> error_fs (Is_directory ("rm", path))
| _ -> Ok ()
in
match unlink path check c with
| Ok (c, true) -> Ok (c,true)
| Ok (c, false) when ignore_error -> Ok (c,false)
| Ok (_, false) -> error_fs (No_such_file_or_directory ("rm", path))
| Error _ when ignore_error -> Ok (c,false)
| Error _ as e -> e
let rmdir ?(ignore_error=false) path c =
let check = function
| `Leaf _ -> error_fs (Is_file ("rmdir", path))
| _ -> Ok ()
in
match unlink path check c with
| Ok (c, true) -> Ok (c,true)
| Ok (c, false) when ignore_error -> Ok (c,false)
| Ok (_, false) -> error_fs (No_such_file_or_directory ("rm", path))
| Error _ when ignore_error -> Ok (c,false)
| Error _ as e -> e
let ls_no_lwt : Path.t -> (Name.t * cursor) list t =
fun path0 c ->
with_pushd (fun c ->
seek path0 c >>? function
| Z (File _, _), _ ->
error_fs (Is_file ("ls", path0))
| (Z (Dir m, trail) as c), _ ->
let rec f = function
| [] -> []
| (k,v)::kvs ->
(k, Z (v, (k, Map.remove k m) :: trail)) :: f kvs
in
Ok (c, f @@ Map.bindings m)) c
let do_then f (g : 'a t) = fun c -> f c; g c
let run c op = op c
let compute_hash c = Ok (c, "dummy")
let may_forget c = Ok (c, ())
end
module Op_lwt = struct
include Monad.Make1(struct
type 'a t = cursor -> (cursor * 'a, Error.t) result Lwt.t
let return a c = Lwt.return_ok (c, a)
let bind (aop : 'a t) (f : 'a -> 'b t) : 'b t =
fun c ->
Lwt.bind (aop c) (function
| Error _ as e -> Lwt.return e
| Ok (c, a) -> f a c)
end)
let lift : 'a Op.t -> 'a t = fun op c -> Lwt.return @@ op c
let ls : Path.t -> (Name.t * cursor) list t = fun path c ->
Lwt.return @@ Op.ls_no_lwt path c
let fold
: 'acc
-> Path.t
-> ('acc -> Path.t -> cursor -> ([`Continue | `Exit | `Up ] * 'acc, Error.t) result Lwt.t)
-> 'acc t
= fun init path f c ->
match Op.seek path c with
| Error e -> Lwt.return @@ Error e
| Ok (c, _) ->
let rec loop acc c =
Lwt.bind (f acc (Op.get_path c) c) @@ fun res ->
match res with
| Error e -> Lwt.return @@ Error e
| Ok (`Exit, acc) -> Lwt.return @@ Ok (`Exit, acc)
| Ok (`Up, acc) -> Lwt.return @@ Ok (`Continue, acc)
| Ok (`Continue, acc) ->
match c with
| Z (File _, _) -> Lwt.return @@ Ok (`Continue, acc)
| Z (Dir m, trail) ->
let rec loop2 acc = function
| [] -> Lwt.return @@ Ok (`Continue, acc)
| (k,v)::kvs ->
Lwt.bind (loop acc (Z (v,(k,Map.remove k m)::trail))) @@ function
| Error e -> Lwt.return @@ Error e
| Ok (`Exit, acc) -> Lwt.return @@ Ok (`Exit, acc)
| Ok (`Up, acc) -> Lwt.return @@ Ok (`Up, acc)
| Ok (`Continue, acc) -> loop2 acc kvs
in
loop2 acc @@ Map.bindings m
in
Lwt.bind (loop init c) @@ fun res ->
match res with
| Error e -> Lwt.return (Error e)
| Ok (_, acc) -> Lwt.return (Ok (c, acc))
let fold'
: ?depth:[< `Eq of int | `Ge of int | `Gt of int | `Le of int | `Lt of int ]
-> 'acc
-> Path.t
-> (Path.t -> cursor -> 'acc -> ('acc, Error.t) result Lwt.t)
-> 'acc t
= fun ?depth init path0 f ->
let check =
match depth with
| None -> fun _ -> true, true
| Some (`Eq n) -> fun x -> x < n, x = n
| Some (`Le n) -> fun x -> x < n, x <= n
| Some (`Lt n) -> fun x -> x < n-1, x < n
| Some (`Ge n) -> fun x -> true, x >= n
| Some (`Gt n) -> fun x -> true, x > n
in
fold init path0 @@ fun acc path c ->
let depth = Path.length path in
let deeper, callf = check depth in
Lwt.bind (if callf then f path c acc else Lwt.return (Ok acc)) @@ function
| Error e -> Lwt.return @@ Error e
| Ok acc ->
let command = if deeper then `Continue else `Up in
Lwt.return @@ Ok (command, acc)
end
end
module WithSimulation = struct
module F = Fs_impl
module S = Simulation
module Name = F.Name
module Path = F.Path
include F.FsError
type name = Name.t
type cursor = F.cursor * S.cursor
type raw_cursor = cursor
type view = F.view * S.view
type hash = F.hash
let empty ctxt conv = F.empty ctxt conv, S.empty ctxt
let make _ _ = assert false
type 'a op_lwt = cursor -> (cursor * 'a, Error.t) result Lwt.t
let error_fs e = Error (FS_error e)
let handle_conflict n (res, res') =
match res, res' with
| Ok _, Error e ->
Format.eprintf "%s: F: Ok, S: Error %a@." n Error.pp e;
assert false
| Error e, Ok _ ->
Format.eprintf "%s: F: Error %a, S: Ok@." n Error.pp e;
assert false
| Error e, Error _ -> Error e
| _ -> assert false
module Op = struct
type 'a t = cursor -> (cursor * 'a, Error.t) result
let fail e _ = error_fs e
let get_raw_cursor c = c
let get_real_cursor f = F.get_raw_cursor f
let get_model_tree (Z (mt, _) : S.cursor) = mt
let context (fc,_) = F.context fc
let raw_cursor c = Ok (c,c)
let path (c, c') =
match F.Op.path c, S.Op.path c' with
| Ok (c, p), Ok (c', p') when p = p' -> Ok ((c,c'), p)
| Ok (_c, p), Ok (_c', p') ->
Format.eprintf "path: F: %a, S: %a@." Path.pp p Path.pp p';
assert false
| res, res' -> handle_conflict "path" (res, res')
let chdir_parent (c, c') =
match F.Op.chdir_parent c, S.Op.chdir_parent c' with
| Ok (c, ()), Ok (c', ()) -> Ok ((c,c'), ())
| res -> handle_conflict "chdir_parent" res
let chdir_root (c, c') =
match F.Op.chdir_root c, S.Op.chdir_root c' with
| Ok (c, ()), Ok (c', ()) -> Ok ((c,c'), ())
| res -> handle_conflict "chdir_root" res
let chdir ?dig path (c, c') =
match F.Op.chdir ?dig path c, S.Op.chdir ?dig path c' with
| Ok (c, ()), Ok (c', ()) -> Ok ((c,c'), ())
| res -> handle_conflict "chdir" res
let get segs (c, c') =
match F.Op.get segs c, S.Op.get segs c' with
| Ok (c, (c_, v)), Ok (c', (c'_, v')) ->
(match v, v' with
| Leaf (value, _, _), File value' when value = value' ->
Ok ((c, c'), ((c_,c'_), (v, v')))
| Bud _, Dir _ ->
Ok ((c, c'), ((c_,c'_), (v, v')))
| _ -> assert false)
| x -> handle_conflict "get" x
let cat segs (c, c') =
match F.Op.cat segs c, S.Op.cat segs c' with
| Ok (c, v), Ok (c', v') ->
assert (v = v');
Ok ((c,c'),v)
| x -> handle_conflict "cat" x
let write segs0 v (c, c') =
match
F.Op.write segs0 v c,
S.Op.write segs0 v c'
with
| Ok (c, ()), Ok (c', ()) ->
Ok ((c,c'), ())
| x -> handle_conflict "write" x
let set segs0 (d,d') (c,c') =
match
F.Op.set segs0 d c,
S.Op.set segs0 d' c'
with
| Ok (c, ()), Ok (c', ()) ->
Ok ((c,c'), ())
| x -> handle_conflict "set" x
let copy from to_ (c,c') =
match
F.Op.copy from to_ c,
S.Op.copy from to_ c'
with
| Ok (c, ()), Ok (c', ()) ->
Ok ((c,c'), ())
| x -> handle_conflict "copy" x
let rm ?recursive ?ignore_error segs (c, c') =
match
F.Op.rm ?recursive ?ignore_error segs c,
S.Op.rm ?recursive ?ignore_error segs c'
with
| Ok (c, b), Ok (c', b') ->
assert (b = b');
Ok ((c, c'), b)
| x -> handle_conflict "rm" x
let rmdir ?ignore_error segs (c, c') =
match
F.Op.rmdir ?ignore_error segs c,
S.Op.rmdir ?ignore_error segs c'
with
| Ok (c, b), Ok (c', b') ->
assert (b = b');
Ok ((c, c'), b)
| x -> handle_conflict "rm" x
module Monad = Monad.Make1(struct
type nonrec 'a t = 'a t
let bind (aop : 'a t) (f : 'a -> 'b t) : 'b t = fun c ->
match aop c with
| Error e ->
Error e
| Ok (c, a) ->
f a c
let return a c = Ok (c, a)
end)
let do_then f (g : 'a t) = fun c -> f c; g c
let run c op = op c
end
module Op_lwt = struct
include Monad.Make1(struct
type 'a t = cursor -> (cursor * 'a, Error.t) result Lwt.t
let return a c = Lwt.return_ok (c, a)
let bind (aop : 'a t) (f : 'a -> 'b t) : 'b t =
fun c ->
Lwt.bind (aop c) (function
| Error _ as e -> Lwt.return e
| Ok (c, a) -> f a c)
end)
let lift : 'a Op.t -> 'a t = fun op c -> Lwt.return @@ op c
let ls segs0 (c, c') =
Lwt.bind (F.Op_lwt.ls segs0 c) @@ fun res ->
Lwt.bind (S.Op_lwt.ls segs0 c') @@ fun res' ->
Lwt.return @@
match res, res' with
| Ok (c,kvs), Ok (c',kvs') ->
let kvs = List.sort (fun (k,_v) (k',_v') -> compare k k') kvs in
let kvs' = List.sort (fun (k,_v) (k',_v') -> compare k k') kvs' in
let ks = List.map fst kvs in
let ks' = List.map fst kvs' in
if not (Path.equal ks ks') then begin
Format.eprintf "ls F: [%a] S: [%a]@." F.Path.pp ks F.Path.pp ks';
assert false
end;
Ok ((c,c'), List.map2 (fun (k,v) (_k',v') -> (k, (v,v'))) kvs kvs')
| x -> handle_conflict "ls" x
let fold init path (f,f') : _ op_lwt = fun (c,c') ->
Lwt.bind (F.Op_lwt.fold init path f c) @@ fun res ->
Lwt.bind (S.Op_lwt.fold init path f' c') @@ fun res' ->
Lwt.return @@
match res, res' with
| Ok (c,acc), Ok (c',_acc') -> Ok ((c,c'),acc)
| x -> handle_conflict "fold" x
let fold' ?depth init path (f,f') : _ op_lwt = fun (c,c') ->
Lwt.bind (F.Op_lwt.fold' ?depth init path f c) @@ fun res ->
Lwt.bind (S.Op_lwt.fold' ?depth init path f' c') @@ fun res' ->
Lwt.return @@
match res, res' with
| Ok (c,acc), Ok (c',acc') ->
Ok ((c,c'),(acc, acc'))
| x -> handle_conflict "fold'" x
end
let use_f f (c,c') =
match f c with
| Ok (c, res) -> Ok ((c,c'), res)
| Error e -> Error e
let write_top_cursor x = use_f F.write_top_cursor x
let compute_hash x = use_f F.Op.compute_hash x
let may_forget x = use_f F.Op.may_forget x
end