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
Pino Toscano
2015-Feb-23  13:09 UTC
[Libguestfs] [PATCH 2/2] builder: use mkdir_p to create the cachedir (RHBZ#1195204)
---
 builder/cache.ml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/builder/cache.ml b/builder/cache.ml
index 9e69bc1..86ac41b 100644
--- a/builder/cache.ml
+++ b/builder/cache.ml
@@ -35,7 +35,7 @@ type t = {
 
 let create ~verbose ~directory    if not (is_directory directory) then
-    mkdir directory 0o755;
+    mkdir_p directory 0o755;
   {
     verbose = verbose;
     directory = directory;
-- 
2.1.0
Richard W.M. Jones
2015-Feb-23  21:13 UTC
Re: [Libguestfs] [PATCH 2/2] builder: use mkdir_p to create the cachedir (RHBZ#1195204)
ACK series. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
Kashyap Chamarthy
2015-Feb-24  10:12 UTC
Re: [Libguestfs] [PATCH 2/2] builder: use mkdir_p to create the cachedir (RHBZ#1195204)
On Mon, Feb 23, 2015 at 02:09:18PM +0100, Pino Toscano wrote:> --- > builder/cache.ml | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-)ACK. Just tested locally with this.> diff --git a/builder/cache.ml b/builder/cache.ml > index 9e69bc1..86ac41b 100644 > --- a/builder/cache.ml > +++ b/builder/cache.ml > @@ -35,7 +35,7 @@ type t = { > > let create ~verbose ~directory > if not (is_directory directory) then > - mkdir directory 0o755; > + mkdir_p directory 0o755; > { > verbose = verbose; > directory = directory; > -- > 2.1.0-- /kashyap
Apparently Analagous Threads
- [PATCH 1/2] mllib: add normalize_arch helper
- [PATCH 1/2] mllib: add simple qemu filename sanitizing function
- [PATCH 5/5] mllib: add a new run_command helper
- [PATCH 1/2] mllib: allow external_command to return on nonzero return value
- Re: [PATCH 1/2] mllib: add normalize_arch helper