Skip to content

Commit ee6d70a

Browse files
authored
refactor: extract [Fpath.mkdir_p_strict] (#13515)
This function will be useful elsewhere --------- Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
1 parent 7614c41 commit ee6d70a

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

bin/install_uninstall.ml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,11 @@ module File_ops_real (W : sig
357357
;;
358358

359359
let mkdir_p p =
360-
(* CR-someday amokhov: We should really change [Path.mkdir_p dir] to fail if
361-
it turns out that [dir] exists and is not a directory. Even better, make
362-
[Path.mkdir_p] return an explicit variant to deal with. *)
363-
match Fpath.mkdir_p (Path.to_string p) with
364-
| `Created -> ()
365-
| `Already_exists ->
366-
(match Path.is_directory p with
367-
| true -> ()
368-
| false ->
369-
User_error.raise
370-
[ Pp.textf "Please delete file %s manually." (Path.to_string_maybe_quoted p) ])
360+
match Fpath.mkdir_p_strict (Path.to_string p) with
361+
| `Created | `Already_exists -> ()
362+
| `Not_a_dir ->
363+
User_error.raise
364+
[ Pp.textf "Please delete file %s manually." (Path.to_string_maybe_quoted p) ]
371365
;;
372366
end
373367

otherlibs/stdune/src/fpath.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ let rec mkdir_p ?perms t_s =
5757
Code_error.raise "failed to create parent directory" [ "t_s", Dyn.string t_s ]))
5858
;;
5959

60+
let mkdir_p_strict ?perms t_s =
61+
match mkdir_p ?perms t_s with
62+
| `Created -> `Created
63+
| `Already_exists ->
64+
(match (Unix.stat t_s).st_kind with
65+
| S_DIR -> `Already_exists
66+
| _ -> `Not_a_dir)
67+
;;
68+
6069
let link src dst =
6170
match Unix.link src dst with
6271
| exception Unix.Unix_error (Unix.EUNKNOWNERR -1142, syscall, arg)

otherlibs/stdune/src/fpath.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type mkdir_p_result =
1616

1717
val dyn_of_mkdir_p_result : mkdir_p_result -> Dyn.t
1818
val mkdir_p : ?perms:int -> string -> mkdir_p_result
19+
val mkdir_p_strict : ?perms:int -> string -> [ mkdir_p_result | `Not_a_dir ]
1920

2021
(** [link src dst] creates a hardlink from [src] to [dst]. *)
2122
val link : string -> string -> unit

0 commit comments

Comments
 (0)