Small function to create in OCaml-based code a directory and its
parents, much like `mkdir -p`.
---
mllib/common_utils.ml | 11 +++++++++++
mllib/common_utils.mli | 3 +++
2 files changed, 14 insertions(+)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 898be17..76d8b79 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -673,3 +673,14 @@ let qemu_input_filename filename =
"./" ^ filename
else
filename
+
+let rec mkdir_p path permissions =
+ try Unix.mkdir path permissions
+ with
+ | Unix.Unix_error (Unix.EEXIST, _, _) -> ()
+ | Unix.Unix_error (Unix.ENOENT, _, _) ->
+ (* A component in the path does not exist, so first try
+ * creating the parent directory, and then again the requested
+ * directory. *)
+ mkdir_p (Filename.dirname path) permissions;
+ Unix.mkdir path permissions
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 5d3149a..28ba648 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -147,3 +147,6 @@ val qemu_input_filename : string -> string
try to interpret that as "foo" in the file:/// protocol. To
avoid that, if the path is relative prefix it with "./" since
qemu-img won't try to interpret such a path. *)
+
+val mkdir_p : string -> int -> unit
+(** Creates a directory, and its parents if missing. *)
--
2.1.0