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 <glommer@cloudius-systems.com>
Reviewed-by: Nadav Har'El <nyh@cloudius-systems.com>
This commit is contained in:
Glauber Costa
2015-06-17 14:45:53 -04:00
committed by Avi Kivity
parent 534401c91f
commit 92803a2db3
2 changed files with 13 additions and 0 deletions

View File

@@ -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));
}

View File

@@ -108,5 +108,6 @@ future<connected_socket> connect(socket_address sa);
future<file> open_file_dma(sstring name, open_flags flags);
future<file> open_directory(sstring name);
future<> make_directory(sstring name);
future<> touch_directory(sstring name);
future<> remove_file(sstring pathname);