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.

225 lines
5.3 KiB

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