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