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.

151 lines
4.0 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. #include "branches.hpp"
  15. #include "errno.hpp"
  16. #include "fs_close.hpp"
  17. #include "fs_devid.hpp"
  18. #include "fs_fstatat.hpp"
  19. #include "fs_getdents64.hpp"
  20. #include "fs_inode.hpp"
  21. #include "fs_open.hpp"
  22. #include "fs_path.hpp"
  23. #include "fs_stat.hpp"
  24. #include "hashset.hpp"
  25. #include "linux_dirent64.h"
  26. #include "mempools.hpp"
  27. #include "fuse.h"
  28. #include "fuse_dirents.h"
  29. #include <string>
  30. #include <vector>
  31. #include <stddef.h>
  32. using std::string;
  33. using std::vector;
  34. namespace l
  35. {
  36. static
  37. int
  38. close_free_ret_enomem(int fd_,
  39. void *buf_)
  40. {
  41. fs::close(fd_);
  42. g_DENTS_BUF_POOL.free(buf_);
  43. return -ENOMEM;
  44. }
  45. static
  46. int
  47. readdir_plus(const Branches::CPtr &branches_,
  48. const char *dirname_,
  49. const uint64_t entry_timeout_,
  50. const uint64_t attr_timeout_,
  51. fuse_dirents_t *buf_)
  52. {
  53. int rv;
  54. dev_t dev;
  55. char *buf;
  56. HashSet names;
  57. string basepath;
  58. string fullpath;
  59. uint64_t namelen;
  60. struct stat st;
  61. fuse_entry_t entry;
  62. struct linux_dirent64 *d;
  63. fuse_dirents_reset(buf_);
  64. buf = (char*)g_DENTS_BUF_POOL.alloc();
  65. entry.nodeid = 0;
  66. entry.generation = 0;
  67. entry.entry_valid = entry_timeout_;
  68. entry.attr_valid = attr_timeout_;
  69. entry.entry_valid_nsec = 0;
  70. entry.attr_valid_nsec = 0;
  71. for(auto &branch : *branches_)
  72. {
  73. int dirfd;
  74. int64_t nread;
  75. basepath = fs::path::make(branch.path,dirname_);
  76. dirfd = fs::open_dir_ro(basepath);
  77. if(dirfd == -1)
  78. continue;
  79. dev = fs::devid(dirfd);
  80. for(;;)
  81. {
  82. nread = fs::getdents_64(dirfd,buf,g_DENTS_BUF_POOL.size());
  83. if(nread == -1)
  84. break;
  85. if(nread == 0)
  86. break;
  87. for(int64_t pos = 0; pos < nread; pos += d->reclen)
  88. {
  89. d = (struct linux_dirent64*)(buf + pos);
  90. namelen = (strlen(d->name) + 1);
  91. rv = names.put(d->name,namelen);
  92. if(rv == 0)
  93. continue;
  94. rv = fs::fstatat_nofollow(dirfd,d->name,&st);
  95. if(rv == -1)
  96. {
  97. memset(&st,0,sizeof(st));
  98. st.st_ino = d->ino;
  99. st.st_dev = dev;
  100. st.st_mode = DTTOIF(d->type);
  101. }
  102. fullpath = fs::path::make(dirname_,d->name);
  103. fs::inode::calc(fullpath,&st);
  104. d->ino = st.st_ino;
  105. rv = fuse_dirents_add_linux_plus(buf_,d,namelen,&entry,&st);
  106. if(rv)
  107. return close_free_ret_enomem(dirfd,buf);
  108. }
  109. }
  110. fs::close(dirfd);
  111. }
  112. g_DENTS_BUF_POOL.free(buf);
  113. return 0;
  114. }
  115. }
  116. namespace FUSE
  117. {
  118. int
  119. readdir_plus_linux(const Branches::CPtr &branches_,
  120. const char *dirname_,
  121. const uint64_t entry_timeout_,
  122. const uint64_t attr_timeout_,
  123. fuse_dirents_t *buf_)
  124. {
  125. return l::readdir_plus(branches_,dirname_,entry_timeout_,attr_timeout_,buf_);
  126. }
  127. }