You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
4.9 KiB

  1. /*
  2. Copyright (c) 2023, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "fuse_readdir_cosr.hpp"
  15. #include "config.hpp"
  16. #include "dirinfo.hpp"
  17. #include "errno.hpp"
  18. #include "fs_closedir.hpp"
  19. #include "fs_devid.hpp"
  20. #include "fs_dirfd.hpp"
  21. #include "fs_inode.hpp"
  22. #include "fs_opendir.hpp"
  23. #include "fs_path.hpp"
  24. #include "fs_readdir.hpp"
  25. #include "fs_stat.hpp"
  26. #include "hashset.hpp"
  27. #include "scope_guard.hpp"
  28. #include "ugid.hpp"
  29. #include "fuse_dirents.h"
  30. FUSE::ReadDirCOSR::ReadDirCOSR(unsigned concurrency_,
  31. unsigned max_queue_depth_)
  32. : _tp(concurrency_,max_queue_depth_,"readdir.cosr")
  33. {
  34. }
  35. FUSE::ReadDirCOSR::~ReadDirCOSR()
  36. {
  37. }
  38. namespace l
  39. {
  40. struct DirRV
  41. {
  42. DIR *dir;
  43. int err;
  44. };
  45. struct Error
  46. {
  47. private:
  48. int _err;
  49. public:
  50. Error()
  51. : _err(ENOENT)
  52. {
  53. }
  54. operator int()
  55. {
  56. return _err;
  57. }
  58. Error&
  59. operator=(int const v_)
  60. {
  61. if(_err != 0)
  62. _err = v_;
  63. return *this;
  64. }
  65. };
  66. static
  67. uint64_t
  68. dirent_exact_namelen(const struct dirent *d_)
  69. {
  70. #ifdef _D_EXACT_NAMLEN
  71. return _D_EXACT_NAMLEN(d_);
  72. #elif defined _DIRENT_HAVE_D_NAMLEN
  73. return d_->d_namlen;
  74. #else
  75. return strlen(d_->d_name);
  76. #endif
  77. }
  78. static
  79. inline
  80. std::vector<std::future<DirRV>>
  81. opendir(ThreadPool &tp_,
  82. const Branches::CPtr &branches_,
  83. char const *dirname_,
  84. uid_t const uid_,
  85. gid_t const gid_)
  86. {
  87. std::vector<std::future<DirRV>> futures;
  88. futures.reserve(branches_->size());
  89. for(auto const &branch : *branches_)
  90. {
  91. auto func = [&branch,dirname_,uid_,gid_]()
  92. {
  93. DirRV rv;
  94. std::string basepath;
  95. ugid::Set const ugid(uid_,gid_);
  96. basepath = fs::path::make(branch.path,dirname_);
  97. errno = 0;
  98. rv.dir = fs::opendir(basepath);
  99. rv.err = errno;
  100. return rv;
  101. };
  102. auto rv = tp_.enqueue_task(func);
  103. futures.emplace_back(std::move(rv));
  104. }
  105. return futures;
  106. }
  107. static
  108. inline
  109. int
  110. readdir(std::vector<std::future<DirRV>> &dh_futures_,
  111. char const *dirname_,
  112. fuse_dirents_t *buf_)
  113. {
  114. Error error;
  115. HashSet names;
  116. std::string fullpath;
  117. for(auto &dh_future : dh_futures_)
  118. {
  119. int rv;
  120. dev_t dev;
  121. DirRV dirrv;
  122. dirrv = dh_future.get();
  123. error = dirrv.err;
  124. if(dirrv.dir == NULL)
  125. continue;
  126. DEFER { fs::closedir(dirrv.dir); };
  127. dev = fs::devid(dirrv.dir);
  128. rv = 0;
  129. for(dirent *de = fs::readdir(dirrv.dir); de && !rv; de = fs::readdir(dirrv.dir))
  130. {
  131. std::uint64_t namelen;
  132. namelen = l::dirent_exact_namelen(de);
  133. rv = names.put(de->d_name,namelen);
  134. if(rv == 0)
  135. continue;
  136. fullpath = fs::path::make(dirname_,de->d_name);
  137. de->d_ino = fs::inode::calc(fullpath,
  138. DTTOIF(de->d_type),
  139. dev,
  140. de->d_ino);
  141. rv = fuse_dirents_add(buf_,de,namelen);
  142. if(rv == 0)
  143. continue;
  144. error = ENOMEM;
  145. }
  146. }
  147. return -error;
  148. }
  149. static
  150. inline
  151. int
  152. readdir(ThreadPool &tp_,
  153. const Branches::CPtr &branches_,
  154. const char *dirname_,
  155. fuse_dirents_t *buf_,
  156. uid_t const uid_,
  157. gid_t const gid_)
  158. {
  159. int rv;
  160. std::vector<std::future<DirRV>> futures;
  161. fuse_dirents_reset(buf_);
  162. futures = l::opendir(tp_,branches_,dirname_,uid_,gid_);
  163. rv = l::readdir(futures,dirname_,buf_);
  164. return rv;
  165. }
  166. }
  167. int
  168. FUSE::ReadDirCOSR::operator()(fuse_file_info_t const *ffi_,
  169. fuse_dirents_t *buf_)
  170. {
  171. Config::Read cfg;
  172. DirInfo *di = reinterpret_cast<DirInfo*>(ffi_->fh);
  173. const fuse_context *fc = fuse_get_context();
  174. return l::readdir(_tp,
  175. cfg->branches,
  176. di->fusepath.c_str(),
  177. buf_,
  178. fc->uid,
  179. fc->gid);
  180. }