package owl-zoo

  1. Overview
  2. Docs

Source file owl_zoo_path.ml

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
38
39
40
41
42
43
44
45
46
47
(*
 * OWL - OCaml Scientific and Engineering Computing
 * Copyright (c) 2016-2020 Liang Wang <liang.wang@cl.cam.ac.uk>
 *)

(* Root of Zoo System *)
let dir = Sys.getenv "HOME" ^ "/.owl/zoo"

(* Path of Zoo version database *)
let htb = dir ^ "/" ^ "zoo_ver.htb"

(* Used internally *)
let gist_path gid vid = dir ^ "/" ^ gid ^ "/" ^ vid

(* Used by script developers *)
let extend_zoo_path ?(gid = "") ?(vid = "") filepath =
  match gid, vid with
  | "", "" -> filepath
  | g, v   -> gist_path g v ^ "/" ^ filepath


(* Make temporary directory *)
let mk_temp_dir ?(mode = 0o700) ?dir pat =
  let dir =
    match dir with
    | Some d -> d
    | None   -> Filename.get_temp_dir_name ()
  in
  let rand_digits () =
    let rand = Random.State.(bits (make_self_init ()) land 0xFFFFFF) in
    Printf.sprintf "%06x" rand
  in
  let raise_err msg = raise (Sys_error ("mk_temp_dir: " ^ msg)) in
  let rec loop count =
    if count < 0
    then raise_err "too many failing attemps"
    else (
      let dir = Printf.sprintf "%s/%s%s" dir pat (rand_digits ()) in
      try
        Unix.mkdir dir mode;
        dir
      with
      | Unix.Unix_error (Unix.EEXIST, _, _) -> loop (count - 1)
      | Unix.Unix_error (Unix.EINTR, _, _) -> loop count
      | Unix.Unix_error (e, _, _) -> raise_err (Unix.error_message e))
  in
  loop 1000
OCaml

Innovation. Community. Security.