let ensure_directory_path ?perm dirname =
(* Code inspired by Core.Std.Unix *)
let init, dirs =
match Filename.parts dirname with
| [] -> ksprintf failwith "Sys.mkdir_p: BUG! Filename.parts %s -> []" dirname
| init :: dirs -> (init, dirs)
in
mkdir_even_if_exists ?perm init
>>= fun () ->
List.fold_left dirs ~init:(return init) ~f:(fun m part ->
m >>= fun previous ->
let dir = Filename.concat previous part in
mkdir_even_if_exists ?perm dir
>>= fun () ->
return dir)
>>= fun _ ->
return ()