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.

188 lines
4.5 KiB

  1. /*
  2. Copyright (c) 2016, 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 "config.hpp"
  15. #include "errno.hpp"
  16. #include "fs_inode.hpp"
  17. #include "fs_lstat.hpp"
  18. #include "fs_path.hpp"
  19. #include "fs_stat.hpp"
  20. #include "symlinkify.hpp"
  21. #include "ugid.hpp"
  22. #include "fuse.h"
  23. #include <string>
  24. using std::string;
  25. namespace l
  26. {
  27. static
  28. void
  29. set_stat_if_leads_to_dir(const std::string &path_,
  30. struct stat *st_)
  31. {
  32. int rv;
  33. struct stat st;
  34. rv = fs::stat(path_,&st);
  35. if(rv == -1)
  36. return;
  37. if(S_ISDIR(st.st_mode))
  38. *st_ = st;
  39. return;
  40. }
  41. static
  42. void
  43. set_stat_if_leads_to_reg(const std::string &path_,
  44. struct stat *st_)
  45. {
  46. int rv;
  47. struct stat st;
  48. rv = fs::stat(path_,&st);
  49. if(rv == -1)
  50. return;
  51. if(S_ISREG(st.st_mode))
  52. *st_ = st;
  53. return;
  54. }
  55. static
  56. int
  57. getattr_controlfile(struct stat *st_)
  58. {
  59. static const uid_t uid = ::getuid();
  60. static const gid_t gid = ::getgid();
  61. static const time_t now = ::time(NULL);
  62. st_->st_dev = 0;
  63. st_->st_ino = fs::inode::MAGIC;
  64. st_->st_mode = (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
  65. st_->st_nlink = 1;
  66. st_->st_uid = uid;
  67. st_->st_gid = gid;
  68. st_->st_rdev = 0;
  69. st_->st_size = 0;
  70. st_->st_blksize = 512;
  71. st_->st_blocks = 0;
  72. st_->st_atime = now;
  73. st_->st_mtime = now;
  74. st_->st_ctime = now;
  75. return 0;
  76. }
  77. static
  78. int
  79. getattr(const Policy::Search &searchFunc_,
  80. const Branches &branches_,
  81. const char *fusepath_,
  82. struct stat *st_,
  83. const bool symlinkify_,
  84. const time_t symlinkify_timeout_,
  85. FollowSymlinks followsymlinks_)
  86. {
  87. int rv;
  88. string fullpath;
  89. StrVec basepaths;
  90. rv = searchFunc_(branches_,fusepath_,&basepaths);
  91. if(rv == -1)
  92. return -errno;
  93. fullpath = fs::path::make(basepaths[0],fusepath_);
  94. switch(followsymlinks_)
  95. {
  96. case FollowSymlinks::ENUM::NEVER:
  97. rv = fs::lstat(fullpath,st_);
  98. break;
  99. case FollowSymlinks::ENUM::DIRECTORY:
  100. rv = fs::lstat(fullpath,st_);
  101. if(S_ISLNK(st_->st_mode))
  102. l::set_stat_if_leads_to_dir(fullpath,st_);
  103. break;
  104. case FollowSymlinks::ENUM::REGULAR:
  105. rv = fs::lstat(fullpath,st_);
  106. if(S_ISLNK(st_->st_mode))
  107. l::set_stat_if_leads_to_reg(fullpath,st_);
  108. break;
  109. case FollowSymlinks::ENUM::ALL:
  110. rv = fs::stat(fullpath,st_);
  111. if(rv != 0)
  112. rv = fs::lstat(fullpath,st_);
  113. break;
  114. }
  115. if(rv == -1)
  116. return -errno;
  117. if(symlinkify_ && symlinkify::can_be_symlink(*st_,symlinkify_timeout_))
  118. symlinkify::convert(fullpath,st_);
  119. fs::inode::calc(fusepath_,st_);
  120. return 0;
  121. }
  122. int
  123. getattr(const char *fusepath_,
  124. struct stat *st_,
  125. fuse_timeouts_t *timeout_)
  126. {
  127. int rv;
  128. Config::Read cfg;
  129. const fuse_context *fc = fuse_get_context();
  130. const ugid::Set ugid(fc->uid,fc->gid);
  131. rv = l::getattr(cfg->func.getattr.policy,
  132. cfg->branches,
  133. fusepath_,
  134. st_,
  135. cfg->symlinkify,
  136. cfg->symlinkify_timeout,
  137. cfg->follow_symlinks);
  138. timeout_->entry = ((rv >= 0) ?
  139. cfg->cache_entry :
  140. cfg->cache_negative_entry);
  141. timeout_->attr = cfg->cache_attr;
  142. return rv;
  143. }
  144. }
  145. namespace FUSE
  146. {
  147. int
  148. getattr(const char *fusepath_,
  149. struct stat *st_,
  150. fuse_timeouts_t *timeout_)
  151. {
  152. if(fusepath_ == CONTROLFILE)
  153. return l::getattr_controlfile(st_);
  154. return l::getattr(fusepath_,st_,timeout_);
  155. }
  156. }