Source file io.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
let close_in = close_in
let close_out = close_out
let close_both (ic, oc) =
match close_out oc with
| () -> close_in ic
| exception exn ->
close_in ic;
Exn.reraise exn
;;
let input_lines =
let rec loop ic acc =
match input_line ic with
| exception End_of_file -> List.rev acc
| line -> loop ic (line :: acc)
in
fun ic -> loop ic []
;;
let input_zero_from_buffer from buf =
match String.index_from_opt buf from '\x00' with
| None -> None
| Some eos -> Some (String.sub buf ~pos:from ~len:(eos - from), eos + 1)
;;
let input_zero_separated =
let rec scan_inputs_buf from buf acc =
match input_zero_from_buffer from buf with
| Some (istr, from) -> scan_inputs_buf from buf (istr :: acc)
| None ->
let total_len = String.length buf in
if total_len > from
then (
let rest = String.sub buf ~pos:from ~len:(total_len - from) in
Some rest, acc)
else None, acc
in
let ibuf_size = 65536 in
let ibuf = Bytes.create ibuf_size in
let rec input_loop ic rem acc =
let res = input ic ibuf 0 ibuf_size in
if res = 0
then (
match rem with
| Some rem -> List.rev (rem :: acc)
| None -> List.rev acc)
else (
let actual_input = Bytes.sub_string ibuf ~pos:0 ~len:res in
let actual_input =
match rem with
| None -> actual_input
| Some rem -> rem ^ actual_input
in
let rem, acc = scan_inputs_buf 0 actual_input acc in
input_loop ic rem acc)
in
fun ic -> input_loop ic None []
;;
let copy_channels =
let buf_len = 65536 in
let global_buf = Bytes.create buf_len in
let rec loop buf ic oc =
match input ic buf 0 buf_len with
| 0 -> ()
| n ->
output oc buf 0 n;
loop buf ic oc
in
let busy = ref false in
fun ic oc ->
if !busy
then loop (Bytes.create buf_len) ic oc
else (
busy := true;
match loop global_buf ic oc with
| () -> busy := false
| exception exn ->
busy := false;
Exn.reraise exn)
;;
let setup_copy ?(chmod = Fun.id) ~src ~dst () =
let ic = Stdlib.open_in_bin src in
let oc =
try
let perm = (Unix.fstat (Unix.descr_of_in_channel ic)).st_perm |> chmod in
Stdlib.open_out_gen [ Open_wronly; Open_creat; Open_trunc; Open_binary ] perm dst
with
| exn ->
close_in ic;
Exn.reraise exn
in
ic, oc
;;
module Copyfile = struct
external copyfile : string -> string -> unit = "stdune_copyfile"
external sendfile
: src:Unix.file_descr
-> dst:Unix.file_descr
-> int
-> unit
= "stdune_sendfile"
let available =
match Platform.OS.value with
| Darwin -> `Copyfile
| Linux -> `Sendfile
| _ -> `Nothing
;;
let sendfile_with_fallback =
let setup_copy ?(chmod = Fun.id) ~src ~dst () =
match Unix.openfile src [ O_RDONLY; O_CLOEXEC ] 0 with
| exception Unix.Unix_error (Unix.ENOENT, _, _) -> Error `Src_missing
| fd_src ->
(match Unix.fstat fd_src with
| exception exn ->
Unix.close fd_src;
Error (`Exn (Exn_with_backtrace.capture exn))
| src_stat ->
(match src_stat.st_kind with
| S_DIR -> Error `Src_is_a_dir
| _ ->
let open Result.O in
let+ fd_dst, src_size =
match
let dst_perm = chmod src_stat.st_perm in
Unix.openfile dst [ O_WRONLY; O_CREAT; O_TRUNC; O_CLOEXEC ] dst_perm
with
| fd_dst -> Ok (fd_dst, src_stat.st_size)
| exception exn ->
Unix.close fd_src;
(match exn with
| Unix.Unix_error (Unix.EISDIR, _, _) -> Error `Dst_is_a_dir
| _ -> Error (`Exn (Exn_with_backtrace.capture exn)))
in
fd_src, fd_dst, src_size))
in
fun ?chmod ~src ~dst () ->
match setup_copy ?chmod ~src ~dst () with
| Error (`Exn exn) -> Exn_with_backtrace.reraise exn
| Error `Src_is_a_dir -> raise (Sys_error "Is a directory")
| Error `Dst_is_a_dir ->
let message = Printf.sprintf "%s: Is a directory" dst in
raise (Sys_error message)
| Error `Src_missing ->
let message = Printf.sprintf "%s: No such file or directory" src in
raise (Sys_error message)
| Ok (src, dst, src_size) ->
let close_fds () =
Unix.close src;
Unix.close dst
in
(match sendfile ~src ~dst src_size with
| exception Unix.Unix_error (EINVAL, "sendfile", _) ->
Exn.protectx
(Unix.in_channel_of_descr src, Unix.out_channel_of_descr dst)
~f:(fun (ic, oc) -> copy_channels ic oc)
~finally:close_both
| () -> close_fds ()
| exception exn ->
close_fds ();
Exn.reraise exn)
;;
let copyfile ?chmod ~src ~dst () =
let src_stats =
match Unix.stat src with
| exception Unix.Unix_error (Unix.ENOENT, _, _) ->
let message = Printf.sprintf "%s: No such file or directory" src in
raise (Sys_error message)
| { st_kind = S_DIR; _ } -> raise (Sys_error "Is a directory")
| stats -> stats
in
(try copyfile src dst with
| Unix.Unix_error (Unix.EPERM, "unlink", _) ->
let message = Printf.sprintf "%s: Is a directory" dst in
raise (Sys_error message)
| Unix.Unix_error (Unix.ENOENT, "realpath", _) ->
let message = Printf.sprintf "%s: No such file or directory" src in
raise (Sys_error message));
match chmod with
| None -> ()
| Some chmod -> src_stats.st_perm |> chmod |> Unix.chmod dst
;;
let copy_file_portable ?chmod ~src ~dst () =
Exn.protectx (setup_copy ?chmod ~src ~dst ()) ~finally:close_both ~f:(fun (ic, oc) ->
copy_channels ic oc)
;;
let copy_file =
match available with
| `Sendfile -> sendfile_with_fallback
| `Copyfile -> copyfile
| `Nothing -> copy_file_portable
;;
end
module Make (Path : sig
type t
val to_string : t -> string
end) =
struct
type path = Path.t
let open_in ?(binary = true) p =
let fn = Path.to_string p in
if binary then Stdlib.open_in_bin fn else Stdlib.open_in fn
;;
let open_out ?(binary = true) ?(perm = 0o666) p =
let fn = Path.to_string p in
let flags : Stdlib.open_flag list =
[ Open_wronly; Open_creat; Open_trunc; (if binary then Open_binary else Open_text) ]
in
Stdlib.open_out_gen flags perm fn
;;
let with_file_in ?binary fn ~f = Exn.protectx (open_in ?binary fn) ~finally:close_in ~f
let with_file_in_fd fn ~f =
Exn.protectx (Unix.openfile fn [ O_RDONLY; O_CLOEXEC ] 0) ~f ~finally:Unix.close
;;
let with_file_out ?binary ?perm p ~f =
Exn.protectx (open_out ?binary ?perm p) ~finally:close_out ~f
;;
let with_lexbuf_from_file fn ~f =
with_file_in fn ~f:(fun ic ->
let lb = Lexing.from_channel ic in
lb.lex_curr_p
<- { pos_fname = Path.to_string fn; pos_lnum = 1; pos_bol = 0; pos_cnum = 0 };
f lb)
;;
let rec eagerly_input_acc ic s ~pos ~len acc =
if len <= 0
then acc
else (
let r = input ic s pos len in
if r = 0 then acc else eagerly_input_acc ic s ~pos:(pos + r) ~len:(len - r) (acc + r))
;;
let eagerly_input_string ic len =
let buf = Bytes.create len in
let r = eagerly_input_acc ic buf ~pos:0 ~len 0 in
if r = len then Bytes.unsafe_to_string buf else Bytes.sub_string buf ~pos:0 ~len:r
;;
let read_all_fd =
let rec read fd buf pos left =
if left = 0
then `Ok
else (
match Unix.read fd buf pos left with
| 0 -> `Eof
| n -> read fd buf (pos + n) (left - n))
in
fun fd ->
match Unix.fstat fd with
| exception Unix.Unix_error (e, x, y) -> Error (`Unix (e, x, y))
| { Unix.st_size; _ } ->
if st_size = 0
then Ok ""
else if st_size > Sys.max_string_length
then Error `Too_big
else (
let b = Bytes.create st_size in
match read fd b 0 st_size with
| exception Unix.Unix_error (e, x, y) -> Error (`Unix (e, x, y))
| `Eof -> Error `Retry
| `Ok -> Ok (Bytes.unsafe_to_string b))
;;
let read_all_unless_large =
let chunk_size = 65536 in
let read_all_generic t buffer =
let rec loop () =
Buffer.add_channel buffer t chunk_size;
loop ()
in
try loop () with
| End_of_file -> Ok (Buffer.contents buffer)
in
fun t ->
match in_channel_length t with
| exception Sys_error _ -> read_all_generic t (Buffer.create chunk_size)
| n when n > Sys.max_string_length -> Error ()
| n ->
let s = eagerly_input_string t n in
(match input_char t with
| exception End_of_file -> Ok s
| c ->
let buffer = Buffer.create (String.length s + 1 + chunk_size) in
Buffer.add_string buffer s;
Buffer.add_char buffer c;
read_all_generic t buffer)
;;
let path_to_dyn path = String.to_dyn (Path.to_string path)
let read_file_chan ?binary fn =
match with_file_in fn ~f:read_all_unless_large ?binary with
| Ok x -> x
| Error () ->
Code_error.raise
"read_file: file is larger than Sys.max_string_length"
[ "fn", path_to_dyn fn ]
;;
let read_file ?(binary = true) fn =
if binary
then
with_file_in_fd (Path.to_string fn) ~f:(fun fd ->
match read_all_fd fd with
| Ok s -> s
| Error `Retry -> read_file_chan ~binary fn
| Error `Too_big ->
Code_error.raise
"read_file: file is larger than Sys.max_string_length"
[ "fn", path_to_dyn fn ]
| Error (`Unix e) -> Dune_filesystem_stubs.Unix_error.Detailed.raise e)
else read_file_chan ~binary fn
;;
let lines_of_file fn = with_file_in fn ~f:input_lines ~binary:false
let zero_strings_of_file fn = with_file_in fn ~f:input_zero_separated ~binary:true
let write_file ?binary ?perm fn data =
with_file_out ?binary ?perm fn ~f:(fun oc -> output_string oc data)
;;
let write_lines ?binary ?perm fn lines =
with_file_out ?binary ?perm fn ~f:(fun oc ->
List.iter
~f:(fun line ->
output_string oc line;
output_string oc "\n")
lines)
;;
let read_file_and_normalize_eols fn =
if not Stdlib.Sys.win32
then read_file fn
else (
let src = read_file fn in
let len = String.length src in
let dst = Bytes.create len in
let rec find_next_crnl i =
match String.index_from src i '\r' with
| None -> None
| Some j ->
if j + 1 < len && src.[j + 1] = '\n' then Some j else find_next_crnl (j + 1)
in
let rec loop src_pos dst_pos =
match find_next_crnl src_pos with
| None ->
let len =
if len > src_pos && src.[len - 1] = '\r'
then len - 1 - src_pos
else len - src_pos
in
Bytes.blit_string ~src ~src_pos ~dst ~dst_pos ~len;
Bytes.sub_string dst ~pos:0 ~len:(dst_pos + len)
| Some i ->
let len = i - src_pos in
Bytes.blit_string ~src ~src_pos ~dst ~dst_pos ~len;
let dst_pos = dst_pos + len in
Bytes.set dst dst_pos '\n';
loop (i + 2) (dst_pos + 1)
in
loop 0 0)
;;
let compare_text_files fn1 fn2 =
let s1 = read_file_and_normalize_eols fn1 in
let s2 = read_file_and_normalize_eols fn2 in
String.compare s1 s2
;;
let compare_files fn1 fn2 =
let s1 = read_file fn1 in
let s2 = read_file fn2 in
String.compare s1 s2
;;
let setup_copy ?chmod ~src ~dst () =
let src = Path.to_string src in
let dst = Path.to_string dst in
setup_copy ?chmod ~src ~dst ()
;;
let copy_file ?chmod ~src ~dst () =
let src = Path.to_string src in
let dst = Path.to_string dst in
Copyfile.copy_file ?chmod ~src ~dst ()
;;
let file_line path n =
with_file_in ~binary:false path ~f:(fun ic ->
for _ = 1 to n - 1 do
ignore (input_line ic)
done;
input_line ic)
;;
let file_lines path ~start ~stop =
with_file_in ~binary:true path ~f:(fun ic ->
let rec aux acc lnum =
if lnum > stop
then List.rev acc
else if lnum < start
then (
ignore (input_line ic);
aux acc (lnum + 1))
else (
let line = input_line ic in
aux ((string_of_int lnum, line) :: acc) (lnum + 1))
in
aux [] 1)
;;
let cat ?binary ?dst fn =
let dst =
match dst with
| Some dst -> dst
| None -> stdout
in
with_file_in ?binary fn ~f:(fun ic -> copy_channels ic dst)
;;
end
include Make (Path)
module String_path = struct
include Make (struct
type t = string
let to_string x = x
end)
let copy_file = Copyfile.copyfile
end
let portable_symlink ~src ~dst =
if Stdlib.Sys.win32
then copy_file ~src ~dst ()
else (
let src =
match Path.parent dst with
| None -> Path.to_string src
| Some from -> Path.reach ~from src
in
let dst = Path.to_string dst in
match Unix.readlink dst with
| target ->
if target <> src
then (
Unix.unlink dst;
Unix.symlink src dst)
| exception Unix.Unix_error _ -> Unix.symlink src dst)
;;
let portable_hardlink ~src ~dst =
let user_error msg =
User_error.raise
[ Pp.textf
"Sandbox creation error: cannot resolve symbolic link %S."
(Path.to_string src)
; Pp.textf "Reason: %s" msg
]
in
match Stdlib.Sys.win32 with
| true -> copy_file ~src ~dst ()
| false ->
let src =
match Fpath.follow_symlink (Path.to_string src) with
| Ok path -> Path.of_string path
| Error Not_a_symlink -> src
| Error Max_depth_exceeded ->
user_error "Too many indirections; is this a cyclic symbolic link?"
| Error (Unix_error error) ->
user_error (Dune_filesystem_stubs.Unix_error.Detailed.to_string_hum error)
in
(try Path.link src dst with
| Unix.Unix_error (Unix.EEXIST, _, _) ->
Path.unlink_exn dst;
Path.link src dst
| Unix.Unix_error (Unix.EMLINK, _, _) ->
copy_file ~src ~dst ())
;;