From cb535f1fa9068ec8fce5dba7b326a420b320d2ef Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Sun, 22 Mar 2015 19:38:00 +0200 Subject: [PATCH] Add file_type method to the reactor This method check the file type and retrun an optional value, if the file does not exists no value is return. On other errors an exception will be thrown. Signed-off-by: Amnon Heiman --- core/reactor.cc | 39 +++++++++++++++++++++++++++++++++++++++ core/reactor.hh | 1 + 2 files changed, 40 insertions(+) diff --git a/core/reactor.cc b/core/reactor.cc index 1533223c68..b0036cd802 100644 --- a/core/reactor.cc +++ b/core/reactor.cc @@ -392,6 +392,45 @@ reactor::open_file_dma(sstring name, open_flags flags) { }); } +directory_entry_type stat_to_entry_type(__mode_t type) { + if (S_ISDIR(type)) { + return directory_entry_type::directory; + } + if (S_ISBLK(type)) { + return directory_entry_type::block_device; + } + if (S_ISCHR(type)) { + return directory_entry_type::char_device; + } + if (S_ISFIFO(type)) { + return directory_entry_type::fifo; + } + if (S_ISLNK(type)) { + return directory_entry_type::link; + } + return directory_entry_type::regular; + +} + +future> +reactor::file_type(sstring name) { + return _thread_pool.submit>([name] { + struct stat st; + auto ret = stat(name.c_str(), &st); + return wrap_syscall(ret, st); + }).then([] (syscall_result_extra sr) { + if (long(sr.result) == -1) { + if (sr.result != ENOENT && sr.result != ENOTDIR) { + sr.throw_if_error(); + } + return make_ready_future > + (std::experimental::optional() ); + } + return make_ready_future > + (std::experimental::optional(stat_to_entry_type(sr.extra.st_mode)) ); + }); +} + future reactor::open_directory(sstring name) { return _thread_pool.submit>([name] { diff --git a/core/reactor.hh b/core/reactor.hh index fbb5143871..f372328841 100644 --- a/core/reactor.hh +++ b/core/reactor.hh @@ -749,6 +749,7 @@ public: future open_file_dma(sstring name, open_flags flags); future open_directory(sstring name); + future> file_type(sstring name); template future submit_io(Func prepare_io);