let mkdir_even_if_exists ?(perm=0o700) dirname =
let fail_here e =
fail_sys (`Make_directory dirname, e) in
Lwt.catch
Lwt.(fun () -> Lwt_unix.mkdir dirname perm >>= fun () -> return (`Ok ()))
begin function
| Unix.Unix_error (Unix.EACCES, cmd, arg) ->
fail_here (`Wrong_access_rights perm)
| Unix.Unix_error (Unix.EISDIR, cmd, arg) ->
(* Bypass MacOSX bug https://github.com/janestreet/core/issues/7 *)
return ()
| Unix.Unix_error (Unix.EEXIST, cmd, arg) -> return ()
| e -> fail_here (`Exn e)
end