Skip to content

Commit f6baa94

Browse files
dra27rjbou
authored andcommitted
system: add mkdir_unique_dir
1 parent d308529 commit f6baa94

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

master_changes.md

+1
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,4 @@ users)
214214
* `OpamCompat`: add `Seq.find_map` from OCaml 4.14 [#6000 @dra27]
215215
* `OpamStd.Sys.{get_windows_executable_variant,get_cygwin_variant,is_cygwin_variant}`: renamed `~cygbin` to `?search_in_path` with a change in semantics so that it acts as though the directory was simply the first entry in PATH [#6000 @dra27]
216216
* `OpamConsole.Symbols`: add `collision` symbol [#5457 @dra27]
217+
* `OpamSystem`: add `mk_unique_dir` that returns an unique directory name as `mk_temp_dir` but not in temporary directory [#5457 @dra27]

src/core/opamSystem.ml

+7
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ let rec mk_temp_dir ?(prefix="opam") () =
8888
else
8989
real_path s
9090

91+
let rec mk_unique_dir ~dir ?(prefix="opam") () =
92+
let s = dir / Printf.sprintf "%s-%06x" prefix (Random.int 0xFFFFFF) in
93+
if Sys.file_exists s then
94+
mk_unique_dir ~dir ~prefix ()
95+
else
96+
real_path s
97+
9198
let safe_mkdir dir =
9299
try
93100
log "mkdir %s" dir;

src/core/opamSystem.mli

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ val verbose_for_base_commands: unit -> bool
5151
(if [prefix] is not set), pid, and random number. *)
5252
val mk_temp_dir: ?prefix:string -> unit -> string
5353

54+
(** Returns a directory name, in the [~dir], composed by {i opam}
55+
(if [prefix] is not set), and a random number. *)
56+
val mk_unique_dir: dir:string -> ?prefix:string -> unit -> string
57+
5458
(** [copy_file src dst] copies [src] to [dst]. Remove [dst] before the copy
5559
if it is a link. *)
5660
val copy_file: string -> string -> unit

0 commit comments

Comments
 (0)