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.

124 lines
3.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. #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_inode.hpp"
  21. #include "fs_opendir.hpp"
  22. #include "fs_path.hpp"
  23. #include "fs_readdir.hpp"
  24. #include "fs_stat.hpp"
  25. #include "hashset.hpp"
  26. #include "fuse.h"
  27. #include "fuse_dirents.h"
  28. #include <string>
  29. #include <vector>
  30. #include <dirent.h>
  31. using std::string;
  32. using std::vector;
  33. namespace l
  34. {
  35. static
  36. uint64_t
  37. dirent_exact_namelen(const struct dirent *d_)
  38. {
  39. #ifdef _D_EXACT_NAMLEN
  40. return _D_EXACT_NAMLEN(d_);
  41. #elif defined _DIRENT_HAVE_D_NAMLEN
  42. return d_->d_namlen;
  43. #else
  44. return strlen(d_->d_name);
  45. #endif
  46. }
  47. static
  48. int
  49. readdir(const Branches::CPtr &branches_,
  50. const char *dirname_,
  51. fuse_dirents_t *buf_)
  52. {
  53. dev_t dev;
  54. HashSet names;
  55. string basepath;
  56. string fullpath;
  57. uint64_t namelen;
  58. fuse_dirents_reset(buf_);
  59. for(const auto &branch : *branches_)
  60. {
  61. int rv;
  62. int dirfd;
  63. DIR *dh;
  64. basepath = fs::path::make(branch.path,dirname_);
  65. dh = fs::opendir(basepath);
  66. if(!dh)
  67. continue;
  68. dirfd = fs::dirfd(dh);
  69. dev = fs::devid(dirfd);
  70. rv = 0;
  71. for(struct dirent *de = fs::readdir(dh); de && !rv; de = fs::readdir(dh))
  72. {
  73. namelen = l::dirent_exact_namelen(de);
  74. rv = names.put(de->d_name,namelen);
  75. if(rv == 0)
  76. continue;
  77. fullpath = fs::path::make(dirname_,de->d_name);
  78. de->d_ino = fs::inode::calc(fullpath.c_str(),
  79. fullpath.size(),
  80. DTTOIF(de->d_type),
  81. dev,
  82. de->d_ino);
  83. rv = fuse_dirents_add(buf_,de,namelen);
  84. if(rv)
  85. return (fs::closedir(dh),-ENOMEM);
  86. }
  87. fs::closedir(dh);
  88. }
  89. return 0;
  90. }
  91. }
  92. namespace FUSE
  93. {
  94. int
  95. readdir_posix(const Branches::CPtr &branches_,
  96. const char *dirname_,
  97. fuse_dirents_t *buf_)
  98. {
  99. return l::readdir(branches_,dirname_,buf_);
  100. }
  101. }