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.

155 lines
3.4 KiB

  1. /*
  2. Copyright (c) 2019, 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. #define _DEFAULT_SOURCE
  15. #include "fuse_readdir_seq.hpp"
  16. #include "branches.hpp"
  17. #include "config.hpp"
  18. #include "dirinfo.hpp"
  19. #include "errno.hpp"
  20. #include "fs_closedir.hpp"
  21. #include "fs_devid.hpp"
  22. #include "fs_dirfd.hpp"
  23. #include "fs_inode.hpp"
  24. #include "fs_opendir.hpp"
  25. #include "fs_path.hpp"
  26. #include "fs_readdir.hpp"
  27. #include "fs_stat.hpp"
  28. #include "hashset.hpp"
  29. #include "scope_guard.hpp"
  30. #include "ugid.hpp"
  31. #include "fuse.h"
  32. #include "fuse_dirents.h"
  33. #include <string>
  34. namespace l
  35. {
  36. struct Error
  37. {
  38. private:
  39. int _err;
  40. public:
  41. Error()
  42. : _err(ENOENT)
  43. {
  44. }
  45. operator int()
  46. {
  47. return _err;
  48. }
  49. Error&
  50. operator=(int v_)
  51. {
  52. if(_err != 0)
  53. _err = v_;
  54. return *this;
  55. }
  56. };
  57. static
  58. uint64_t
  59. dirent_exact_namelen(const struct dirent *d_)
  60. {
  61. #ifdef _D_EXACT_NAMLEN
  62. return _D_EXACT_NAMLEN(d_);
  63. #elif defined _DIRENT_HAVE_D_NAMLEN
  64. return d_->d_namlen;
  65. #else
  66. return strlen(d_->d_name);
  67. #endif
  68. }
  69. static
  70. int
  71. readdir(const Branches::CPtr &branches_,
  72. const char *dirname_,
  73. fuse_dirents_t *buf_)
  74. {
  75. Error error;
  76. HashSet names;
  77. std::string basepath;
  78. std::string fullpath;
  79. fuse_dirents_reset(buf_);
  80. for(auto const &branch : *branches_)
  81. {
  82. int rv;
  83. DIR *dh;
  84. dev_t dev;
  85. basepath = fs::path::make(branch.path,dirname_);
  86. errno = 0;
  87. dh = fs::opendir(basepath);
  88. error = errno;
  89. if(!dh)
  90. continue;
  91. DEFER{ fs::closedir(dh); };
  92. dev = fs::devid(dh);
  93. rv = 0;
  94. for(dirent *de = fs::readdir(dh); de && !rv; de = fs::readdir(dh))
  95. {
  96. std::uint64_t namelen;
  97. namelen = l::dirent_exact_namelen(de);
  98. rv = names.put(de->d_name,namelen);
  99. if(rv == 0)
  100. continue;
  101. fullpath = fs::path::make(dirname_,de->d_name);
  102. de->d_ino = fs::inode::calc(fullpath,
  103. DTTOIF(de->d_type),
  104. dev,
  105. de->d_ino);
  106. rv = fuse_dirents_add(buf_,de,namelen);
  107. if(rv)
  108. return -ENOMEM;
  109. }
  110. }
  111. return -error;
  112. }
  113. }
  114. int
  115. FUSE::ReadDirSeq::operator()(fuse_file_info_t const *ffi_,
  116. fuse_dirents_t *buf_)
  117. {
  118. Config::Read cfg;
  119. DirInfo *di = reinterpret_cast<DirInfo*>(ffi_->fh);
  120. const fuse_context *fc = fuse_get_context();
  121. const ugid::Set ugid(fc->uid,fc->gid);
  122. return l::readdir(cfg->branches,
  123. di->fusepath.c_str(),
  124. buf_);
  125. }