Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Use random ints as suffix for temporary file names
Browse files Browse the repository at this point in the history
Previously, we used the timestamp to create temporary file names and
this could lead to non-unique file names. This commit improves the
generation of temporary directory names and reduces the chances of
conflicting directory names.
  • Loading branch information
punchagan committed Sep 17, 2024
1 parent 328b98e commit b00f2e0
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/dir_helpers.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
* Copyright (c) 2024 Puneeth Chaganti <punchagan@muse-amuse.in>, Shon Feder <shon.feder@gmail.com>, Tarides <contact@tarides.com>
*)

(*Generate random temporary file names. **)
let temp_file_name dir prefix suffix =
let prng = Random.State.make_self_init () in
let random_suffix = string_of_int (Random.State.int prng 0x10000000) in
Filename.concat dir (prefix ^ random_suffix ^ suffix)

(*Create a temporary directory with the given prefix. **)
let create_temp_dir prefix =
let base_temp_dir = Filename.get_temp_dir_name () in
let unique_temp_dir =
Filename.concat base_temp_dir
(prefix ^ (Unix.time () |> int_of_float |> string_of_int))
in
let unique_temp_dir = temp_file_name base_temp_dir prefix "" in
Unix.mkdir unique_temp_dir 0o700;
unique_temp_dir

Expand Down

0 comments on commit b00f2e0

Please sign in to comment.