Debian Codegraph @

vendor/opam/src/core/opamSystem.ml

raw
1 (**************************************************************************)
2 (* *)
3 (* Copyright 2012-2020 OCamlPro *)
4 (* Copyright 2012 INRIA *)
5 (* *)
6 (* All rights reserved. This file is distributed under the terms of the *)
7 (* GNU Lesser General Public License version 2.1, with the special *)
8 (* exception on linking described in the file LICENSE. *)
9 (* *)
10 (**************************************************************************)
11
12 type install_warning =
13 [ `Add_exe | `Install_dll | `Install_script | `Install_unknown
14 | `Cygwin | `Msys2 | `Tainted of [`Msys2 | `Cygwin] | `Cygwin_libraries ]
15 type install_warning_fn = string -> install_warning -> unit
16
17 exception Internal_error of string
18 exception Command_not_found of string
19 exception File_not_found of string
20 exception Permission_denied of string
21
22 let log ?level fmt = OpamConsole.log "SYSTEM" ?level fmt
23 let slog = OpamConsole.slog
24
25 let internal_error fmt =
26 Printf.ksprintf (fun str ->
27 log "error: %s" str;
28 raise (Internal_error str)
29 ) fmt
30
31 let command_not_found cmd =
32 raise (Command_not_found cmd)
33
34 let permission_denied cmd =
35 raise (Permission_denied cmd)
36
37 module Sys2 = struct
38 (* same as [Sys.is_directory] except for symlinks, which returns always [false]. *)
39 let is_directory file =
40 try Unix.( (lstat file).st_kind = S_DIR )
41 with Unix.Unix_error _ as e -> raise (Sys_error (Printexc.to_string e))
42 end
43
44 let file_or_symlink_exists f =
45 try ignore (Unix.lstat f); true
46 with Unix.Unix_error (Unix.ENOENT, _, _) -> false
47
48 let (/) = Filename.concat
49
50 let temp_basename prefix =
51 Printf.sprintf "%s-%d-%06x" prefix (OpamStubs.getpid ()) (Random.int 0xFFFFFF)
52
53 let rec mk_temp_dir ?(prefix="opam") () =
54 let s = Filename.get_temp_dir_name () / temp_basename prefix in
55 if Sys.file_exists s then
56 mk_temp_dir ()
57 else
58 s
59
60 let safe_mkdir dir =
61 try
62 log "mkdir %s" dir;
63 Unix.mkdir dir 0o755
64 with
65 Unix.Unix_error(Unix.EEXIST,_,_) -> ()
66
67 let mkdir dir =
68 let rec aux dir =
69 if not (Sys.file_exists dir) then begin
70 aux (Filename.dirname dir);
71 safe_mkdir dir;
72 end in
73 aux dir
74
75 let get_files dirname =
76 let dir = Unix.opendir dirname in
77 let rec aux files =
78 match Unix.readdir dir with
79 | "." | ".." -> aux files
80 | file -> aux (file :: files)
81 | exception End_of_file -> files
82 in
83 let files = aux [] in
84 Unix.closedir dir;
85 files
86
87 let log_for_file_management () =
88 OpamCoreConfig.(!r.debug_level) >= 4
89
90 (* From stdune/src/fpath.ml *)
91 let win32_unlink fn =
92 try Unix.unlink fn
93 with Unix.Unix_error (Unix.EACCES, _, _) as e -> (
94 try
95 (* Try removing the read-only attribute *)
96 Unix.chmod fn 0o666;
97 Unix.unlink fn
98 with _ -> raise e)
99
100 let remove_file_t ?(with_log=true) file =
101 if
102 try ignore (Unix.lstat file); true with Unix.Unix_error _ -> false
103 then (
104 try
105 if with_log || log_for_file_management () then
106 log "rm %s" file;
107 if Sys.win32 then
108 win32_unlink file
109 else
110 Unix.unlink file
111 with Unix.Unix_error _ as e ->
112 internal_error "Cannot remove %s (%s)." file (Printexc.to_string e)
113 )
114
115 let rec remove_dir_t dir =
116 let files = get_files dir in
117 List.iter (fun file ->
118 let file = Filename.concat dir file in
119 match Unix.lstat file with
120 | {Unix.st_kind = Unix.S_DIR; _} ->
121 remove_dir_t file
122 | {Unix.st_kind = Unix.(S_REG | S_LNK | S_CHR | S_BLK | S_FIFO | S_SOCK); _} ->
123 remove_file_t ~with_log:false file
124 ) files;
125 Unix.rmdir dir
126
127 let remove_file = remove_file_t ~with_log:true
128
129 let remove_dir dir =
130 log "rmdir %s" dir;
131 if Sys.file_exists dir then begin
132 if Sys.is_directory dir then
133 remove_dir_t dir
134 else
135 remove_file dir
136 end
137
138 let temp_files = Hashtbl.create 1024
139 let logs_cleaner =
140 let to_clean = ref OpamStd.String.Set.empty in
141 OpamStd.Sys.at_exit
142 (fun () ->
143 OpamStd.String.Set.iter (fun f ->
144 try
145 Unix.unlink f;
146 (* Only log the item if unlink succeeded *)
147 log "logs_cleaner: rm: %s" f
148 with Unix.Unix_error _ -> ())
149 !to_clean;
150 if OpamCoreConfig.(!r.log_dir = default.log_dir) then
151 try Unix.rmdir OpamCoreConfig.(default.log_dir)
152 with Unix.Unix_error _ -> ());
153 fun tmp_dir ->
154 if OpamCoreConfig.(!r.keep_log_dir) then
155 to_clean := OpamStd.String.Set.remove tmp_dir !to_clean
156 else
157 to_clean := OpamStd.String.Set.add tmp_dir !to_clean
158
159 let rec temp_file ?(auto_clean=true) ?dir prefix =
160 let temp_dir = match dir with
161 | None -> OpamCoreConfig.(!r.log_dir)
162 | Some d -> d in
163 mkdir temp_dir;
164 let file = temp_dir / temp_basename prefix in
165 if Hashtbl.mem temp_files file then
166 temp_file ~auto_clean ?dir prefix
167 else (
168 Hashtbl.add temp_files file true;
169 if auto_clean then logs_cleaner file;
170 file
171 )
172
173 let remove_file file =
174 if
175 try ignore (Unix.lstat file); true with Unix.Unix_error _ -> false
176 then (
177 log "rm %s" file;
178 try
179 try Unix.unlink file
180 with Unix.Unix_error(EACCES, _, _) when Sys.win32 ->
181 (* Attempt to remove the read-only bit on Windows *)
182 Unix.chmod file 0o666;
183 Unix.unlink file
184 with Unix.Unix_error _ as e ->
185 internal_error "Cannot remove %s (%s)." file (Printexc.to_string e)
186 )
187
188 let string_of_channel ic =
189 let n = 32768 in
190 let s = Bytes.create n in
191 let b = Buffer.create 1024 in
192 let rec iter ic b s =
193 let nread =
194 try input ic s 0 n
195 with End_of_file -> 0 in
196 if nread > 0 then (
197 Buffer.add_subbytes b s 0 nread;
198 iter ic b s
199 ) in
200 iter ic b s;
201 Buffer.contents b
202
203 let read file =
204 let ic =
205 try open_in_bin file
206 with Sys_error _ -> raise (File_not_found file) in
207 Unix.lockf (Unix.descr_of_in_channel ic) Unix.F_RLOCK 0;
208 let s = string_of_channel ic in
209 close_in ic;
210 s
211
212 let write file contents =
213 mkdir (Filename.dirname file);
214 let oc =
215 try open_out_bin file
216 with Sys_error _ -> raise (File_not_found file)
217 in
218 Unix.lockf (Unix.descr_of_out_channel oc) Unix.F_LOCK 0;
219 output_string oc contents;
220 close_out oc
221
222 let setup_copy ?(chmod = fun x -> x) ~src ~dst () =
223 let ic = open_in_bin src in
224 try
225 let perm =
226 (Unix.fstat (Unix.descr_of_in_channel ic)).st_perm |> chmod
227 in
228 let () =
229 try if Unix.((lstat dst).st_kind <> S_REG) then
230 remove_file dst
231 with Unix.Unix_error(ENOENT, _, _) -> ()
232 in
233 let fd =
234 let flags = Unix.[ O_WRONLY; O_CREAT; O_TRUNC ] in
235 try Unix.openfile dst flags perm
236 with Unix.Unix_error(EACCES, _, _) when Sys.win32 ->
237 (* Attempt to remove the read-only bit on Windows *)
238 begin
239 try Unix.chmod dst 0o666
240 with Unix.Unix_error(_, _, _) -> ()
241 end;
242 Unix.openfile dst flags perm
243 in
244 try
245 if Unix.((fstat fd).st_perm) <> perm then
246 Unix.fchmod fd perm;
247 (ic, Unix.out_channel_of_descr fd)
248 with exn ->
249 OpamStd.Exn.finalise exn (fun () -> Unix.close fd)
250 with exn ->
251 OpamStd.Exn.finalise exn (fun () -> close_in ic)
252
253 let copy_channels =
254 let buf_len = 4096 in
255 let buf = Bytes.create buf_len in
256 let rec loop ic oc =
257 match input ic buf 0 buf_len with
258 | 0 -> ()
259 | n ->
260 output oc buf 0 n;
261 loop ic oc
262 in
263 loop
264
265 let copy_file_aux ?chmod ~src ~dst () =
266 let close_channels ic oc =
267 OpamStd.Exn.finally (fun () -> close_in ic) (fun () -> close_out oc) in
268 try
269 let ic, oc = setup_copy ?chmod ~src ~dst () in
270 OpamStd.Exn.finally (fun () -> close_channels ic oc)
271 (fun () -> copy_channels ic oc);
272 with Unix.Unix_error _ as e ->
273 (* Remove the partial destination file, if any. *)
274 (try Unix.unlink dst with Unix.Unix_error _ -> ());
275 internal_error "Cannot copy %s to %s (%s)." src dst (Printexc.to_string e)
276
277 let chdir dir =
278 try Unix.chdir dir
279 with Unix.Unix_error _ -> raise (File_not_found dir)
280
281 let in_dir dir fn =
282 let reset_cwd =
283 let cwd =
284 try Some (Sys.getcwd ())
285 with Sys_error _ -> None in
286 fun () ->
287 match cwd with
288 | None -> ()
289 | Some cwd -> try chdir cwd with File_not_found _ -> () in
290 chdir dir;
291 try
292 let r = fn () in
293 reset_cwd ();
294 r
295 with e ->
296 OpamStd.Exn.finalise e reset_cwd
297
298 let list kind dir =
299 try
300 in_dir dir (fun () ->
301 let d = Sys.readdir (Sys.getcwd ()) in
302 let d = Array.to_list d in
303 let l = List.filter kind d in
304 List.map (Filename.concat dir) (List.sort compare l)
305 )
306 with File_not_found _ -> []
307
308 let ls dir = list (fun _ -> true) dir
309
310 let files_with_links =
311 list (fun f -> try not (Sys.is_directory f) with Sys_error _ -> false)
312
313 let files_all_not_dir =
314 list (fun f -> try not (Sys2.is_directory f) with Sys_error _ -> false)
315
316 let directories_strict =
317 list (fun f -> try Sys2.is_directory f with Sys_error _ -> false)
318
319 let directories_with_links =
320 list (fun f -> try Sys.is_directory f with Sys_error _ -> false)
321
322 let rec_files dir =
323 let rec aux accu dir =
324 let d = directories_with_links dir in
325 let f = files_with_links dir in
326 List.fold_left aux (f @ accu) d in
327 aux [] dir
328
329 let files dir =
330 files_with_links dir
331
332 let rec_dirs dir =
333 let rec aux accu dir =
334 let d = directories_with_links dir in
335 List.fold_left aux (d @ accu) d in
336 aux [] dir
337
338 let dirs dir =
339 directories_with_links dir
340
341 let dir_is_empty dir =
342 try in_dir dir (fun () -> Sys.readdir (Sys.getcwd ()) = [||])
343 with File_not_found _ -> false
344
345 let with_tmp_dir fn =
346 let dir = mk_temp_dir () in
347 try
348 mkdir dir;
349 let e = fn dir in
350 remove_dir dir;
351 e
352 with e ->
353 OpamStd.Exn.finalise e @@ fun () ->
354 remove_dir dir
355
356 let in_tmp_dir fn =
357 with_tmp_dir @@ fun dir ->
358 in_dir dir fn
359
360 let remove file =
361 if (try Sys2.is_directory file with Sys_error _ -> false) then
362 remove_dir file
363 else
364 remove_file file
365
366 let real_path p =
367 (* if Filename.is_relative p then *)
368 match (try Some (Sys.is_directory p) with Sys_error _ -> None) with
369 | None ->
370 let rec resolve dir =
371 if Sys.file_exists dir then OpamCompat.Unix.realpath dir else
372 let parent = Filename.dirname dir in
373 if dir = parent then dir
374 else Filename.concat (resolve parent) (Filename.basename dir)
375 in
376 let p =
377 if Filename.is_relative p then Filename.concat (Sys.getcwd ()) p
378 else p
379 in
380 resolve p
381 | Some true -> OpamCompat.Unix.realpath p
382 | Some false ->
383 let dir = OpamCompat.Unix.realpath (Filename.dirname p) in
384 match Filename.basename p with
385 | "." -> dir
386 | base -> dir / base
387 (* else p *)
388
389 type command = string list
390
391 let env_var env var =
392 let len = Array.length env in
393 let f = if Sys.win32 then String.uppercase_ascii else fun x -> x in
394 let prefix = f var^"=" in
395 let pfxlen = String.length prefix in
396 let rec aux i =
397 if i >= len then "" else
398 let s = env.(i) in
399 if OpamStd.String.starts_with ~prefix (f s) then
400 String.sub s pfxlen (String.length s - pfxlen)
401 else aux (i+1)
402 in
403 aux 0
404
405 let forward_to_back =
406 if Sys.win32 then
407 String.map (function '/' -> '\\' | c -> c)
408 else
409 fun x -> x
410
411 let back_to_forward =
412 if Sys.win32 then
413 String.map (function '\\' -> '/' | c -> c)
414 else
415 fun x -> x
416
417 let runs = ref []
418 let print_stats () =
419 match !runs with
420 | [] -> ()
421 | l ->
422 OpamConsole.msg "%d external processes called:\n%s"
423 (List.length l) (OpamStd.Format.itemize ~bullet:" " (String.concat " ") l)
424
425 let log_file ?dir name = temp_file ?dir (OpamStd.Option.default "log" name)
426
427 let verbose_for_base_commands () =
428 OpamCoreConfig.(!r.verbose_level) >= 3
429
430 let copy_file_t ?(with_log=true) src dst =
431 if (try Sys.is_directory src
432 with Sys_error _ -> raise (File_not_found src))
433 then internal_error "Cannot copy %s: it is a directory." src;
434 if (try Sys.is_directory dst with Sys_error _ -> false)
435 then internal_error "Cannot copy to %s: it is a directory." dst;
436 if file_or_symlink_exists dst
437 then remove_file dst;
438 mkdir (Filename.dirname dst);
439 if with_log || log_for_file_management () then
440 log "copy %s -> %s" src dst;
441 copy_file_aux ~src ~dst ()
442
443 let rec link_t ?(with_log=true) src dst =
444 mkdir (Filename.dirname dst);
445 if file_or_symlink_exists dst then
446 remove_file dst;
447 try
448 if with_log || log_for_file_management () then
449 log "ln -s %s %s" src dst;
450 Unix.symlink src dst
451 with Unix.Unix_error (Unix.EXDEV, _, _) ->
452 (* Fall back to copy if symlinks are not supported *)
453 let src =
454 if Filename.is_relative src then Filename.dirname dst / src
455 else src
456 in
457 if Sys.is_directory src then
458 copy_dir_t src dst
459 else
460 copy_file_t src dst
461
462 and copy_dir_t ?(with_log=true) src dst =
463 if with_log || log_for_file_management () then
464 log "copydir %s -> %s" src dst;
465 let files = get_files src in
466 mkdir dst;
467 let with_log = false in
468 List.iter (fun file ->
469 let src = Filename.concat src file in
470 let dst = Filename.concat dst file in
471 match Unix.lstat src with
472 | {Unix.st_kind = Unix.S_REG; _} ->
473 copy_file_t ~with_log src dst
474 | {Unix.st_kind = Unix.S_DIR; _} ->
475 copy_dir_t ~with_log src dst
476 | {Unix.st_kind = Unix.S_LNK; _} ->
477 let src = Unix.readlink src in
478 link_t ~with_log src dst
479 | {Unix.st_kind = Unix.S_CHR; _} ->
480 failwith (Printf.sprintf "Copying character devices (%s) is unsupported" src)
481 | {Unix.st_kind = Unix.S_BLK; _} ->
482 failwith (Printf.sprintf "Copying block devices (%s) is unsupported" src)
483 | {Unix.st_kind = Unix.S_FIFO; _} ->
484 failwith (Printf.sprintf "Copying named pipes (%s) is unsupported" src)
485 | {Unix.st_kind = Unix.S_SOCK; _} ->
486 failwith (Printf.sprintf "Copying sockets (%s) is unsupported" src)
487 ) files
488
489 let copy_dir = copy_dir_t ~with_log:true
490 let copy_file = copy_file_t ~with_log:true
491
492 let mv src dst =
493 if file_or_symlink_exists dst then remove_file dst;
494 mkdir (Filename.dirname dst);
495 log "mv %s -> %s" src dst;
496 try
497 Unix.rename src dst
498 with
499 | Unix.Unix_error(Unix.EXDEV, _, _) ->
500 let with_log = false in
501 if Sys.is_directory src
502 then (copy_dir_t ~with_log src dst; remove_dir_t src)
503 else (copy_file_t ~with_log src dst; remove_file_t ~with_log src)
504
505 let is_exec file =
506 let stat = Unix.stat file in
507 stat.Unix.st_kind = Unix.S_REG &&
508 stat.Unix.st_perm land 0o111 <> 0
509
510 let file_is_empty f = Unix.((stat f).st_size = 0)
511
512 let classify_executable file =
513 let c = open_in file in
514 (* On a 32-bit system, this could fail for a PE image with a 2GB+ DOS header =-o *)
515 let input_int_little c =
516 let b1 = input_byte c in
517 let b2 = input_byte c in
518 let b3 = input_byte c in
519 let b4 = input_byte c in
520 b1 lor (b2 lsl 8) lor (b3 lsl 16) lor (b4 lsl 24) in
521 let input_short_little c =
522 let b1 = input_byte c in
523 let b2 = input_byte c in
524 b1 lor (b2 lsl 8) in
525 set_binary_mode_in c true;
526 try
527 match really_input_string c 2 with
528 "#!" ->
529 close_in c;
530 `Script
531 | "MZ" ->
532 let is_pe =
533 try
534 (* Offset to PE header at 0x3c (but we've already read two bytes) *)
535 ignore (really_input_string c 0x3a);
536 ignore (really_input_string c (input_int_little c - 0x40));
537 let magic = really_input_string c 4 in
538 magic = "PE\000\000"
539 with End_of_file ->
540 close_in c;
541 false in
542 if is_pe then
543 try
544 let arch =
545 (* NB It's not necessary to determine PE/PE+ headers for x64/x86 determination *)
546 match input_short_little c with
547 0x8664 ->
548 `x86_64
549 | 0x14c ->
550 `x86
551 | _ ->
552 raise End_of_file
553 in
554 ignore (really_input_string c 14);
555 let size_of_opt_header = input_short_little c in
556 let characteristics = input_short_little c in
557 (* Executable images must have a PE "optional" header and be marked executable *)
558 (* Could also validate IMAGE_FILE_32BIT_MACHINE (0x100) for x86 and IMAGE_FILE_LARGE_ADDRESS_AWARE (0x20) for x64 *)
559 if size_of_opt_header <= 0 || characteristics land 0x2 = 0 then
560 raise End_of_file;
561 close_in c;
562 if characteristics land 0x2000 <> 0 then
563 `Dll arch
564 else
565 `Exe arch
566 with End_of_file ->
567 close_in c;
568 `Unknown
569 else
570 `Exe `i386
571 | _ ->
572 close_in c;
573 `Unknown
574 with End_of_file ->
575 close_in c;
576 `Unknown
577
578 let default_install_warning dst = function
579 | `Add_exe ->
580 OpamConsole.warning "Automatically adding .exe to %s" dst
581 | `Install_dll ->
582 (* TODO Installation of .dll to bin is unfortunate, but not sure if it
583 should be a warning *)
584 ()
585 | `Install_script ->
586 (* TODO Generate a .cmd wrapper (and warn about it - they're not perfect) *)
587 OpamConsole.warning "%s is a script; the command won't be available" dst;
588 | `Install_unknown ->
589 (* TODO Installation of a non-executable file is unexpected, but not sure
590 if it should be a warning/error *)
591 ()
592 | `Cygwin ->
593 OpamConsole.warning "%s is a Cygwin-linked executable" dst
594 | `Msys2 ->
595 OpamConsole.warning "%s is a MSYS2-linked executable" dst
596 | `Tainted `Cygwin ->
597 OpamConsole.warning
598 "%s is an executable which links to a Cygwin-linked library" dst
599 | `Tainted `Msys2 ->
600 OpamConsole.warning
601 "%s is an executable which links to a MSYS2-linked library" dst
602 | `Cygwin_libraries ->
603 OpamConsole.warning
604 "%s links with a Cygwin-compiled DLL (almost certainly a packaging \
605 or environment error)" dst
606
607 let install ?(warning=default_install_warning) ?exec src dst =
608 if Sys.is_directory src then
609 internal_error "Cannot install %s: it is a directory." src;
610 if (try Sys.is_directory dst with Sys_error _ -> false) then
611 internal_error "Cannot install to %s: it is a directory." dst;
612 mkdir (Filename.dirname dst);
613 let exec = match exec with
614 | Some e -> e
615 | None -> is_exec src in
616 let perm = if exec then 0o755 else 0o644 in
617 log "install %s -> %s (%o)" src dst perm;
618 if Sys.win32 then
619 if exec then begin
620 let (dst, cygcheck) =
621 match classify_executable src with
622 `Exe _ ->
623 if not (Filename.check_suffix dst ".exe") && not (Filename.check_suffix dst ".dll") then begin
624 warning dst `Add_exe;
625 (dst ^ ".exe", true)
626 end else
627 (dst, true)
628 | `Dll _ ->
629 warning dst `Install_dll;
630 (dst, true)
631 | `Script ->
632 warning dst `Install_script;
633 (dst, false)
634 | `Unknown ->
635 warning dst `Install_unknown;
636 (dst, false)
637 in
638 copy_file_aux ~src ~dst ();
639 if cygcheck then
640 match OpamStd.Sys.get_windows_executable_variant
641 ~cygbin:OpamCoreConfig.(!r.cygbin) dst with
642 | `Native -> ()
643 | (`Cygwin | `Msys2 | `Tainted _) as code -> warning dst code
644 end else
645 copy_file_aux ~src ~dst ()
646 else
647 copy_file_aux ~chmod:(fun _ -> perm) ~src ~dst ()
648
649 module Tar = struct
650
651 type extract =
652 | Bzip2
653 | Gzip
654 | Lzma
655 | Xz
656
657 let extract_command = function
658 | Bzip2 -> "bzip2"
659 | Gzip -> "gzip"
660 | Lzma -> "lzma"
661 | Xz -> "xz"
662
663 let extract_option = function
664 | Bzip2 -> 'j'
665 | Gzip -> 'z'
666 | Lzma -> 'Y'
667 | Xz -> 'J'
668
669 let extensions =
670 [ [ "tar.gz" ; "tgz" ], Gzip
671 ; [ "tar.bz2" ; "tbz" ], Bzip2
672 ; [ "tar.xz" ; "txz" ], Xz
673 ; [ "tar.lzma" ; "tlz" ], Lzma
674 ]
675
676 let guess_type f =
677 try
678 let ic = open_in f in
679 let c1 = input_char ic in
680 let c2 = input_char ic in
681 close_in ic;
682 match c1, c2 with
683 | '\031', '\139' -> Some Gzip
684 | 'B' , 'Z' -> Some Bzip2
685 | '\xfd', '\x37' -> Some Xz
686 | '\x5d', '\x00' -> Some Lzma
687 | _ -> None
688 with Sys_error _ -> None
689
690 let match_ext file ext =
691 List.exists (Filename.check_suffix file) ext
692
693 let get_type file =
694 let ext =
695 List.fold_left
696 (fun acc (ext, t) -> match acc with
697 | Some t -> Some t
698 | None ->
699 if match_ext file ext
700 then Some t
701 else None)
702 None
703 extensions in
704 if Sys.file_exists file then guess_type file
705 else ext
706
707 let is_archive file =
708 get_type file <> None
709
710 let tar_cmd = lazy (
711 match OpamStd.Sys.os () with
712 | OpamStd.Sys.OpenBSD -> "gtar"
713 | _ -> "tar"
714 )
715
716 end
717
718 module Zip = struct
719
720 let is_archive f =
721 if Sys.file_exists f then
722 try
723 let ic = open_in f in
724 let c1 = input_char ic in
725 let c2 = input_char ic in
726 let c3 = input_char ic in
727 let c4 = input_char ic in
728 close_in ic;
729 match c1, c2, c3, c4 with
730 | '\x50', '\x4b', '\x03', '\x04' -> true
731 | _ -> false
732 with Sys_error _ | End_of_file -> false
733 else
734 Filename.check_suffix f "zip"
735 end
736
737 let is_archive file =
738 Tar.is_archive file || Zip.is_archive file
739
740 let link src dst =
741 let fallback () =
742 (* Fall back to copy if symlinks are not supported *)
743 let src =
744 if Filename.is_relative src then Filename.dirname dst / src
745 else src
746 in
747 if Sys.is_directory src then
748 copy_dir src dst
749 else
750 copy_file src dst
751 in
752 mkdir (Filename.dirname dst);
753 if file_or_symlink_exists dst then (
754 (* TODO: move this into remove_file *)
755 if Sys.win32 then Unix.chmod dst 0o640;
756 remove_file dst
757 );
758 if Unix.has_symlink () then
759 try
760 log "ln -s %s %s" src dst;
761 Unix.symlink src dst
762 with Unix.Unix_error (Unix.EXDEV, _, _) ->
763 fallback ()
764 else (
765 log "copy %s -> %s" src dst;
766 fallback ()
767 )
768
769 type actual_lock_flag = [ `Lock_read | `Lock_write ]
770 type lock_flag = [ `Lock_none | actual_lock_flag ]
771
772 type lock = {
773 mutable fd: Unix.file_descr option;
774 file: string;
775 mutable kind: lock_flag;
776 }
777
778 exception Locked
779
780 let unix_lock_op ~dontblock = function
781 | `Lock_read -> if dontblock then Unix.F_TRLOCK else Unix.F_RLOCK
782 | `Lock_write ->
783 if OpamCoreConfig.(!r.safe_mode) then
784 OpamConsole.error_and_exit `Locked "Write lock attempt in safe mode"
785 else
786 if dontblock then Unix.F_TLOCK else Unix.F_LOCK
787
788 let string_of_lock_kind = function
789 | `Lock_none -> "none"
790 | `Lock_read -> "read"
791 | `Lock_write -> "write"
792
793 let locks = Hashtbl.create 16
794
795 let release_all_locks () =
796 Hashtbl.iter (fun fd _ -> Unix.close fd) locks;
797 Hashtbl.clear locks
798
799 let rec flock_update
800 : 'a. ([< lock_flag ] as 'a) -> ?dontblock:bool -> lock -> unit
801 = fun flag ?(dontblock=OpamCoreConfig.(!r.safe_mode)) lock ->
802 log "LOCK %s (%a => %a)" ~level:2 lock.file
803 (slog string_of_lock_kind) (lock.kind)
804 (slog string_of_lock_kind) flag;
805 if lock.kind = (flag :> lock_flag) then ()
806 else
807 match flag, lock with
808 | `Lock_none, { fd = Some fd; kind = (`Lock_read | `Lock_write); _ } ->
809 Hashtbl.remove locks fd;
810 Unix.close fd; (* implies Unix.lockf fd Unix.F_ULOCK 0 *)
811 lock.kind <- (flag :> lock_flag);
812 lock.fd <- None
813 | (`Lock_read | `Lock_write), { fd = None; kind = `Lock_none; file } ->
814 let new_lock = flock flag ~dontblock file in
815 lock.kind <- (flag :> lock_flag);
816 lock.fd <- new_lock.fd
817 | `Lock_write, { fd = Some fd; file; kind = `Lock_read } ->
818 Unix.close fd; (* fd needs read-write reopen *)
819 let new_lock = flock flag ~dontblock file in
820 lock.kind <- (flag :> lock_flag);
821 lock.fd <- new_lock.fd
822 | (`Lock_read | `Lock_write) as flag, { fd = Some fd; file; kind } ->
823 (* Write locks are not recursive on Windows, so only call lockf if necessary *)
824 if kind <> flag then
825 (try
826 (* Locks can't be promoted (or demoted) on Windows - see PR#7264 *)
827 if Sys.win32 && kind <> `Lock_none then
828 Unix.(lockf fd F_ULOCK 0);
829 Unix.lockf fd (unix_lock_op ~dontblock:true flag) 0
830 with Unix.Unix_error (Unix.EAGAIN,_,_)
831 | Unix.Unix_error (Unix.EACCES,_,_) ->
832 if dontblock then
833 OpamConsole.error_and_exit `Locked
834 "Another process has locked %s and non blocking mode enabled"
835 file;
836 OpamConsole.formatted_msg
837 "Another process has locked %s, waiting (%s to abort)... "
838 file (if Sys.win32 then "CTRL+C" else "C-c");
839 let rec lock_w_ignore_sig () =
840 try Unix.lockf fd (unix_lock_op ~dontblock:false flag) 0;
841 with Sys.Break as e -> (OpamConsole.msg "\n"; raise e)
842 | Unix.Unix_error (Unix.EINTR,_,_) -> lock_w_ignore_sig ()
843 in lock_w_ignore_sig ();
844 OpamConsole.msg "lock acquired.\n");
845 lock.kind <- (flag :> lock_flag)
846 | _ -> assert false
847
848 and flock: 'a. ([< lock_flag ] as 'a) -> ?dontblock:bool -> string -> lock =
849 fun flag ?dontblock file ->
850 match flag with
851 | `Lock_none -> { fd = None; file; kind = `Lock_none }
852 | `Lock_write when OpamCoreConfig.(!r.safe_mode) ->
853 OpamConsole.error_and_exit `Locked "Write lock attempt in safe mode";
854 | flag ->
855 mkdir (Filename.dirname file);
856 let rdflag = if (flag :> lock_flag) = `Lock_write then Unix.O_RDWR else Unix.O_RDONLY in
857 let fd = Unix.openfile file Unix.([O_CREAT; O_CLOEXEC; O_SHARE_DELETE; rdflag]) 0o666 in
858 Hashtbl.add locks fd ();
859 let lock = { fd = Some fd; file; kind = `Lock_none } in
860 flock_update flag ?dontblock lock;
861 lock
862
863 let funlock lock = flock_update `Lock_none lock
864
865 let get_lock_flag lock = lock.kind
866
867 let get_lock_fd lock =
868 match lock.fd with
869 Some fd -> fd
870 | None -> raise Not_found
871
872 let lock_max flag1 flag2 = match flag1, flag2 with
873 | `Lock_write, _ | _, `Lock_write -> `Lock_write
874 | `Lock_read, _ | _, `Lock_read -> `Lock_read
875 | `Lock_none, `Lock_none -> `Lock_none
876
877 let lock_none = {
878 fd = None;
879 file = "";
880 kind = `Lock_none;
881 }
882
883 let lock_isatleast flag lock =
884 lock_max flag lock.kind = lock.kind
885
886 let get_eol_encoding file =
887 let ch =
888 try open_in_bin file
889 with Sys_error _ -> raise (File_not_found file)
890 in
891 let has_cr line =
892 let length = String.length line in
893 length > 0 && line.[length - 1] = '\r'
894 in
895 let last_char ch = seek_in ch (in_channel_length ch - 1); input_char ch in
896 let rec read_lines cr line =
897 let has_cr = has_cr line in
898 match input_line ch with
899 | line ->
900 if has_cr = cr then
901 read_lines cr line
902 else begin
903 close_in ch;
904 None
905 end
906 | exception End_of_file ->
907 let result =
908 if cr = has_cr then
909 Some cr
910 else
911 if cr && last_char ch <> '\n' then
912 Some true
913 else
914 None
915 in
916 close_in ch;
917 result
918 in
919 match input_line ch with
920 | line_one ->
921 let has_cr = has_cr line_one in
922 begin match input_line ch with
923 | line_two ->
924 read_lines has_cr line_two
925 | exception End_of_file ->
926 let result =
927 if last_char ch = '\n' then
928 Some has_cr
929 else
930 None
931 in
932 close_in ch;
933 result
934 end
935 | exception End_of_file ->
936 close_in ch;
937 None
938
939 let translate_patch ~dir orig corrected =
940 (* It's unnecessarily complicated to infer whether the entire file is CRLF
941 encoded and also the status of individual files, so accept scanning the
942 file three times instead of two. *)
943 let log ?level fmt = OpamConsole.log "PATCH" ?level fmt in
944 let strip_cr = get_eol_encoding orig = Some true in
945 let ch =
946 try open_in_bin orig
947 with Sys_error _ -> raise (File_not_found orig)
948 in
949 (* CRLF detection with patching can be more complicated than that used here,
950 especially in the presence of files with mixed LF/CRLF endings. The
951 processing done here aims to allow patching to succeed on files which are
952 wholly encoded CRLF or LF against patches which may have been translated to
953 be the opposite.
954
955 The resulting patch will *always* have LF line endings for the patch
956 metadata (headers, chunk locations, etc.) but uses either CRLF or LF
957 depending on the target file. Endings in the patch are always preserved for
958 new files. The benefit of always using LF endings for the metadata is that
959 patch's "Stripping trailing CRs from patch" behaviour won't be triggered.
960
961 There are various patch formats, though only the Unified and Context
962 formats allow multiple files to be patched. I tired of trying to get
963 sufficient documented detail of Context diffs to be able to parse them
964 without resorting to reverse-engineering code. It is unusual to see them
965 these days, so for now opam just emits a warning if a Context diff file is
966 encountered and does no processing to it.
967
968 There are various semantic aspects of Unified diffs which are not handled
969 (at least at present) by this function which are documented in the code
970 with the marker "Weakness". *)
971 let process_chunk_header result line =
972 match OpamStd.String.split line ' ' with
973 | "@@"::a::b::"@@"::_ ->
974 (* Weakness: for a new file [a] should always be -0,0 (not checked) *)
975 let l_a = String.length a in
976 let l_b = String.length b in
977 if l_a > 1 && l_b > 1 && a.[0] = '-' && b.[0] = '+' then
978 try
979 let f (_, v) = int_of_string v in
980 let neg =
981 OpamStd.String.cut_at (String.sub a 1 (l_a - 1)) ','
982 |> OpamStd.Option.map_default f 1
983 in
984 let pos =
985 OpamStd.String.cut_at (String.sub b 1 (l_b - 1)) ','
986 |> OpamStd.Option.map_default f 1
987 in
988 result neg pos
989 with e ->
990 OpamStd.Exn.fatal e;
991 (* TODO Should display some kind of re-sync warning *)
992 `Header
993 else
994 (* TODO Should display some kind of re-sync warning *)
995 `Header
996 | _ ->
997 (* TODO Should display some kind of warning that there were no chunks *)
998 `Header
999 in
1000 let process_state_transition next_state state transforms =
1001 match (state, next_state) with
1002 | (`Processing _, `Processing _) ->
1003 transforms
1004 | (`Processing (_, target, crlf, patch_crlf, chunks, _), _) ->
1005 let compute_transform patch_crlf =
1006 (* Emit the patch *)
1007 let transform =
1008 match (crlf, patch_crlf) with
1009 | (None, _)
1010 | (_, None) ->
1011 log ~level:3 "CRLF adaptation skipped for %s" target;
1012 None
1013 | (Some crlf, Some patch_crlf) ->
1014 if crlf = patch_crlf then begin
1015 log ~level:3 "No CRLF adaptation necessary for %s" target;
1016 None
1017 end else if crlf then begin
1018 log ~level:3 "Adding \\r to patch chunks for %s" target;
1019 Some true
1020 end else begin
1021 log ~level:3 "Stripping \\r to patch chunks for %s" target;
1022 Some false
1023 end
1024 in
1025 let record_transform transform =
1026 let augment_record (first_line, last_line) =
1027 (first_line, last_line, transform)
1028 in
1029 List.rev_append (List.rev_map augment_record chunks) transforms
1030 in
1031 OpamStd.Option.map_default record_transform transforms transform
1032 in
1033 OpamStd.Option.map_default compute_transform transforms patch_crlf
1034 | _ ->
1035 transforms
1036 in
1037 let rec fold_lines state n transforms =
1038 match input_line ch with
1039 | line ->
1040 let line =
1041 if strip_cr then
1042 String.sub line 0 (String.length line - 1)
1043 else
1044 line
1045 in
1046 let length = String.length line in
1047 let next_state =
1048 match state with
1049 | `Header ->
1050 begin
1051 match (if length > 4 then String.sub line 0 4 else "") with
1052 | "--- " ->
1053 (* Start of a unified diff header. *)
1054 let file =
1055 let file = String.sub line 4 (length - 4) in
1056 let open OpamStd in
1057 Option.map_default fst file (String.cut_at file '\t')
1058 in
1059 (* Weakness: new files are also marked with a time-stamp at
1060 the start of the epoch, however it's localised,
1061 making it a bit tricky to identify! New files are
1062 also identified by their absence on disk, so this
1063 weakness isn't particularly critical. *)
1064 if file = "/dev/null" then
1065 `NewHeader
1066 else
1067 let target =
1068 OpamStd.String.cut_at (back_to_forward file) '/'
1069 |> OpamStd.Option.map_default snd file
1070 |> Filename.concat dir
1071 in
1072 if Sys.file_exists target then
1073 let crlf = get_eol_encoding target in
1074 `Patching (file, crlf)
1075 else
1076 `NewHeader
1077 | "*** " ->
1078 OpamConsole.warning "File %s uses context diffs which are \
1079 less portable; consider using unified \
1080 diffs" orig;
1081 `SkipFile
1082 | _ ->
1083 (* Headers will contain other lines, which are ignored (e.g.
1084 the diff command which generated the diff, or Git commit
1085 messages) *)
1086 `Header
1087 end
1088 | `NewHeader ->
1089 if (if length > 4 then String.sub line 0 4 else "") = "+++ " then
1090 `New
1091 else
1092 (* TODO Should display some kind of re-sync warning *)
1093 `Header
1094 | `New ->
1095 process_chunk_header (fun neg pos -> `NewChunk (neg, pos))
1096 line
1097 | `NewChunk (neg, pos) ->
1098 (* Weakness: new files should only have + lines *)
1099 let neg =
1100 if line = "" || line.[0] = ' ' || line.[0] = '-' then
1101 neg - 1
1102 else
1103 neg
1104 in
1105 let pos =
1106 if line = "" || line.[0] = ' ' || line.[0] = '+' then
1107 pos - 1
1108 else
1109 pos
1110 in
1111 if neg = 0 && pos = 0 then
1112 `New
1113 else
1114 (* Weakness: there should only be one chunk for a new file *)
1115 `NewChunk (neg, pos)
1116 | `Patching (orig, crlf) ->
1117 if (if length > 4 then String.sub line 0 4 else "") = "+++ " then
1118 let file =
1119 let file = String.sub line 4 (length - 4) in
1120 let open OpamStd in
1121 Option.map_default fst file (String.cut_at file '\t')
1122 in
1123 `Processing (orig, file, crlf, None, [], `Head)
1124 else
1125 `Header
1126 | `Processing (orig, target, crlf, patch_crlf, chunks, `Head) ->
1127 if line = "\\ No newline at end of file" then
1128 (* If the no eol-at-eof indicator is found, never add \r to
1129 final chunk line *)
1130 let chunks =
1131 match chunks with
1132 | (a, b)::chunks ->
1133 (a, b - 1)::chunks
1134 | _ ->
1135 chunks
1136 in
1137 `Processing (orig, target, crlf, patch_crlf, chunks, `Head)
1138 else
1139 process_chunk_header
1140 (fun neg pos ->
1141 `Processing (orig, target, crlf, patch_crlf, chunks,
1142 `Chunk (succ n, neg, pos)))
1143 line
1144 | `Processing (orig, target, crlf, patch_crlf, chunks,
1145 `Chunk (first_line, neg, pos)) ->
1146 let neg =
1147 if line = "" || line.[0] = ' ' || line.[0] = '-' then
1148 neg - 1
1149 else
1150 neg
1151 in
1152 let pos =
1153 if line = "" || line.[0] = ' ' || line.[0] = '+' then
1154 pos - 1
1155 else
1156 pos
1157 in
1158 let patch_crlf =
1159 let has_cr = (length > 0 && line.[length - 1] = '\r') in
1160 match patch_crlf with
1161 | None ->
1162 Some (Some has_cr)
1163 | Some (Some think_cr) when think_cr <> has_cr ->
1164 log ~level:2 "Patch adaptation disabled for %s: \
1165 mixed endings or binary file" target;
1166 Some None
1167 | _ ->
1168 patch_crlf
1169 in
1170 if neg = 0 && pos = 0 then
1171 let chunks = (first_line, n)::chunks in
1172 `Processing (orig, target, crlf, patch_crlf, chunks, `Head)
1173 else
1174 `Processing (orig, target, crlf, patch_crlf, chunks,
1175 `Chunk (first_line, neg, pos))
1176 | `SkipFile ->
1177 `SkipFile
1178 in
1179 if next_state = `SkipFile then
1180 []
1181 else
1182 process_state_transition next_state state transforms
1183 |> fold_lines next_state (succ n)
1184 | exception End_of_file ->
1185 process_state_transition `Header state transforms |> List.rev
1186 in
1187 let transforms = fold_lines `Header 1 [] in
1188 if transforms = [] then
1189 copy_file orig corrected
1190 else begin
1191 seek_in ch 0;
1192 let ch_out =
1193 try open_out_bin corrected
1194 with Sys_error _ ->
1195 close_in ch;
1196 raise (File_not_found corrected)
1197 in
1198 let (normal, add_cr, strip_cr) =
1199 let strip n s = String.sub s 0 (String.length s - n) in
1200 let id x = x in
1201 if strip_cr then
1202 (strip 1, id, strip 2)
1203 else
1204 (id, (fun s -> s ^ "\r"), strip 1)
1205 in
1206 if OpamConsole.debug () then
1207 let log_transform (first_line, last_line, add_cr) =
1208 let indicator = if add_cr then '+' else '-' in
1209 log ~level:3 "Transform %d-%d %c\\r" first_line last_line indicator
1210 in
1211 List.iter log_transform transforms;
1212 let rec fold_lines n transforms =
1213 match input_line ch with
1214 | line ->
1215 let (f, transforms) =
1216 match transforms with
1217 | (first_line, last_line, add_cr_to_chunks)::next_transforms ->
1218 let transforms =
1219 if n = last_line then
1220 next_transforms
1221 else
1222 transforms
1223 in
1224 let f =
1225 if n >= first_line then
1226 if add_cr_to_chunks then
1227 add_cr
1228 else
1229 strip_cr
1230 else
1231 normal
1232 in
1233 (f, transforms)
1234 | [] ->
1235 (normal, [])
1236 in
1237 output_string ch_out (f line);
1238 output_char ch_out '\n';
1239 fold_lines (succ n) transforms
1240 | exception End_of_file ->
1241 close_out ch_out
1242 in
1243 fold_lines 1 transforms
1244 end;
1245 close_in ch
1246
1247 let register_printer () =
1248 Printexc.register_printer (function
1249 | Internal_error m -> Some m
1250 | Command_not_found c -> Some (Printf.sprintf "%S: command not found." c)
1251 | Permission_denied c -> Some (Printf.sprintf "%S: permission denied." c)
1252 | Sys.Break -> Some "User interruption"
1253 | Unix.Unix_error (e, fn, msg) ->
1254 let msg = if msg = "" then "" else " on " ^ msg in
1255 let error = Printf.sprintf "%s: %S failed%s: %s"
1256 Sys.executable_name fn msg (Unix.error_message e) in
1257 Some error
1258 | _ -> None
1259 )
1260
1261 let init () =
1262 register_printer ();
1263 Sys.catch_break true;
1264 try Sys.set_signal Sys.sigpipe (Sys.Signal_handle (fun _ -> ()))
1265 with Invalid_argument _ -> ()