From 92803a2db3818995bee6ee4d5113ff2e99e6e075 Mon Sep 17 00:00:00 2001 From: Glauber Costa Date: Wed, 17 Jun 2015 14:45:53 -0400 Subject: [PATCH] touch_directory: EEXIST-ignoring make directory There are many situations in which we would like to make sure a directory exists. We can do that by creating the directory we want, and just ignoring the relevant error. It is a lot of code though, and I believe it is an idiom common enough to exist on its own. Signed-off-by: Glauber Costa Reviewed-by: Nadav Har'El --- core/reactor.cc | 12 ++++++++++++ core/seastar.hh | 1 + 2 files changed, 13 insertions(+) diff --git a/core/reactor.cc b/core/reactor.cc index f035caf64b..6ce1eec136 100644 --- a/core/reactor.cc +++ b/core/reactor.cc @@ -1917,6 +1917,18 @@ future<> make_directory(sstring name) { return engine().make_directory(std::move(name)); } +future<> touch_directory(sstring name) { + return make_directory(name).then_wrapped([] (future<> f) { + try { + f.get(); + } catch (std::system_error& e) { + if (e.code() != std::error_code(EEXIST, std::system_category())) { + throw; + } + } + }); +} + future<> remove_file(sstring pathname) { return engine().remove_file(std::move(pathname)); } diff --git a/core/seastar.hh b/core/seastar.hh index 9c6017b493..4b15860946 100644 --- a/core/seastar.hh +++ b/core/seastar.hh @@ -108,5 +108,6 @@ future connect(socket_address sa); future open_file_dma(sstring name, open_flags flags); future open_directory(sstring name); future<> make_directory(sstring name); +future<> touch_directory(sstring name); future<> remove_file(sstring pathname);