Debian Codegraph @

clib/cUnix.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Files and load path. *)
12
13 type physical_path = string
14 type load_path = physical_path list
15
16 let physical_path_of_string s = s
17 let string_of_physical_path p = p
18
19 let escaped_string_of_physical_path p =
20 (* We assume a reasonable-enough path (typically utf8) and prevents
21 the presence of space; other escapings might be useful... *)
22 if String.contains p ' ' then "\"" ^ p ^ "\"" else p
23
24 let path_to_list p =
25 let sep = Str.regexp (if Sys.os_type = "Win32" then ";" else ":") in
26 Str.split sep p
27
28 (* Some static definitions concerning filenames *)
29
30 let dirsep = Filename.dir_sep (* Unix: "/" *)
31 let dirsep_len = String.length dirsep
32 let curdir = Filename.concat Filename.current_dir_name "" (* Unix: "./" *)
33 let curdir_len = String.length curdir
34
35 (* Hints to partially detects if two paths refer to the same directory *)
36
37 (** cut path [p] after all the [/] that come at position [pos]. *)
38 let rec cut_after_dirsep p pos =
39 if CString.is_sub dirsep p pos then
40 cut_after_dirsep p (pos + dirsep_len)
41 else
42 String.sub p pos (String.length p - pos)
43
44 (** remove all initial "./" in a path unless the path is exactly "./" *)
45 let rec remove_path_dot p =
46 if CString.is_sub curdir p 0 then
47 if String.length p = curdir_len
48 then Filename.current_dir_name
49 else remove_path_dot (cut_after_dirsep p curdir_len)
50 else
51 p
52
53 (** If a path [p] starts with the current directory $PWD then
54 [strip_path p] returns the sub-path relative to $PWD.
55 Any leading "./" are also removed from the result. *)
56 let strip_path p =
57 let cwd = Filename.concat (Sys.getcwd ()) "" in (* Unix: "`pwd`/" *)
58 if CString.is_sub cwd p 0 then
59 remove_path_dot (cut_after_dirsep p (String.length cwd))
60 else
61 remove_path_dot p
62
63 let canonical_path_name p =
64 let current = Sys.getcwd () in
65 try
66 Sys.chdir p;
67 let p' = Sys.getcwd () in
68 Sys.chdir current;
69 p'
70 with Sys_error _ ->
71 (* We give up to find a canonical name and just simplify it... *)
72 current ^ dirsep ^ strip_path p
73
74 let make_suffix name suffix =
75 if Filename.check_suffix name suffix then name else (name ^ suffix)
76
77 let correct_path f dir =
78 if Filename.is_relative f then Filename.concat dir f else f
79
80 let file_readable_p name =
81 try Unix.access name [Unix.R_OK];true
82 with Unix.Unix_error (_, _, _) -> false
83
84 (* As for [Unix.close_process], a [Unix.waipid] that ignores all [EINTR] *)
85
86 let rec waitpid_non_intr pid =
87 try snd (Unix.waitpid [] pid)
88 with Unix.Unix_error (Unix.EINTR, _, _) -> waitpid_non_intr pid
89
90 (** [run_command com] launches command [com] (via /bin/sh),
91 and returns the contents of stdout and stderr. If given, [~hook]
92 is called on each elements read on stdout or stderr. *)
93
94 let run_command ?(hook=(fun _ ->())) c =
95 let result = Buffer.create 127 in
96 let cin,cout,cerr = Unix.open_process_full c (Unix.environment ()) in
97 let buff = Bytes.make 127 ' ' in
98 let buffe = Bytes.make 127 ' ' in
99 let n = ref 0 in
100 let ne = ref 0 in
101 while n:= input cin buff 0 127 ; ne := input cerr buffe 0 127 ;
102 !n+ !ne <> 0
103 do
104 let r = Bytes.sub buff 0 !n in (hook r; Buffer.add_bytes result r);
105 let r = Bytes.sub buffe 0 !ne in (hook r; Buffer.add_bytes result r);
106 done;
107 (Unix.close_process_full (cin,cout,cerr), Buffer.contents result)
108
109 (** [sys_command] launches program [prog] with arguments [args].
110 It behaves like [Sys.command], except that we rely on
111 [Unix.create_process], it's hardly more complex and avoids dealing
112 with shells. In particular, no need to quote arguments
113 (against whitespace or other funny chars in paths), hence no need
114 to care about the different quoting conventions of /bin/sh and cmd.exe. *)
115
116 let sys_command prog args =
117 let argv = Array.of_list (prog::args) in
118 let pid = Unix.create_process prog argv Unix.stdin Unix.stdout Unix.stderr in
119 waitpid_non_intr pid
120
121 (*
122 checks if two file names refer to the same (existing) file by
123 comparing their device and inode.
124 It seems that under Windows, inode is always 0, so we cannot
125 accurately check if
126
127 *)
128 (* Optimised for partial application (in case many candidates must be
129 compared to f1). *)
130 let same_file f1 =
131 try
132 let s1 = Unix.stat f1 in
133 (fun f2 ->
134 try
135 let s2 = Unix.stat f2 in
136 s1.Unix.st_dev = s2.Unix.st_dev &&
137 if Sys.os_type = "Win32" then f1 = f2
138 else s1.Unix.st_ino = s2.Unix.st_ino
139 with
140 Unix.Unix_error _ -> false)
141 with
142 Unix.Unix_error _ -> (fun _ -> false)
143
144 (* Copied from ocaml filename.ml *)
145 let prng = lazy(Random.State.make_self_init ())
146
147 let temp_file_name temp_dir prefix suffix =
148 let rnd = (Random.State.bits (Lazy.force prng)) land 0xFFFFFF in
149 Filename.concat temp_dir (Printf.sprintf "%s%06x%s" prefix rnd suffix)
150
151 let mktemp_dir ?(temp_dir=Filename.get_temp_dir_name()) prefix suffix =
152 let rec try_name counter =
153 let name = temp_file_name temp_dir prefix suffix in
154 match Unix.mkdir name 0o700 with
155 | () -> name
156 | exception (Sys_error _ as e) ->
157 if counter >= 1000 then raise e else try_name (counter + 1)
158 in
159 try_name 0