Source file reader0.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
open Core
open Import
module Scheduler = Raw_scheduler
module Unix = Unix_syscalls
module Id = Unique_id.Int63 ()
module Read_result = struct
module Z = struct
type 'a t =
[ `Ok of 'a
| `Eof
]
[@@deriving bin_io, sexp]
let bind a ~f =
match a with
| `Ok a -> f a
| `Eof -> `Eof
;;
let map a ~f =
match a with
| `Ok a -> `Ok (f a)
| `Eof -> `Eof
;;
let map = `Custom map
let return a = `Ok a
end
include Z
include Monad.Make (Z)
end
module Internal = struct
module State = struct
type t =
[ `Not_in_use
| `In_use
| `Closed
]
[@@deriving sexp]
end
module Open_flags = Unix.Open_flags
type open_flags =
[ `Already_closed
| `Ok of Open_flags.t
| `Error of exn
]
[@@deriving sexp_of]
type t =
{ fd : Fd.t
; id : Id.t
; mutable bytes_read : Int63.t
;
mutable buf : Bigstring.t
;
mutable close_may_destroy_buf : [ `Yes | `Not_now | `Not_ever ]
;
mutable pos : int
;
mutable available : int
;
mutable state : State.t
; close_finished : unit Ivar.t
; mutable last_read_time : Time.t
;
open_flags : open_flags
}
[@@deriving fields]
let sexp_of_t t = [%sexp (t.fd : Fd.t_hum)]
type t_internals = t
let sexp_of_t_internals
{ available
; buf = _
; close_finished
; close_may_destroy_buf
; id
; fd
; bytes_read
; last_read_time
; open_flags
; pos
; state
}
=
let unless_testing x = Option.some_if (not Ppx_inline_test_lib.am_running) x in
[%sexp
{ id = (id |> unless_testing : (Id.t option[@sexp.option]))
; state : State.t
; available : int
; pos : int
; open_flags = (open_flags |> unless_testing : (open_flags option[@sexp.option]))
; last_read_time =
(last_read_time |> unless_testing : (Time.t option[@sexp.option]))
; close_may_destroy_buf : [ `Yes | `Not_now | `Not_ever ]
; close_finished : unit Ivar.t
; fd = (fd |> unless_testing : (Fd.t option[@sexp.option]))
; bytes_read : Int63.t
}]
;;
let invariant t : unit =
assert (0 <= t.pos);
assert (0 <= t.available);
assert (t.pos + t.available <= Bigstring.length t.buf)
;;
let create ?buf_len fd =
let buf_len =
match buf_len with
| None ->
(match Fd.kind fd with
| Char | File -> 32 * 1024
| Fifo | Socket _ -> 128 * 1024)
| Some buf_len ->
if buf_len > 0
then buf_len
else
raise_s
[%message
"Reader.create got non positive buf_len" (buf_len : int) (fd : Fd.t)]
in
let open_flags =
Fd.syscall fd (fun file_descr -> Core_unix.fcntl_getfl file_descr)
in
{ fd
; id = Id.create ()
; bytes_read = Int63.zero
; buf = Bigstring.create buf_len
; close_may_destroy_buf = `Yes
; pos = 0
; available = 0
; state = `Not_in_use
; close_finished = Ivar.create ()
; last_read_time = Scheduler.cycle_start ()
; open_flags
}
;;
let of_in_channel ic kind = create (Fd.of_in_channel ic kind)
let open_file ?buf_len file =
let%map fd = Unix.openfile file ~mode:[ `Rdonly ] ~perm:0o000 in
create fd ?buf_len
;;
let stdin = lazy (create (Fd.stdin ()))
let close_finished t = Ivar.read t.close_finished
let is_closed t =
match t.state with
| `Closed -> true
| `Not_in_use | `In_use -> false
;;
let empty_buf = Bigstring.create 0
let destroy t =
Bigstring.unsafe_destroy t.buf;
t.buf <- empty_buf
;;
let close t =
(match t.state with
| `Closed -> ()
| `Not_in_use | `In_use ->
t.state <- `Closed;
upon (Unix.close t.fd) (fun () -> Ivar.fill t.close_finished ());
t.pos <- 0;
t.available <- 0;
(match t.close_may_destroy_buf with
| `Yes -> destroy t
| `Not_now | `Not_ever -> ()));
close_finished t
;;
let with_close t ~f =
Monitor.protect
~run:`Schedule
f
~finally:(fun () -> close t)
;;
let with_reader_exclusive t f =
let%bind () = Unix.lockf t.fd Shared in
Monitor.protect
~run:`Schedule
f
~finally:(fun () ->
if not (Fd.is_closed t.fd) then Unix.unlockf t.fd;
return ())
;;
let with_file ?buf_len ?(exclusive = false) file ~f =
let%bind t = open_file ?buf_len file in
with_close t ~f:(fun () ->
if exclusive then with_reader_exclusive t (fun () -> f t) else f t)
;;
let get_data t : [ `Ok | `Eof ] Deferred.t =
Deferred.create (fun result ->
let eof () = Ivar.fill result `Eof in
match t.state, t.open_flags with
| `Not_in_use, _ -> assert false
| `Closed, _ | _, `Already_closed -> eof ()
| `In_use, ((`Error _ | `Ok _) as open_flags) ->
let can_read_fd =
match open_flags with
| `Error _ -> false
| `Ok open_flags -> Unix.Open_flags.can_read open_flags
in
if not can_read_fd
then
raise_s
[%message
"not allowed to read due to file-descriptor flags"
(open_flags : open_flags)
~reader:(t : t)];
let ebadf () =
raise_s
[%message "reader file descriptor was unexpectedly closed" ~reader:(t : t)]
in
let finish res handle =
match res with
| `Already_closed -> eof ()
| `Error exn ->
(match exn with
| Bigstring_unix.IOError (0, End_of_file)
| Unix.Unix_error
( ( ECONNRESET
| EHOSTUNREACH
| ENETDOWN
| ENETRESET
| ENETUNREACH
| EPIPE
| ETIMEDOUT )
, _
, _ ) -> eof ()
| Unix.Unix_error (EBADF, _, _) -> ebadf ()
| _ -> handle exn)
| `Ok (bytes_read, read_time) ->
t.bytes_read <- Int63.(t.bytes_read + of_int bytes_read);
if bytes_read = 0
then eof ()
else (
t.pos <- 0;
t.available <- t.available + bytes_read;
t.last_read_time <- read_time;
Ivar.fill result `Ok)
in
let buf = t.buf in
if t.available > 0 && t.pos > 0
then (
Bigstring.blit ~src:buf ~src_pos:t.pos ~dst:buf ~dst_pos:0 ~len:t.available;
t.pos <- 0);
let pos = t.available in
let len = Bigstring.length buf - pos in
if not (Fd.supports_nonblock t.fd)
then (
(match t.close_may_destroy_buf with
| `Yes -> t.close_may_destroy_buf <- `Not_now
| `Not_now | `Not_ever -> ());
Fd.syscall_in_thread t.fd ~name:"read" (fun file_descr ->
let res = Bigstring_unix.read file_descr buf ~pos ~len in
res, Time.now ())
>>> fun res ->
(match t.close_may_destroy_buf with
| `Not_now -> t.close_may_destroy_buf <- `Yes
| `Yes | `Not_ever -> ());
match t.state with
| `Not_in_use -> assert false
| `In_use -> finish res raise
| `Closed ->
destroy t;
eof ())
else (
let rec loop () =
Fd.ready_to t.fd `Read
>>> function
| `Bad_fd -> ebadf ()
| `Closed -> eof ()
| `Ready ->
(match t.state with
| `Not_in_use -> assert false
| `Closed -> eof ()
| `In_use ->
finish
(Fd.syscall t.fd ~nonblocking:true (fun file_descr ->
let res =
Unix.Syscall_result.Int.ok_or_unix_error_exn
(Bigstring_unix.read_assume_fd_is_nonblocking
file_descr
buf
~pos
~len)
~syscall_name:"read"
in
res, Scheduler.cycle_start ()))
(function
| Unix.Unix_error ((EWOULDBLOCK | EAGAIN), _, _) -> loop ()
| exn -> raise exn))
in
loop ()))
;;
let ensure_buf_len t ~at_least =
let buf_len = Bigstring.length t.buf in
if buf_len < at_least
then (
let new_buf = Bigstring.create (Int.max at_least (2 * Bigstring.length t.buf)) in
if t.available > 0
then
Bigstring.blit ~src:t.buf ~src_pos:t.pos ~len:t.available ~dst:new_buf ~dst_pos:0;
t.buf <- new_buf;
t.pos <- 0);
assert (Bigstring.length t.buf >= at_least)
;;
let get_data_until t ~available_at_least =
if t.available >= available_at_least
then return `Ok
else (
ensure_buf_len t ~at_least:available_at_least;
if t.pos > 0
then (
Bigstring.blit ~src:t.buf ~src_pos:t.pos ~dst:t.buf ~dst_pos:0 ~len:t.available;
t.pos <- 0);
let rec loop () =
let%bind result = get_data t in
if t.available >= available_at_least
then return `Ok
else (
match result with
| `Eof -> return (`Eof t.available)
| `Ok -> loop ())
in
loop ())
;;
let with_nonempty_buffer (type a) t (f : [ `Ok | `Eof ] -> a) : a Deferred.t =
match t.state with
| `Not_in_use -> assert false
| `Closed -> return (f `Eof)
| `In_use ->
if t.available > 0
then return (f `Ok)
else (
let%map ok_or_eof = get_data t in
match t.state with
| `Not_in_use -> assert false
| `Closed -> f `Eof
| `In_use -> f ok_or_eof)
;;
let with_nonempty_buffer' ?(force_refill = false) t (f : [ `Ok | `Eof ] -> unit) : unit =
match t.state with
| `Not_in_use -> assert false
| `Closed -> f `Eof
| `In_use ->
if (not force_refill) && t.available > 0
then f `Ok
else
get_data t
>>> fun ok_or_eof ->
(match t.state with
| `Not_in_use -> assert false
| `Closed -> f `Eof
| `In_use -> f ok_or_eof)
;;
let consume t amount =
assert (0 <= amount && amount <= t.available);
t.pos <- t.pos + amount;
t.available <- t.available - amount
;;
type 'a handle_chunk_result =
[ `Stop of 'a
| `Stop_consumed of 'a * int
| `Continue
| `Consumed of int * [ `Need of int | `Need_unknown ]
]
[@@deriving sexp_of]
type 'a read_one_chunk_at_a_time_result =
[ `Eof
| `Stopped of 'a
| `Eof_with_unconsumed_data of string
]
[@@deriving sexp_of]
type consumed = [ `Consumed of int * [ `Need of int | `Need_unknown ] ]
[@@deriving sexp_of]
let read_one_chunk_at_a_time t ~handle_chunk =
t.close_may_destroy_buf <- `Not_ever;
Deferred.create (fun final_result ->
let rec loop ~force_refill =
with_nonempty_buffer' t ~force_refill (function
| `Eof ->
let result =
if t.available > 0
then
`Eof_with_unconsumed_data
(Bigstring.to_string t.buf ~pos:t.pos ~len:t.available)
else `Eof
in
Ivar.fill final_result result
| `Ok ->
let len = t.available in
let continue z =
match t.state with
| `Not_in_use -> assert false
| `Closed -> Ivar.fill final_result `Eof
| `In_use ->
(match z with
| `Stop a ->
consume t len;
Ivar.fill final_result (`Stopped a)
| `Stop_consumed (a, consumed) ->
consume t consumed;
Ivar.fill final_result (`Stopped a)
| `Continue ->
consume t len;
loop ~force_refill:true
| `Consumed (consumed, need) as c ->
if consumed < 0
|| consumed > len
||
match need with
| `Need_unknown -> false
| `Need need -> need < 0 || consumed + need <= len
then
raise_s
[%message
"handle_chunk returned invalid `Consumed"
~_:(c : consumed)
(len : int)
~reader:(t : t)];
consume t consumed;
let buf_len = Bigstring.length t.buf in
let new_len =
match need with
| `Need_unknown ->
if t.available = buf_len
then buf_len * 2
else buf_len
| `Need need ->
if need > buf_len then Int.max need (buf_len * 2) else buf_len
in
if new_len < 0
then
raise_s
[%message
"read_one_chunk_at_a_time got overflow in buffer len"
~reader:(t : t_internals)];
if new_len > buf_len
then (
let new_buf = Bigstring.create new_len in
if t.available > 0
then
Bigstring.blit
~src:t.buf
~src_pos:t.pos
~len:t.available
~dst:new_buf
~dst_pos:0;
t.buf <- new_buf;
t.pos <- 0);
loop ~force_refill:true)
in
let deferred = handle_chunk t.buf ~pos:t.pos ~len in
(match Deferred.peek deferred with
| None -> deferred >>> continue
| Some result -> continue result))
in
loop ~force_refill:false)
;;
type 'a handle_iobuf_result =
[ `Stop of 'a
| `Continue
]
[@@deriving sexp_of]
let read_one_iobuf_at_a_time t ~handle_chunk =
let iobuf = Iobuf.of_bigstring t.buf in
read_one_chunk_at_a_time t ~handle_chunk:(fun bstr ~pos ~len ->
Iobuf.Expert.reinitialize_of_bigstring iobuf bstr ~pos ~len;
let%map handle_result = handle_chunk iobuf in
if Iobuf.is_empty iobuf
then (handle_result :> _ handle_chunk_result)
else (
let consumed = len - Iobuf.length iobuf in
match handle_result with
| `Continue -> `Consumed (consumed, `Need_unknown)
| `Stop a -> `Stop_consumed (a, consumed)))
;;
module Read
(S : Substring_intf.S) (Name : sig
val name : string
end) =
struct
let read_available t s =
let len = Int.min t.available (S.length s) in
S.blit_from_bigstring s ~src:t.buf ~src_pos:t.pos ~len;
consume t len;
len
;;
let read t s =
if S.length s = 0 then invalid_argf "Reader.read_%s with empty string" Name.name ();
with_nonempty_buffer t (function
| `Ok -> `Ok (read_available t s)
| `Eof -> `Eof)
;;
let really_read t s =
Deferred.create (fun result ->
let rec loop s amount_read =
if S.length s = 0
then Ivar.fill result `Ok
else
read t s
>>> function
| `Eof -> Ivar.fill result (`Eof amount_read)
| `Ok len -> loop (S.drop_prefix s len) (amount_read + len)
in
loop s 0)
;;
end
module Read_substring =
Read
(Substring)
(struct
let name = "substring"
end)
let read_substring_available = Read_substring.read_available
let read_substring = Read_substring.read
let really_read_substring = Read_substring.really_read
module Read_bigsubstring =
Read
(Bigsubstring)
(struct
let name = "bigsubstring"
end)
let read_bigsubstring = Read_bigsubstring.read
let really_read_bigsubstring = Read_bigsubstring.really_read
let really_read_bigstring t bigstring =
really_read_bigsubstring t (Bigsubstring.create bigstring)
;;
let peek_available t ~len =
Bigstring.to_string t.buf ~pos:t.pos ~len:(Int.min len t.available)
;;
let peek t ~len =
match%map get_data_until t ~available_at_least:len with
| `Eof (_ : int) ->
assert (t.available < len);
`Eof
| `Ok ->
assert (t.available >= len);
`Ok (Bigstring.to_string t.buf ~pos:t.pos ~len)
;;
let read_available t ?pos ?len s =
read_substring_available t (Substring.create s ?pos ?len)
;;
let read t ?pos ?len s = read_substring t (Substring.create s ?pos ?len)
let really_read t ?pos ?len s = really_read_substring t (Substring.create s ?pos ?len)
let read_char t =
with_nonempty_buffer t (function
| `Eof -> `Eof
| `Ok ->
let c = t.buf.{t.pos} in
consume t 1;
`Ok c)
;;
let first_char t p ~available =
let limit = t.pos + available in
let buf = t.buf in
match p with
| `Pred p ->
let rec loop pos =
if pos = limit then None else if p buf.{pos} then Some pos else loop (pos + 1)
in
Or_error.try_with (fun () -> loop t.pos)
| `Char ch ->
let rec loop pos =
if pos = limit
then None
else if Char.O.(ch = buf.{pos})
then Some pos
else loop (pos + 1)
in
Ok (loop t.pos)
;;
let read_until_gen t p ~keep_delim ~max k =
let rec loop ac total =
with_nonempty_buffer' t (function
| `Eof ->
k
(Ok
(if List.is_empty ac
then `Eof
else `Eof_without_delim (Bigsubstring.concat_string (List.rev ac))))
| `Ok ->
let concat_helper ss lst =
Bigsubstring.concat_string (List.rev_append lst [ ss ])
in
let available, need_more_bytes_to_exceed_max =
match max with
| None -> t.available, true
| Some max ->
if t.available < max - total + 1
then t.available, true
else max - total + 1, false
in
(match first_char t p ~available with
| Error _ as e -> k e
| Ok None ->
(match need_more_bytes_to_exceed_max with
| false ->
let amount_consumed = available in
let len = amount_consumed in
let ss = Bigsubstring.create t.buf ~pos:t.pos ~len in
consume t amount_consumed;
let res = concat_helper ss ac in
k (Ok (`Max_exceeded res))
| true ->
let len = t.available in
let total = total + len in
let ss = Bigsubstring.create t.buf ~pos:t.pos ~len in
t.buf <- Bigstring.create (Bigstring.length t.buf);
t.pos <- 0;
t.available <- 0;
loop (ss :: ac) total)
| Ok (Some pos) ->
let amount_consumed = pos + 1 - t.pos in
let len = if keep_delim then amount_consumed else amount_consumed - 1 in
let ss = Bigsubstring.create t.buf ~pos:t.pos ~len in
consume t amount_consumed;
let res = concat_helper ss ac in
k (Ok (`Ok res))))
in
loop [] 0
;;
let read_until t pred ~keep_delim k =
read_until_gen t pred ~keep_delim ~max:None (function
| Error _ as x -> k x
| Ok (`Max_exceeded _) -> assert false
| Ok (`Eof | `Eof_without_delim _ | `Ok _) as x -> k x)
;;
let line_delimiter_pred = `Char '\n'
let read_line_gen t k =
read_until t line_delimiter_pred ~keep_delim:false (function
| Error _ ->
assert false
| Ok ((`Eof | `Eof_without_delim _) as x) -> k x
| Ok (`Ok line) ->
k
(`Ok
(let len = String.length line in
if len >= 1 && Char.O.(line.[len - 1] = '\r')
then String.sub line ~pos:0 ~len:(len - 1)
else line)))
;;
let read_line t =
Deferred.create (fun result ->
read_line_gen t (fun z ->
Ivar.fill
result
(match z with
| `Eof_without_delim str -> `Ok str
| (`Ok _ | `Eof) as x -> x)))
;;
let really_read_line ~wait_time t =
Deferred.create (fun result ->
let fill_result = function
| [] -> Ivar.fill result None
| ac -> Ivar.fill result (Some (String.concat (List.rev ac)))
in
let rec continue ac =
match t.state with
| `Not_in_use -> assert false
| `Closed -> fill_result ac
| `In_use -> Clock.after wait_time >>> fun () -> loop ac
and loop ac =
read_line_gen t (function
| `Eof -> continue ac
| `Eof_without_delim str -> continue (str :: ac)
| `Ok line -> fill_result (line :: ac))
in
loop [])
;;
let space = Bigstring.of_string " "
type 'sexp sexp_kind =
| Plain : Sexp.t sexp_kind
| Annotated : Sexp.Annotated.t sexp_kind
let gen_read_sexp (type sexp) ?parse_pos t ~(sexp_kind : sexp sexp_kind) k =
let rec loop parse_fun =
with_nonempty_buffer' t (function
| `Eof ->
(match Or_error.try_with (fun () -> parse_fun ~pos:0 ~len:1 space) with
| Error _ as e -> k e
| Ok (Sexp.Done (sexp, parse_pos)) -> k (Ok (`Ok (sexp, parse_pos)))
| Ok (Cont (Parsing_toplevel_whitespace, _)) -> k (Ok `Eof)
| Ok
(Cont
( ( Parsing_atom
| Parsing_list
| Parsing_nested_whitespace
| Parsing_sexp_comment
| Parsing_block_comment )
, _ )) ->
raise_s [%message "Reader.read_sexp got unexpected eof" ~reader:(t : t)])
| `Ok ->
(match
Or_error.try_with (fun () -> parse_fun ~pos:t.pos ~len:t.available t.buf)
with
| Error _ as e -> k e
| Ok (Done (sexp, parse_pos)) ->
consume t (parse_pos.buf_pos - t.pos);
k (Ok (`Ok (sexp, parse_pos)))
| Ok (Cont (_, parse_fun)) ->
t.available <- 0;
loop parse_fun))
in
let parse ~pos ~len buf : (_, sexp) Sexp.parse_result =
let parse_pos =
match parse_pos with
| None -> Sexp.Parse_pos.create ~buf_pos:pos ()
| Some parse_pos -> Sexp.Parse_pos.with_buf_pos parse_pos pos
in
match sexp_kind with
| Plain -> Sexp.parse_bigstring ?parse_pos:(Some parse_pos) ?len:(Some len) buf
| Annotated ->
Sexp.Annotated.parse_bigstring ?parse_pos:(Some parse_pos) ?len:(Some len) buf
in
loop parse
;;
type 'a read = ?parse_pos:Sexp.Parse_pos.t -> 'a
let gen_read_sexps ?parse_pos t ~sexp_kind =
let pipe_r, pipe_w = Pipe.create () in
let finished =
Deferred.create (fun result ->
let rec loop parse_pos =
gen_read_sexp t ~sexp_kind ?parse_pos (function
| Error error -> Error.raise error
| Ok `Eof -> Ivar.fill result ()
| Ok (`Ok (sexp, parse_pos)) ->
if Pipe.is_closed pipe_w
then Ivar.fill result ()
else Pipe.write pipe_w sexp >>> fun () -> loop (Some parse_pos))
in
loop parse_pos)
in
upon finished (fun () -> close t >>> fun () -> Pipe.close pipe_w);
pipe_r
;;
let read_sexps ?parse_pos t = gen_read_sexps t ~sexp_kind:Plain ?parse_pos
let read_annotated_sexps ?parse_pos t = gen_read_sexps t ~sexp_kind:Annotated ?parse_pos
module Read_bin_prot = struct
type 'a result =
| Ok of 'a
| Need_bytes of int
| Error of Error.t
let[@cold] unexpected_pos bin_type t ~old_pos ~read_len ~new_pos =
Error
(Error.create_s
[%message
"Unexpected reader position after read"
(bin_type : string)
~reader:(t : t)
(old_pos : int)
(read_len : int)
(new_pos : int)])
;;
let[@cold] size_error message t ~size ~pos =
Error (Error.create_s [%message message ~reader:(t : t) (size : int) (pos : int)])
;;
let read_raw t buf ~pos_ref ~len ~bin_prot_reader =
if len < Bin_prot.Utils.size_header_length
then Need_bytes Bin_prot.Utils.size_header_length
else (
let = !pos_ref in
let message_size = Bin_prot.Utils.bin_read_size_header buf ~pos_ref in
if !pos_ref <> header_pos + Bin_prot.Utils.size_header_length
then
unexpected_pos
"header"
t
~old_pos:header_pos
~read_len:Bin_prot.Utils.size_header_length
~new_pos:!pos_ref
else if len - Bin_prot.Utils.size_header_length < message_size
then (
let bytes_needed = Bin_prot.Utils.size_header_length + message_size in
if message_size < 0
then size_error "Negative message size" t ~size:message_size ~pos:header_pos
else if bytes_needed < 0
then size_error "Bytes needed overflowed" t ~size:bytes_needed ~pos:header_pos
else Need_bytes bytes_needed)
else (
let message_pos = !pos_ref in
let message = bin_prot_reader.Bin_prot.Type_class.read buf ~pos_ref in
if !pos_ref <> message_pos + message_size
then
unexpected_pos
"message"
t
~old_pos:message_pos
~read_len:message_size
~new_pos:!pos_ref
else Ok message))
;;
end
module Peek_or_read = struct
type t =
| Peek
| Read
[@@deriving sexp_of]
let to_string = Sexplib.Conv.string_of__of__sexp_of [%sexp_of: t]
end
let peek_or_read_bin_prot
?(max_len = 100 * 1024 * 1024)
t
~(peek_or_read : Peek_or_read.t)
(bin_prot_reader : _ Bin_prot.Type_class.reader)
k
=
let error f =
ksprintf
(fun msg () ->
k (Or_error.error "Reader.read_bin_prot" (msg, t) [%sexp_of: string * t]))
f
in
let handle_eof n =
if n = 0 then k (Ok `Eof) else error "got Eof with %d bytes left over" n ()
in
if max_len < 0
then error "max read length is negative: %d" max_len ()
else (
let =
let = max_len + Bin_prot.Utils.size_header_length in
if len_with_header < max_len then Int.max_value else len_with_header
in
let rec read_loop () =
match t.state with
| `Not_in_use -> assert false
| `Closed -> error "Reader.read_bin_prot got closed reader" ()
| `In_use ->
let pos = t.pos in
let pos_ref = ref pos in
let len = min max_len_with_header t.available in
(match Read_bin_prot.read_raw t t.buf ~pos_ref ~len ~bin_prot_reader with
| Ok message ->
(match peek_or_read with
| Peek -> ()
| Read -> consume t (!pos_ref - pos));
k (Ok (`Ok message))
| Need_bytes need ->
let need_message = need - Bin_prot.Utils.size_header_length in
if need_message > max_len
then error "max read length exceeded: %d > %d" need_message max_len ()
else
get_data_until t ~available_at_least:need
>>> (function
| `Eof n -> handle_eof n
| `Ok -> read_loop ())
| Error error -> k (Error error)
| exception exn -> k (Or_error.of_exn exn))
in
read_loop ())
;;
let iter_bin_prot t bin_prot_reader ~f =
let open Eager_deferred.Let_syntax in
let handle_chunk buf ~pos:chunk_pos ~len =
let limit = chunk_pos + len in
let pos_ref = ref chunk_pos in
let rec read_loop () =
let = !pos_ref in
let len = limit - header_pos in
match Read_bin_prot.read_raw t buf ~pos_ref ~len ~bin_prot_reader with
| Ok message ->
let%bind () = f message in
read_loop ()
| Need_bytes bytes ->
if header_pos = limit
then return `Continue
else return (`Consumed (header_pos - chunk_pos, `Need bytes))
| Error error -> return (`Stop error)
in
match%map try_with ~run:`Now read_loop with
| Ok result -> result
| Error exn ->
`Stop
(Error.of_exn exn
|> Error.tag_s ~tag:[%message "Error deserializing reader" ~reader:(t : t)])
in
let%bind result = read_one_chunk_at_a_time t ~handle_chunk in
let%bind () = close t in
match result with
| `Eof -> Deferred.Or_error.ok_unit
| `Stopped error -> return (Error error)
| `Eof_with_unconsumed_data data ->
let length = String.length data in
Deferred.Or_error.error_s
[%message "Unconsumed data" (length : int) (data : string)]
;;
let read_marshal_raw t =
let eofn n =
if n = 0
then `Eof
else
raise_s [%message "Reader.read_marshal got EOF with bytes remaining" ~_:(n : int)]
in
let = Bytes.create Marshal.header_size in
match%bind really_read t header with
| `Eof n -> return (eofn n)
| `Ok ->
let len = Marshal.data_size header 0 in
let buf = Bytes.create (len + Marshal.header_size) in
Bytes.blit ~src:header ~dst:buf ~src_pos:0 ~dst_pos:0 ~len:Marshal.header_size;
let sub = Substring.create buf ~pos:Marshal.header_size ~len in
(match%map really_read_substring t sub with
| `Eof n -> eofn n
| `Ok -> `Ok buf)
;;
let read_marshal t =
match%map read_marshal_raw t with
| `Eof -> `Eof
| `Ok buf -> `Ok (Marshal.from_bytes buf 0)
;;
let read_all ?(close_when_finished = true) t read_one =
let pipe_r, pipe_w = Pipe.create () in
let finished =
Deferred.repeat_until_finished () (fun () ->
match%bind read_one t with
| `Eof -> return (`Finished ())
| `Ok one ->
if Pipe.is_closed pipe_w
then return (`Finished ())
else (
let%map () = Pipe.write pipe_w one in
`Repeat ()))
in
let maybe_close t = if close_when_finished then close t else return () in
upon finished (fun () -> maybe_close t >>> fun () -> Pipe.close pipe_w);
pipe_r
;;
let lines t = read_all t read_line
let contents t =
let buf = Buffer.create 1024 in
let sbuf = Bytes.create 1024 in
let%bind () =
Deferred.repeat_until_finished () (fun () ->
match%map read t sbuf with
| `Eof -> `Finished ()
| `Ok l ->
Buffer.add_subbytes buf sbuf ~pos:0 ~len:l;
`Repeat ())
in
let%map () = close t in
Buffer.contents buf
;;
let recv t =
Deferred.create (fun i ->
read_line t
>>> function
| `Eof -> Ivar.fill i `Eof
| `Ok length_str ->
(match
try Ok (int_of_string length_str) with
| _ -> Error ()
with
| Error () ->
raise_s
[%message
"Reader.recv got strange length" (length_str : string) ~reader:(t : t)]
| Ok length ->
let buf = Bytes.create length in
really_read t buf
>>> (function
| `Eof _ -> raise_s [%message "Reader.recv got unexpected EOF"]
| `Ok -> Ivar.fill i (`Ok buf))))
;;
let transfer t pipe_w =
Deferred.create (fun finished ->
don't_wait_for
(let%map () = Pipe.closed pipe_w in
Ivar.fill_if_empty finished ());
let rec loop () =
with_nonempty_buffer' t (function
| `Eof -> Ivar.fill_if_empty finished ()
| `Ok ->
if not (Pipe.is_closed pipe_w)
then (
let pos = t.pos in
let len = t.available in
consume t len;
Pipe.write pipe_w (Bigstring.to_string t.buf ~pos ~len) >>> loop))
in
loop ())
;;
end
open Internal
type nonrec t = t [@@deriving sexp_of]
type nonrec 'a handle_chunk_result = 'a handle_chunk_result [@@deriving sexp_of]
type nonrec 'a handle_iobuf_result = 'a handle_iobuf_result [@@deriving sexp_of]
type nonrec 'a read_one_chunk_at_a_time_result = 'a read_one_chunk_at_a_time_result
[@@deriving sexp_of]
type nonrec 'a read = 'a read
let close = close
let close_finished = close_finished
let create = create
let fd = fd
let id = id
let invariant = invariant
let bytes_read = bytes_read
let is_closed = is_closed
let last_read_time = last_read_time
let of_in_channel = of_in_channel
let open_file = open_file
let stdin = stdin
let with_close = with_close
let with_file = with_file
let use t =
let error reason =
raise_s [%message "can not read from reader" (reason : string) ~reader:(t : t)]
in
match t.state with
| `Closed -> error "closed"
| `In_use -> error "in use"
| `Not_in_use -> t.state <- `In_use
;;
let finished_read t =
match t.state with
| `Closed -> ()
| `Not_in_use -> assert false
| `In_use -> t.state <- `Not_in_use
;;
let do_read_now t f =
use t;
let x = f () in
finished_read t;
x
;;
let bytes_available t = do_read_now t (fun () -> t.available)
let peek_available t ~len = do_read_now t (fun () -> peek_available t ~len)
let read_available t ?pos ?len s = do_read_now t (fun () -> read_available t ?pos ?len s)
let do_read t f =
use t;
let%map x = f () in
finished_read t;
x
;;
let peek t ~len =
if len < 0 then raise_s [%message "[Reader.peek] got negative len" (len : int)];
do_read t (fun () -> peek t ~len)
;;
let read t ?pos ?len s = do_read t (fun () -> read t ?pos ?len s)
let read_char t = do_read t (fun () -> read_char t)
let read_substring t s = do_read t (fun () -> read_substring t s)
let read_bigsubstring t s = do_read t (fun () -> read_bigsubstring t s)
let read_one_chunk_at_a_time t ~handle_chunk =
do_read t (fun () -> read_one_chunk_at_a_time t ~handle_chunk)
;;
let read_one_iobuf_at_a_time t ~handle_chunk =
do_read t (fun () -> read_one_iobuf_at_a_time t ~handle_chunk)
;;
let really_read t ?pos ?len s = do_read t (fun () -> really_read t ?pos ?len s)
let really_read_substring t s = do_read t (fun () -> really_read_substring t s)
let really_read_bigsubstring t s = do_read t (fun () -> really_read_bigsubstring t s)
let read_line t = do_read t (fun () -> read_line t)
let really_read_line ~wait_time t = do_read t (fun () -> really_read_line ~wait_time t)
let do_read_k
(type r r')
t
(read_k : (r Or_error.t -> unit) -> unit)
(make_result : r -> r')
: r' Deferred.t
=
use t;
Deferred.create (fun result ->
read_k (fun r ->
finished_read t;
Ivar.fill result (make_result (ok_exn r))))
;;
let read_until t p ~keep_delim = do_read_k t (read_until t p ~keep_delim) Fn.id
let read_until_bounded t p ~keep_delim ~max =
do_read_k t (read_until_gen t p ~keep_delim ~max:(Some max)) Fn.id
;;
let read_sexp ?parse_pos t =
do_read_k t (gen_read_sexp t ~sexp_kind:Plain ?parse_pos) (function
| `Eof -> `Eof
| `Ok (sexp, _) -> `Ok sexp)
;;
let read_sexps ?parse_pos t =
use t;
read_sexps ?parse_pos t
;;
let read_annotated_sexps ?parse_pos t =
use t;
read_annotated_sexps ?parse_pos t
;;
let peek_or_read_bin_prot ?max_len t reader ~peek_or_read =
do_read_k t (peek_or_read_bin_prot ?max_len t reader ~peek_or_read) Fn.id
;;
let peek_bin_prot ?max_len t reader =
peek_or_read_bin_prot ?max_len t reader ~peek_or_read:Peek
;;
let read_bin_prot ?max_len t reader =
peek_or_read_bin_prot ?max_len t reader ~peek_or_read:Read
;;
let iter_bin_prot t reader ~f =
use t;
iter_bin_prot t reader ~f
;;
let iter_bin_prot_exn t reader ~f = iter_bin_prot t reader ~f >>| ok_exn
let read_bin_prot_into_pipe t reader ~f =
Pipe.create_reader ~close_on_exception:false (fun writer ->
upon (Pipe.closed writer) (fun () -> don't_wait_for (close t));
iter_bin_prot_exn t reader ~f:(fun element ->
Eager_deferred.(f element >>= Pipe.write_if_open writer)))
;;
let read_marshal_raw t = do_read t (fun () -> read_marshal_raw t)
let read_marshal t = do_read t (fun () -> read_marshal t)
let recv t = do_read t (fun () -> recv t)
let read_all ?close_when_finished t read_one = read_all ?close_when_finished t read_one
let lines t =
use t;
lines t
;;
let contents t = do_read t (fun () -> contents t)
let file_contents file = with_file file ~f:contents
let file_lines file =
let%bind t = open_file file in
Pipe.to_list (lines t)
;;
let transfer t =
use t;
transfer t
;;
let lseek t offset ~mode =
do_read t (fun () ->
t.pos <- 0;
t.available <- 0;
Unix_syscalls.lseek t.fd offset ~mode)
;;
let ltell t =
do_read t (fun () ->
let%map fd_offset = Unix_syscalls.lseek t.fd Int64.zero ~mode:`Cur in
Int64.( - ) fd_offset (Int64.of_int t.available))
;;
let get_error
(type a sexp)
~file
~(sexp_kind : sexp sexp_kind)
~(a_of_sexp : sexp -> a)
(annotated_sexp : Sexp.Annotated.t)
=
try
ignore
(a_of_sexp
(match sexp_kind with
| Plain -> (Sexp.Annotated.get_sexp annotated_sexp : sexp)
| Annotated -> (annotated_sexp : sexp))
: a);
Ok ()
with
| exn ->
let unexpected_error () =
error "Reader.load_sexp error" (file, exn) [%sexp_of: string * exn]
in
(match exn with
| Of_sexp_error (exc, bad_sexp) ->
(match Sexp.Annotated.find_sexp annotated_sexp bad_sexp with
| None -> unexpected_error ()
| Some bad_annotated_sexp ->
(match Sexp.Annotated.get_conv_exn ~file ~exc bad_annotated_sexp with
| Of_sexp_error (Sexp.Annotated.Conv_exn (pos, exn), sexp) ->
Or_error.error
"invalid sexp"
(pos, exn, "in", sexp)
[%sexp_of: string * exn * string * Sexp.t]
| _ -> unexpected_error ()))
| _ -> unexpected_error ())
;;
let gen_load_exn
(type sexp a)
?exclusive
~(sexp_kind : sexp sexp_kind)
~file
(convert : sexp list -> a)
(get_error : Sexp.Annotated.t list -> Error.t)
: a Deferred.t
=
let may_load_file_multiple_times = ref false in
let load ~sexp_kind =
match%map
Monitor.try_with
~run:`Schedule
~rest:`Log
~extract_exn:true
(fun () ->
with_file ?exclusive file ~f:(fun t ->
(may_load_file_multiple_times
:=
match Fd.kind (fd t) with
| File -> true
| Char | Fifo | Socket _ -> false);
use t;
Pipe.to_list (gen_read_sexps t ~sexp_kind)))
with
| Ok sexps -> sexps
| Error exn ->
(match exn with
| Sexp.Parse_error { err_msg; parse_state; _ } ->
let parse_pos =
match parse_state with
| `Sexp { parse_pos; _ } | `Annot { parse_pos; _ } -> parse_pos
in
Error.raise
(Error.create
"syntax error when parsing sexp"
(sprintf "%s:%d:%d" file parse_pos.text_line parse_pos.text_char, err_msg)
[%sexp_of: string * string])
| _ -> raise exn)
in
let%bind sexps = load ~sexp_kind in
try return (convert sexps) with
| Of_sexp_error (exn, _bad_subsexp) ->
if !may_load_file_multiple_times
then (
let%bind sexps = load ~sexp_kind:Annotated in
Error.raise (get_error sexps))
else
raise_s
[%message
"invalid sexp (failed to determine location information)"
(file : string)
(exn : exn)]
| exn -> raise_s [%message "Reader.load_sexp(s) error" (file : string) (exn : exn)]
;;
type ('sexp, 'a, 'b) load = ?exclusive:bool -> string -> ('sexp -> 'a) -> 'b Deferred.t
let get_load_result_exn = function
| `Result x -> x
| `Error (exn, _sexp) -> raise exn
;;
let gen_load_sexp_exn
(type a sexp)
?exclusive
~(sexp_kind : sexp sexp_kind)
~file
~(a_of_sexp : sexp -> a)
()
=
let multiple sexps =
Error.create
"Reader.load_sexp requires one sexp but got"
(List.length sexps, file)
[%sexp_of: int * string]
in
gen_load_exn
?exclusive
~file
~sexp_kind
(fun sexps ->
match sexps with
| [ sexp ] -> a_of_sexp sexp
| _ -> Error.raise (multiple sexps))
(fun annot_sexps ->
match annot_sexps with
| [ annot_sexp ] ->
(match get_error ~file ~sexp_kind ~a_of_sexp annot_sexp with
| Error e -> e
| Ok () ->
Error.create
"conversion of annotated sexp unexpectedly succeeded"
(Sexp.Annotated.get_sexp annot_sexp)
[%sexp_of: Sexp.t])
| _ -> multiple annot_sexps)
;;
let load_sexp_exn ?exclusive file a_of_sexp =
gen_load_sexp_exn ?exclusive ~sexp_kind:Plain ~file ~a_of_sexp ()
;;
let load_annotated_sexp_exn ?exclusive file a_of_sexp =
gen_load_sexp_exn ?exclusive ~sexp_kind:Annotated ~file ~a_of_sexp ()
;;
let gen_load_sexp ?exclusive ~sexp_kind ~file ~a_of_sexp () =
Deferred.Or_error.try_with
~run:`Schedule
~rest:`Log
~extract_exn:true
(gen_load_sexp_exn ?exclusive ~sexp_kind ~file ~a_of_sexp)
;;
let load_sexp ?exclusive file a_of_sexp =
gen_load_sexp ?exclusive ~sexp_kind:Plain ~file ~a_of_sexp ()
;;
let load_annotated_sexp ?exclusive file a_of_sexp =
gen_load_sexp ?exclusive ~sexp_kind:Annotated ~file ~a_of_sexp ()
;;
let gen_load_sexps_exn
(type a sexp)
?exclusive
~(sexp_kind : sexp sexp_kind)
~file
~(a_of_sexp : sexp -> a)
()
=
gen_load_exn
?exclusive
~file
~sexp_kind
(fun sexps -> List.map sexps ~f:a_of_sexp)
(fun annot_sexps ->
Error.of_list
(List.filter_map annot_sexps ~f:(fun annot_sexp ->
match get_error ~file ~sexp_kind ~a_of_sexp annot_sexp with
| Ok _ -> None
| Error error -> Some error)))
;;
let load_sexps_exn ?exclusive file a_of_sexp =
gen_load_sexps_exn ?exclusive ~sexp_kind:Plain ~file ~a_of_sexp ()
;;
let load_annotated_sexps_exn ?exclusive file a_of_sexp =
gen_load_sexps_exn ?exclusive ~sexp_kind:Annotated ~file ~a_of_sexp ()
;;
let gen_load_sexps ?exclusive ~sexp_kind ~file ~a_of_sexp () =
Deferred.Or_error.try_with
~run:`Schedule
~rest:`Log
~extract_exn:true
(gen_load_sexps_exn ?exclusive ~sexp_kind ~file ~a_of_sexp)
;;
let load_sexps ?exclusive file a_of_sexp =
gen_load_sexps ?exclusive ~sexp_kind:Plain ~file ~a_of_sexp ()
;;
let load_annotated_sexps ?exclusive file a_of_sexp =
gen_load_sexps ?exclusive ~sexp_kind:Annotated ~file ~a_of_sexp ()
;;
let pipe t =
let pipe_r, pipe_w = Pipe.create () in
upon (transfer t pipe_w) (fun () -> close t >>> fun () -> Pipe.close pipe_w);
pipe_r
;;
let drain t =
match%bind
read_one_chunk_at_a_time t ~handle_chunk:(fun _bigstring ~pos:_ ~len:_ ->
return `Continue)
with
| `Stopped _ | `Eof_with_unconsumed_data _ -> assert false
| `Eof -> close t
;;
type ('a, 'b) load_bin_prot =
?exclusive:bool
-> ?max_len:int
-> string
-> 'a Bin_prot.Type_class.reader
-> 'b Deferred.t
let load_bin_prot ?exclusive ?max_len file bin_reader =
match%map
Monitor.try_with_or_error
~rest:`Log
~name:"Reader.load_bin_prot"
(fun () ->
with_file ?exclusive file ~f:(fun t -> read_bin_prot ?max_len t bin_reader))
with
| Ok (`Ok v) -> Ok v
| Ok `Eof -> Or_error.error_string "Reader.load_bin_prot got unexpected eof"
| Error _ as result -> result
;;
let load_bin_prot_exn ?exclusive ?max_len file bin_reader =
load_bin_prot ?exclusive ?max_len file bin_reader >>| ok_exn
;;