| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 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 |
|
| 21 |
|
| 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 |
|
| 29 |
|
| 30 |
let dirsep = Filename.dir_sep |
| 31 |
let dirsep_len = String.length dirsep |
| 32 |
let curdir = Filename.concat Filename.current_dir_name "" |
| 33 |
let curdir_len = String.length curdir |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
let strip_path p = |
| 57 |
let cwd = Filename.concat (Sys.getcwd ()) "" in |
| 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 |
|
| 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 |
|
| 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 |
|
| 91 |
|
| 92 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 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 |