let mkdir_or_fail ?(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.EEXIST, cmd, arg) ->
fail_here (`Already_exists)
| Unix.Unix_error (Unix.EISDIR, cmd, arg) ->
(* Bypass MacOSX bug https://github.com/janestreet/core/issues/7 *)
fail_here (`Already_exists)
| e -> fail_here (`Exn e)
end