From a65ad526202e5abd0c5e41e364fbe263350d7858 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Mon, 23 Jan 2017 11:09:39 +0100 Subject: [PATCH] Shell: improve `Lwt_utils` --- src/utils/lwt_utils.ml | 16 ++++++++++------ src/utils/lwt_utils.mli | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/utils/lwt_utils.ml b/src/utils/lwt_utils.ml index 00f857ab9..ceb472d13 100644 --- a/src/utils/lwt_utils.ml +++ b/src/utils/lwt_utils.ml @@ -346,12 +346,14 @@ let remove_dir dir = Lwt.return () let rec create_dir ?(perm = 0o755) dir = - if Sys.file_exists dir then - Lwt.return () - else begin - create_dir (Filename.dirname dir) >>= fun () -> - Lwt_unix.mkdir dir perm - end + Lwt_unix.file_exists dir >>= function + | false -> + create_dir (Filename.dirname dir) >>= fun () -> + Lwt_unix.mkdir dir perm + | true -> + Lwt_unix.stat dir >>= function + | {st_kind = S_DIR} -> Lwt.return_unit + | _ -> failwith "Not a directory" let create_file ?(perm = 0o644) name content = Lwt_unix.openfile name Unix.([O_TRUNC; O_CREAT; O_WRONLY]) perm >>= fun fd -> @@ -402,4 +404,6 @@ let with_timeout ?(canceler = Canceler.create ()) timeout f = Canceler.cancel canceler >>= fun () -> fail Timeout +let unless cond f = + if cond then Lwt.return () else f () diff --git a/src/utils/lwt_utils.mli b/src/utils/lwt_utils.mli index 78cf995a2..14f48cf90 100644 --- a/src/utils/lwt_utils.mli +++ b/src/utils/lwt_utils.mli @@ -67,3 +67,5 @@ val with_timeout: ?canceler:Canceler.t -> float -> (Canceler.t -> 'a tzresult Lwt.t) -> 'a tzresult Lwt.t +val unless: bool -> (unit -> unit Lwt.t) -> unit Lwt.t +