~james/reorder

reorder/src/store.ml -rw-r--r-- 1.3 KiB
735c0c8f — james chore(import): snapshot james.dad.town/reorder at c27220bf810c 16 hours ago
                                                                                
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
let read path =
  let channel = open_in_bin path in
  Fun.protect ~finally:(fun () -> close_in_noerr channel) (fun () ->
    really_input_string channel (in_channel_length channel))

let rec mkdir path =
  if path <> "." && not (Sys.file_exists path) then begin
    mkdir (Filename.dirname path);
    Unix.mkdir path 0o755
  end

let write path text =
  mkdir (Filename.dirname path);
  let temporary, channel = Filename.open_temp_file ~mode:[Open_binary]
    ~temp_dir:(Filename.dirname path) ".reorder" ".tmp" in
  Fun.protect ~finally:(fun () ->
    close_out_noerr channel;
    if Sys.file_exists temporary then Sys.remove temporary) (fun () ->
      output_string channel text;
      close_out channel;
      Sys.rename temporary path)

let root () =
  let rec find path =
    if Sys.file_exists (Filename.concat path "smt/machine.smt2") then path
    else
      let parent = Filename.dirname path in
      if parent = path then invalid_arg "cannot locate smt/machine.smt2"
      else find parent
  in
  match Sys.getenv_opt "REORDER_ROOT" with
  | Some path -> find path
  | None ->
      try find (Sys.getcwd ()) with Invalid_argument _ ->
        let executable = if Filename.is_relative Sys.executable_name then
          Filename.concat (Sys.getcwd ()) Sys.executable_name else Sys.executable_name in
        find (Filename.dirname executable)