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.

142 lines
3.6 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 "branches.hpp"
  16. #include "errno.hpp"
  17. #include "fs_closedir.hpp"
  18. #include "fs_devid.hpp"
  19. #include "fs_dirfd.hpp"
  20. #include "fs_fstatat.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 "fuse.h"
  28. #include "fuse_dirents.h"
  29. #include <string>
  30. #include <vector>
  31. #include <dirent.h>
  32. using std::string;
  33. using std::vector;
  34. namespace l
  35. {
  36. static
  37. uint64_t
  38. dirent_exact_namelen(const struct dirent *d_)
  39. {
  40. #ifdef _D_EXACT_NAMLEN
  41. return _D_EXACT_NAMLEN(d_);
  42. #elif defined _DIRENT_HAVE_D_NAMLEN
  43. return d_->d_namlen;
  44. #else
  45. return strlen(d_->d_name);
  46. #endif
  47. }
  48. static
  49. int
  50. readdir_plus(const Branches::CPtr &branches_,
  51. const char *dirname_,
  52. const uint64_t entry_timeout_,
  53. const uint64_t attr_timeout_,
  54. fuse_dirents_t *buf_)
  55. {
  56. dev_t dev;
  57. HashSet names;
  58. string basepath;
  59. string fullpath;
  60. struct stat st;
  61. uint64_t namelen;
  62. fuse_entry_t entry;
  63. fuse_dirents_reset(buf_);
  64. entry.nodeid = 0;
  65. entry.generation = 0;
  66. entry.entry_valid = entry_timeout_;
  67. entry.attr_valid = attr_timeout_;
  68. entry.entry_valid_nsec = 0;
  69. entry.attr_valid_nsec = 0;
  70. for(auto &branch : *branches_)
  71. {
  72. int rv;
  73. int dirfd;
  74. DIR *dh;
  75. basepath = fs::path::make(branch.path,dirname_);
  76. dh = fs::opendir(basepath);
  77. if(!dh)
  78. continue;
  79. dirfd = fs::dirfd(dh);
  80. dev = fs::devid(dirfd);
  81. rv = 0;
  82. for(struct dirent *de = fs::readdir(dh); de && !rv; de = fs::readdir(dh))
  83. {
  84. namelen = l::dirent_exact_namelen(de);
  85. rv = names.put(de->d_name,namelen);
  86. if(rv == 0)
  87. continue;
  88. rv = fs::fstatat_nofollow(dirfd,de->d_name,&st);
  89. if(rv == -1)
  90. {
  91. memset(&st,0,sizeof(st));
  92. st.st_ino = de->d_ino;
  93. st.st_dev = dev;
  94. st.st_mode = DTTOIF(de->d_type);
  95. }
  96. fullpath = fs::path::make(dirname_,de->d_name);
  97. fs::inode::calc(fullpath,&st);
  98. de->d_ino = st.st_ino;
  99. rv = fuse_dirents_add_plus(buf_,de,namelen,&entry,&st);
  100. if(rv)
  101. return (fs::closedir(dh),-ENOMEM);
  102. }
  103. fs::closedir(dh);
  104. }
  105. return 0;
  106. }
  107. }
  108. namespace FUSE
  109. {
  110. int
  111. readdir_plus_posix(const Branches::CPtr &branches_,
  112. const char *dirname_,
  113. const uint64_t entry_timeout_,
  114. const uint64_t attr_timeout_,
  115. fuse_dirents_t *buf_)
  116. {
  117. return l::readdir_plus(branches_,dirname_,entry_timeout_,attr_timeout_,buf_);
  118. }
  119. }