diff options
| author | Pierre-Marie Pédrot | 2020-01-13 21:05:34 +0100 |
|---|---|---|
| committer | Pierre-Marie Pédrot | 2020-01-13 21:05:34 +0100 |
| commit | 507141cb978ae9383b79e4a6af6ab968cb8d540e (patch) | |
| tree | e28165b0a567c78296f2c075ccadf25ce27ef0bb /clib/cUnix.ml | |
| parent | 7cde333abd7a1c25765a9438d1b830a133a15498 (diff) | |
| parent | a0c02da54bfedeaaa73b1188c3e2e0cd9a4e086b (diff) | |
Merge PR #11081: Native compute: cleanup temporary files on program exit
Reviewed-by: JasonGross
Reviewed-by: Zimmi48
Reviewed-by: maximedenes
Reviewed-by: ppedrot
Diffstat (limited to 'clib/cUnix.ml')
| -rw-r--r-- | clib/cUnix.ml | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/clib/cUnix.ml b/clib/cUnix.ml index c5f6bebb8e..6e3ad59b1f 100644 --- a/clib/cUnix.ml +++ b/clib/cUnix.ml @@ -140,3 +140,20 @@ let same_file f1 = Unix.Unix_error _ -> false) with Unix.Unix_error _ -> (fun _ -> false) + +(* Copied from ocaml filename.ml *) +let prng = lazy(Random.State.make_self_init ()) + +let temp_file_name temp_dir prefix suffix = + let rnd = (Random.State.bits (Lazy.force prng)) land 0xFFFFFF in + Filename.concat temp_dir (Printf.sprintf "%s%06x%s" prefix rnd suffix) + +let mktemp_dir ?(temp_dir=Filename.get_temp_dir_name()) prefix suffix = + let rec try_name counter = + let name = temp_file_name temp_dir prefix suffix in + match Unix.mkdir name 0o700 with + | () -> name + | exception (Sys_error _ as e) -> + if counter >= 1000 then raise e else try_name (counter + 1) + in + try_name 0 |
