| 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 |
module Base = struct |
| 13 |
include OpamStd.AbstractString |
| 14 |
|
| 15 |
let compare = String.compare |
| 16 |
let equal = String.equal |
| 17 |
|
| 18 |
let check_suffix filename s = |
| 19 |
Filename.check_suffix filename s |
| 20 |
|
| 21 |
let add_extension filename suffix = |
| 22 |
filename ^ "." ^ suffix |
| 23 |
end |
| 24 |
|
| 25 |
let log fmt = OpamConsole.log "FILENAME" fmt |
| 26 |
let slog = OpamConsole.slog |
| 27 |
|
| 28 |
module Dir = struct |
| 29 |
|
| 30 |
include OpamStd.AbstractString |
| 31 |
|
| 32 |
let compare = String.compare |
| 33 |
let equal = String.equal |
| 34 |
|
| 35 |
let of_string dirname = |
| 36 |
let dirname = |
| 37 |
if dirname = "~" then OpamStd.Sys.home () |
| 38 |
else if |
| 39 |
OpamStd.String.starts_with ~prefix:("~"^Filename.dir_sep) dirname |
| 40 |
then |
| 41 |
Filename.concat (OpamStd.Sys.home ()) |
| 42 |
(OpamStd.String.remove_prefix ~prefix:("~"^Filename.dir_sep) dirname) |
| 43 |
else dirname |
| 44 |
in |
| 45 |
OpamSystem.real_path (OpamSystem.forward_to_back dirname) |
| 46 |
|
| 47 |
let to_string dirname = dirname |
| 48 |
|
| 49 |
end |
| 50 |
|
| 51 |
let raw_dir s = s |
| 52 |
|
| 53 |
let mk_tmp_dir () = |
| 54 |
Dir.of_string @@ OpamSystem.mk_temp_dir () |
| 55 |
|
| 56 |
let with_tmp_dir fn = |
| 57 |
OpamSystem.with_tmp_dir (fun dir -> fn (Dir.of_string dir)) |
| 58 |
|
| 59 |
let rmdir dirname = |
| 60 |
OpamSystem.remove_dir (Dir.to_string dirname) |
| 61 |
|
| 62 |
let rec rmdir_cleanup dirname = |
| 63 |
let sd = Dir.to_string dirname in |
| 64 |
if OpamSystem.dir_is_empty sd then ( |
| 65 |
rmdir dirname; |
| 66 |
let parent = Filename.dirname sd in |
| 67 |
if parent <> sd then rmdir_cleanup parent |
| 68 |
) |
| 69 |
|
| 70 |
let cwd () = |
| 71 |
Dir.of_string (Unix.getcwd ()) |
| 72 |
|
| 73 |
let mkdir dirname = |
| 74 |
OpamSystem.mkdir (Dir.to_string dirname) |
| 75 |
|
| 76 |
let exists_dir dirname = |
| 77 |
try (Unix.stat (Dir.to_string dirname)).Unix.st_kind = Unix.S_DIR |
| 78 |
with Unix.Unix_error _ -> false |
| 79 |
|
| 80 |
let cleandir dirname = |
| 81 |
if exists_dir dirname then |
| 82 |
(log "cleandir %a" (slog Dir.to_string) dirname; |
| 83 |
OpamSystem.remove (Dir.to_string dirname); |
| 84 |
mkdir dirname) |
| 85 |
|
| 86 |
let rec_dirs d = |
| 87 |
let fs = OpamSystem.rec_dirs (Dir.to_string d) in |
| 88 |
List.rev (List.rev_map Dir.of_string fs) |
| 89 |
|
| 90 |
let dirs d = |
| 91 |
let fs = OpamSystem.dirs (Dir.to_string d) in |
| 92 |
List.rev (List.rev_map Dir.of_string fs) |
| 93 |
|
| 94 |
let dir_is_empty d = |
| 95 |
OpamSystem.dir_is_empty (Dir.to_string d) |
| 96 |
|
| 97 |
let in_dir dirname fn = OpamSystem.in_dir dirname fn |
| 98 |
|
| 99 |
let env_of_list l = Array.of_list (List.rev_map (fun (k,v) -> k^"="^v) l) |
| 100 |
|
| 101 |
let move_dir ~src ~dst = |
| 102 |
OpamSystem.mv (Dir.to_string src) (Dir.to_string dst) |
| 103 |
|
| 104 |
let opt_dir dirname = |
| 105 |
if exists_dir dirname then Some dirname else None |
| 106 |
|
| 107 |
let basename_dir dirname = |
| 108 |
Base.of_string (Filename.basename (Dir.to_string dirname)) |
| 109 |
|
| 110 |
let dirname_dir dirname = Filename.dirname (Dir.to_string dirname) |
| 111 |
|
| 112 |
let link_dir ~target ~link = |
| 113 |
if exists_dir link then |
| 114 |
OpamSystem.internal_error "Cannot link: %s already exists." |
| 115 |
(Dir.to_string link) |
| 116 |
else |
| 117 |
OpamSystem.link (Dir.to_string target) (Dir.to_string link) |
| 118 |
|
| 119 |
let to_list_dir dir = |
| 120 |
let base d = Dir.of_string (Filename.basename (Dir.to_string d)) in |
| 121 |
let rec aux acc dir = |
| 122 |
let d = dirname_dir dir in |
| 123 |
if d <> dir then aux (base dir :: acc) d |
| 124 |
else base dir :: acc in |
| 125 |
aux [] dir |
| 126 |
|
| 127 |
let (/) d1 s2 = |
| 128 |
let s1 = Dir.to_string d1 in |
| 129 |
raw_dir (Filename.concat s1 s2) |
| 130 |
|
| 131 |
let concat_and_resolve d1 s2 = |
| 132 |
let s1 = Dir.to_string d1 in |
| 133 |
Dir.of_string (Filename.concat s1 s2) |
| 134 |
|
| 135 |
type t = { |
| 136 |
dirname: Dir.t; |
| 137 |
basename: Base.t; |
| 138 |
} |
| 139 |
|
| 140 |
let create dirname basename = |
| 141 |
let b1 = OpamSystem.forward_to_back (Filename.dirname (Base.to_string basename)) in |
| 142 |
let b2 = Base.of_string (Filename.basename (Base.to_string basename)) in |
| 143 |
let dirname = OpamSystem.forward_to_back dirname in |
| 144 |
if basename = b2 then |
| 145 |
{ dirname; basename } |
| 146 |
else |
| 147 |
{ dirname = dirname / b1; basename = b2 } |
| 148 |
|
| 149 |
let of_basename basename = |
| 150 |
let dirname = Dir.of_string Filename.current_dir_name in |
| 151 |
{ dirname; basename } |
| 152 |
|
| 153 |
let raw str = |
| 154 |
let dirname = raw_dir (Filename.dirname str) in |
| 155 |
let basename = Base.of_string (Filename.basename str) in |
| 156 |
create dirname basename |
| 157 |
|
| 158 |
let to_string t = |
| 159 |
Filename.concat (Dir.to_string t.dirname) (Base.to_string t.basename) |
| 160 |
|
| 161 |
let touch t = |
| 162 |
OpamSystem.write (to_string t) "" |
| 163 |
|
| 164 |
let chmod t p = |
| 165 |
Unix.chmod (to_string t) p |
| 166 |
|
| 167 |
let written_since file = |
| 168 |
let last_update = |
| 169 |
(Unix.stat (to_string file)).Unix.st_mtime |
| 170 |
in |
| 171 |
(Unix.time () -. last_update) |
| 172 |
|
| 173 |
let of_string s = |
| 174 |
let dirname = Filename.dirname s in |
| 175 |
let basename = Filename.basename s in |
| 176 |
{ |
| 177 |
dirname = Dir.of_string dirname; |
| 178 |
basename = Base.of_string basename; |
| 179 |
} |
| 180 |
|
| 181 |
let dirname t = t.dirname |
| 182 |
|
| 183 |
let basename t = t.basename |
| 184 |
|
| 185 |
let read filename = |
| 186 |
OpamSystem.read (to_string filename) |
| 187 |
|
| 188 |
let open_in filename = |
| 189 |
try open_in (to_string filename) |
| 190 |
with Sys_error _ -> raise (OpamSystem.File_not_found (to_string filename)) |
| 191 |
|
| 192 |
let open_in_bin filename = |
| 193 |
try open_in_bin (to_string filename) |
| 194 |
with Sys_error _ -> raise (OpamSystem.File_not_found (to_string filename)) |
| 195 |
|
| 196 |
let open_out filename = |
| 197 |
try open_out (to_string filename) |
| 198 |
with Sys_error _ -> raise (OpamSystem.File_not_found (to_string filename)) |
| 199 |
|
| 200 |
let open_out_bin filename = |
| 201 |
try open_out_bin (to_string filename) |
| 202 |
with Sys_error _ -> raise (OpamSystem.File_not_found (to_string filename)) |
| 203 |
|
| 204 |
let write filename raw = |
| 205 |
OpamSystem.write (to_string filename) raw |
| 206 |
|
| 207 |
let remove filename = |
| 208 |
OpamSystem.remove_file (to_string filename) |
| 209 |
|
| 210 |
let with_open_out_bin_aux open_out_bin filename f = |
| 211 |
let v, oc = |
| 212 |
mkdir (dirname filename); |
| 213 |
try open_out_bin filename |
| 214 |
with Sys_error _ -> raise (OpamSystem.File_not_found (to_string filename)) |
| 215 |
in |
| 216 |
try |
| 217 |
Unix.lockf (Unix.descr_of_out_channel oc) Unix.F_LOCK 0; |
| 218 |
f oc; |
| 219 |
close_out oc; |
| 220 |
v |
| 221 |
with e -> |
| 222 |
OpamStd.Exn.finalise e @@ fun () -> |
| 223 |
close_out oc; remove filename |
| 224 |
|
| 225 |
let with_open_out_bin = |
| 226 |
with_open_out_bin_aux (fun f -> (), open_out_bin f) |
| 227 |
|
| 228 |
let with_open_out_bin_atomic filename f = |
| 229 |
let open_temp_file filename = |
| 230 |
let mode = [Open_binary] in |
| 231 |
let perms = 0o666 in |
| 232 |
let temp_dir = Dir.to_string (dirname filename) in |
| 233 |
Filename.open_temp_file ~mode ~perms ~temp_dir "opam-atomic" ".tmp" |
| 234 |
in |
| 235 |
let temp_file = with_open_out_bin_aux open_temp_file filename f in |
| 236 |
try |
| 237 |
Sys.rename temp_file (to_string filename) |
| 238 |
with Sys_error _ -> |
| 239 |
OpamSystem.remove_file temp_file; |
| 240 |
raise (OpamSystem.File_not_found (to_string filename)) |
| 241 |
|
| 242 |
let exists filename = |
| 243 |
try (Unix.stat (to_string filename)).Unix.st_kind = Unix.S_REG |
| 244 |
with Unix.Unix_error _ -> false |
| 245 |
|
| 246 |
let opt_file filename = |
| 247 |
if exists filename then Some filename else None |
| 248 |
|
| 249 |
let with_contents fn filename = |
| 250 |
fn (read filename) |
| 251 |
|
| 252 |
let check_suffix filename s = |
| 253 |
Filename.check_suffix (to_string filename) s |
| 254 |
|
| 255 |
let add_extension filename suffix = |
| 256 |
of_string ((to_string filename) ^ "." ^ suffix) |
| 257 |
|
| 258 |
let chop_extension filename = |
| 259 |
of_string (Filename.chop_extension (to_string filename)) |
| 260 |
|
| 261 |
let rec_files d = |
| 262 |
let fs = OpamSystem.rec_files (Dir.to_string d) in |
| 263 |
List.rev_map of_string fs |
| 264 |
|
| 265 |
let files d = |
| 266 |
let fs = OpamSystem.files (Dir.to_string d) in |
| 267 |
List.rev_map of_string fs |
| 268 |
|
| 269 |
let files_and_links d = |
| 270 |
let fs = OpamSystem.files_all_not_dir (Dir.to_string d) in |
| 271 |
List.rev_map of_string fs |
| 272 |
|
| 273 |
let copy ~src ~dst = |
| 274 |
if src <> dst then OpamSystem.copy_file (to_string src) (to_string dst) |
| 275 |
|
| 276 |
let copy_dir ~src ~dst = |
| 277 |
if src <> dst then OpamSystem.copy_dir (Dir.to_string src) (Dir.to_string dst) |
| 278 |
|
| 279 |
let install ?warning ?exec ~src ~dst () = |
| 280 |
if src <> dst then OpamSystem.install ?warning ?exec (to_string src) (to_string dst) |
| 281 |
|
| 282 |
let move ~src ~dst = |
| 283 |
if src <> dst then |
| 284 |
OpamSystem.mv (to_string src) (to_string dst) |
| 285 |
|
| 286 |
let readlink src = |
| 287 |
if exists src then |
| 288 |
try |
| 289 |
let rl = Unix.readlink (to_string src) in |
| 290 |
if Filename.is_relative rl then |
| 291 |
of_string (Filename.concat (dirname src) rl) |
| 292 |
else of_string rl |
| 293 |
with Unix.Unix_error _ -> src |
| 294 |
else |
| 295 |
OpamSystem.internal_error "%s does not exist." (to_string src) |
| 296 |
|
| 297 |
let is_symlink src = |
| 298 |
try |
| 299 |
let s = Unix.lstat (to_string src) in |
| 300 |
s.Unix.st_kind = Unix.S_LNK |
| 301 |
with Unix.Unix_error _ -> false |
| 302 |
|
| 303 |
let is_symlink_dir src = |
| 304 |
try |
| 305 |
let s = Unix.lstat (Dir.to_string src) in |
| 306 |
s.Unix.st_kind = Unix.S_LNK |
| 307 |
with Unix.Unix_error _ -> false |
| 308 |
|
| 309 |
let is_exec file = |
| 310 |
try OpamSystem.is_exec (to_string file) |
| 311 |
with Unix.Unix_error _ -> |
| 312 |
OpamSystem.internal_error "%s does not exist." (to_string file) |
| 313 |
|
| 314 |
let starts_with dirname filename = |
| 315 |
OpamStd.String.starts_with ~prefix:(Dir.to_string dirname) (to_string filename) |
| 316 |
|
| 317 |
let dir_starts_with pfx dir = |
| 318 |
OpamStd.String.starts_with ~prefix:(Dir.to_string pfx) (Dir.to_string dir) |
| 319 |
|
| 320 |
let remove_prefix prefix filename = |
| 321 |
let prefix = |
| 322 |
let str = Dir.to_string prefix in |
| 323 |
if str = "" then "" else Filename.concat str "" in |
| 324 |
let filename = to_string filename in |
| 325 |
OpamStd.String.remove_prefix ~prefix filename |
| 326 |
|
| 327 |
let remove_prefix_dir prefix dir = |
| 328 |
let prefix = Dir.to_string prefix in |
| 329 |
let dirname = Dir.to_string dir in |
| 330 |
if prefix = "" then dirname |
| 331 |
else |
| 332 |
OpamStd.String.remove_prefix ~prefix dirname |> |
| 333 |
OpamStd.String.remove_prefix ~prefix:Filename.dir_sep |
| 334 |
|
| 335 |
let process_in ?root fn src dst = |
| 336 |
let basename = match root with |
| 337 |
| None -> basename src |
| 338 |
| Some r -> |
| 339 |
if starts_with r src then remove_prefix r src |
| 340 |
else OpamSystem.internal_error "%s is not a prefix of %s" |
| 341 |
(Dir.to_string r) (to_string src) in |
| 342 |
let dst = Filename.concat (Dir.to_string dst) basename in |
| 343 |
fn ~src ~dst:(of_string dst) |
| 344 |
|
| 345 |
let copy_in ?root = process_in ?root copy |
| 346 |
|
| 347 |
let is_archive filename = |
| 348 |
OpamSystem.is_archive (to_string filename) |
| 349 |
|
| 350 |
type generic_file = |
| 351 |
| D of Dir.t |
| 352 |
| F of t |
| 353 |
|
| 354 |
let extract_generic_file filename dirname = |
| 355 |
match filename with |
| 356 |
| F f -> |
| 357 |
log "extracting %a to %a" |
| 358 |
(slog to_string) f |
| 359 |
(slog Dir.to_string) dirname; |
| 360 |
| D d -> |
| 361 |
if d <> dirname then ( |
| 362 |
log "copying %a to %a" |
| 363 |
(slog Dir.to_string) d |
| 364 |
(slog Dir.to_string) dirname; |
| 365 |
copy_dir ~src:d ~dst:dirname |
| 366 |
) |
| 367 |
|
| 368 |
let ends_with suffix filename = |
| 369 |
OpamStd.String.ends_with ~suffix (to_string filename) |
| 370 |
|
| 371 |
let dir_ends_with suffix dirname = |
| 372 |
OpamStd.String.ends_with ~suffix (Dir.to_string dirname) |
| 373 |
|
| 374 |
let remove_suffix suffix filename = |
| 375 |
let suffix = Base.to_string suffix in |
| 376 |
let filename = to_string filename in |
| 377 |
OpamStd.String.remove_suffix ~suffix filename |
| 378 |
|
| 379 |
let rec find_in_parents f dir = |
| 380 |
if f dir then Some dir else |
| 381 |
let parent = dirname_dir dir in |
| 382 |
if parent = dir then None |
| 383 |
else find_in_parents f parent |
| 384 |
|
| 385 |
let link ?(relative=false) ~target ~link = |
| 386 |
if target = link then () else |
| 387 |
let target = |
| 388 |
if not relative then to_string target else |
| 389 |
match |
| 390 |
find_in_parents (fun d -> d <> "/" && starts_with d link) (dirname target) |
| 391 |
with |
| 392 |
| None -> to_string target |
| 393 |
| Some ancestor -> |
| 394 |
let back = |
| 395 |
let rel = remove_prefix_dir ancestor (dirname link) in |
| 396 |
OpamStd.List.concat_map Filename.dir_sep |
| 397 |
(fun _ -> "..") |
| 398 |
(OpamStd.String.split rel Filename.dir_sep.[0]) |
| 399 |
in |
| 400 |
let forward = remove_prefix ancestor target in |
| 401 |
Filename.concat back forward |
| 402 |
in |
| 403 |
OpamSystem.link target (to_string link) |
| 404 |
[@@ocaml.warning "-16"] |
| 405 |
|
| 406 |
let flock flag ?dontblock file = OpamSystem.flock flag ?dontblock (to_string file) |
| 407 |
|
| 408 |
let with_flock flag ?dontblock file f = |
| 409 |
let lock = OpamSystem.flock flag ?dontblock (to_string file) in |
| 410 |
try |
| 411 |
let (fd, ch) = |
| 412 |
match OpamSystem.get_lock_fd lock with |
| 413 |
| exception Not_found -> |
| 414 |
let null = |
| 415 |
if OpamStd.Sys.(os () = Win32) then |
| 416 |
"nul" |
| 417 |
else |
| 418 |
"/dev/null" |
| 419 |
in |
| 420 |
let ch = Stdlib.open_out null in |
| 421 |
Unix.descr_of_out_channel ch, Some ch |
| 422 |
| fd -> |
| 423 |
fd, None |
| 424 |
in |
| 425 |
let r = f fd in |
| 426 |
OpamSystem.funlock lock; |
| 427 |
OpamStd.Option.iter Stdlib.close_out ch; |
| 428 |
r |
| 429 |
with e -> |
| 430 |
OpamStd.Exn.finalise e @@ fun () -> |
| 431 |
OpamSystem.funlock lock |
| 432 |
|
| 433 |
let with_flock_upgrade flag ?dontblock lock f = |
| 434 |
if OpamSystem.lock_isatleast flag lock then f (OpamSystem.get_lock_fd lock) |
| 435 |
else ( |
| 436 |
let old_flag = OpamSystem.get_lock_flag lock in |
| 437 |
OpamSystem.flock_update flag ?dontblock lock; |
| 438 |
try |
| 439 |
let r = f (OpamSystem.get_lock_fd lock) in |
| 440 |
OpamSystem.flock_update old_flag lock; |
| 441 |
r |
| 442 |
with e -> |
| 443 |
OpamStd.Exn.finalise e @@ fun () -> |
| 444 |
OpamSystem.flock_update old_flag lock |
| 445 |
) |
| 446 |
|
| 447 |
let with_flock_write_then_read ?dontblock file write read = |
| 448 |
let lock = OpamSystem.flock `Lock_write ?dontblock (to_string file) in |
| 449 |
try |
| 450 |
let r = write (OpamSystem.get_lock_fd lock) in |
| 451 |
OpamSystem.flock_update `Lock_read lock; |
| 452 |
let r = read r in |
| 453 |
OpamSystem.funlock lock; |
| 454 |
r |
| 455 |
with e -> |
| 456 |
OpamStd.Exn.finalise e @@ fun () -> |
| 457 |
OpamSystem.funlock lock |
| 458 |
|
| 459 |
let prettify_path s = |
| 460 |
let aux ~short ~prefix = |
| 461 |
let prefix = Filename.concat prefix "" in |
| 462 |
if OpamStd.String.starts_with ~prefix s then |
| 463 |
let suffix = OpamStd.String.remove_prefix ~prefix s in |
| 464 |
Some (Filename.concat short suffix) |
| 465 |
else |
| 466 |
None in |
| 467 |
try |
| 468 |
match aux ~short:"~" ~prefix:(OpamStd.Sys.home ()) with |
| 469 |
| Some p -> p |
| 470 |
| None -> s |
| 471 |
with Not_found -> s |
| 472 |
|
| 473 |
let prettify_dir d = |
| 474 |
prettify_path (Dir.to_string d) |
| 475 |
|
| 476 |
let prettify s = |
| 477 |
prettify_path (to_string s) |
| 478 |
|
| 479 |
let to_json x = `String (to_string x) |
| 480 |
let of_json = function |
| 481 |
| `String x -> (try Some (of_string x) with _ -> None) |
| 482 |
| _ -> None |
| 483 |
|
| 484 |
let compare {dirname; basename} f = |
| 485 |
let dir = Dir.compare dirname f.dirname in |
| 486 |
if dir <> 0 then dir else |
| 487 |
Base.compare basename f.basename |
| 488 |
|
| 489 |
let equal f g = compare f g = 0 |
| 490 |
|
| 491 |
module O = struct |
| 492 |
type tmp = t |
| 493 |
type t = tmp |
| 494 |
let compare = compare |
| 495 |
let to_string = to_string |
| 496 |
let to_json = to_json |
| 497 |
let of_json = of_json |
| 498 |
end |
| 499 |
|
| 500 |
module Map = OpamStd.Map.Make(O) |
| 501 |
module Set = OpamStd.Set.Make(O) |
| 502 |
|
| 503 |
module SubPath = struct |
| 504 |
|
| 505 |
include OpamStd.AbstractString |
| 506 |
|
| 507 |
let compare = String.compare |
| 508 |
let equal = String.equal |
| 509 |
|
| 510 |
let of_string s = |
| 511 |
OpamSystem.back_to_forward s |
| 512 |
|> OpamStd.String.remove_prefix ~prefix:"./" |
| 513 |
|> of_string |
| 514 |
let to_string = OpamSystem.forward_to_back |
| 515 |
let normalised_string s = s |
| 516 |
|
| 517 |
let (/) d s = d / to_string s |
| 518 |
let (/?) d = function |
| 519 |
| None -> d |
| 520 |
| Some s -> d / to_string s |
| 521 |
|
| 522 |
end |
| 523 |
|
| 524 |
module Op = struct |
| 525 |
|
| 526 |
let (/) = (/) |
| 527 |
|
| 528 |
let (//) d1 s2 = |
| 529 |
let d = Filename.dirname s2 in |
| 530 |
let b = Filename.basename s2 in |
| 531 |
if d <> "." then |
| 532 |
create (d1 / d) (Base.of_string b) |
| 533 |
else |
| 534 |
create d1 (Base.of_string s2) |
| 535 |
|
| 536 |
end |
| 537 |
|
| 538 |
module Attribute = struct |
| 539 |
|
| 540 |
type t = { |
| 541 |
base: Base.t; |
| 542 |
md5 : OpamHash.t; |
| 543 |
perm: int option; |
| 544 |
} |
| 545 |
|
| 546 |
let base t = t.base |
| 547 |
|
| 548 |
let md5 t = t.md5 |
| 549 |
|
| 550 |
let perm t = t.perm |
| 551 |
|
| 552 |
let create base md5 perm = |
| 553 |
{ base; md5; perm=perm } |
| 554 |
|
| 555 |
let to_string_list t = |
| 556 |
let perm = match t.perm with |
| 557 |
| None -> [] |
| 558 |
| Some p -> [Printf.sprintf "0o%o" p] in |
| 559 |
Base.to_string t.base :: OpamHash.to_string t.md5 :: perm |
| 560 |
|
| 561 |
let of_string_list = function |
| 562 |
| [base; md5] -> |
| 563 |
{ base=Base.of_string base; md5=OpamHash.of_string md5; perm=None } |
| 564 |
| [base;md5; perm] -> |
| 565 |
{ base=Base.of_string base; |
| 566 |
md5=OpamHash.of_string md5; |
| 567 |
perm=Some (int_of_string perm) } |
| 568 |
| k -> OpamSystem.internal_error |
| 569 |
"remote_file: '%s' is not a valid line." |
| 570 |
(String.concat " " k) |
| 571 |
|
| 572 |
let to_string t = String.concat " " (to_string_list t) |
| 573 |
let of_string s = of_string_list (OpamStd.String.split s ' ') |
| 574 |
|
| 575 |
let to_json x = |
| 576 |
`O ([ ("base" , Base.to_json x.base); |
| 577 |
("md5" , `String (OpamHash.to_string x.md5))] |
| 578 |
@ match x. perm with |
| 579 |
| None -> [] |
| 580 |
| Some p -> ["perm", `String (string_of_int p)]) |
| 581 |
|
| 582 |
let of_json = function |
| 583 |
| `O dict -> |
| 584 |
begin try |
| 585 |
let open OpamStd.Option.Op in |
| 586 |
Base.of_json (OpamStd.List.assoc String.equal "base" dict) |
| 587 |
>>= fun base -> |
| 588 |
OpamHash.of_json (OpamStd.List.assoc String.equal "md5" dict) |
| 589 |
>>= fun md5 -> |
| 590 |
let perm = |
| 591 |
if not (OpamStd.List.mem_assoc String.equal "perm" dict) then None |
| 592 |
else match OpamStd.List.assoc String.equal "perm" dict with |
| 593 |
| `String hash -> |
| 594 |
(try Some (int_of_string hash) with _ -> raise Not_found) |
| 595 |
| _ -> raise Not_found |
| 596 |
in |
| 597 |
Some { base; md5; perm } |
| 598 |
with Not_found -> None |
| 599 |
end |
| 600 |
| _ -> None |
| 601 |
|
| 602 |
let compare {base; md5; perm} a = |
| 603 |
let base = Base.compare base a.base in |
| 604 |
if base <> 0 then base else |
| 605 |
let md5 = OpamHash.compare md5 a.md5 in |
| 606 |
if md5 <> 0 then md5 else |
| 607 |
OpamStd.Option.compare Int.compare perm a.perm |
| 608 |
|
| 609 |
let equal a b = compare a b = 0 |
| 610 |
|
| 611 |
module O = struct |
| 612 |
type tmp = t |
| 613 |
type t = tmp |
| 614 |
let to_string = to_string |
| 615 |
let compare = compare |
| 616 |
let to_json = to_json |
| 617 |
let of_json = of_json |
| 618 |
end |
| 619 |
|
| 620 |
module Set = OpamStd.Set.Make(O) |
| 621 |
|
| 622 |
module Map = OpamStd.Map.Make(O) |
| 623 |
|
| 624 |
end |
| 625 |
|
| 626 |
let to_attribute root file = |
| 627 |
let basename = Base.of_string (remove_prefix root file) in |
| 628 |
let perm = |
| 629 |
let s = Unix.stat (to_string file) in |
| 630 |
s.Unix.st_perm in |
| 631 |
let digest = OpamHash.compute ~kind:`MD5 (to_string file) in |
| 632 |
Attribute.create basename digest (Some perm) |