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.

366 lines
10 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_clonepath.hpp"
  17. #include "fs_link.hpp"
  18. #include "fs_lstat.hpp"
  19. #include "fs_path.hpp"
  20. #include "fuse_getattr.hpp"
  21. #include "fuse_symlink.hpp"
  22. #include "ghc/filesystem.hpp"
  23. #include "ugid.hpp"
  24. #include "fuse.h"
  25. #include <string>
  26. #include <vector>
  27. using std::string;
  28. using std::vector;
  29. namespace gfs = ghc::filesystem;
  30. namespace error
  31. {
  32. static
  33. inline
  34. int
  35. calc(const int rv_,
  36. const int prev_,
  37. const int cur_)
  38. {
  39. if(rv_ == -1)
  40. {
  41. if(prev_ == 0)
  42. return 0;
  43. return cur_;
  44. }
  45. return 0;
  46. }
  47. }
  48. namespace l
  49. {
  50. static
  51. int
  52. link_create_path_loop(const StrVec &oldbasepaths_,
  53. const string &newbasepath_,
  54. const char *oldfusepath_,
  55. const char *newfusepath_,
  56. const string &newfusedirpath_,
  57. struct stat *st_)
  58. {
  59. int rv;
  60. int error;
  61. string oldfullpath;
  62. string newfullpath;
  63. error = -1;
  64. for(auto &oldbasepath : oldbasepaths_)
  65. {
  66. oldfullpath = fs::path::make(oldbasepath,oldfusepath_);
  67. newfullpath = fs::path::make(oldbasepath,newfusepath_);
  68. rv = fs::link(oldfullpath,newfullpath);
  69. if((rv == -1) && (errno == ENOENT))
  70. {
  71. rv = fs::clonepath_as_root(newbasepath_,oldbasepath,newfusedirpath_);
  72. if(rv == 0)
  73. rv = fs::link(oldfullpath,newfullpath);
  74. }
  75. if((rv == 0) && (st_->st_ino == 0))
  76. rv = fs::lstat(oldfullpath,st_);
  77. error = error::calc(rv,error,errno);
  78. }
  79. return -error;
  80. }
  81. static
  82. int
  83. link_create_path(const Policy::Search &searchFunc_,
  84. const Policy::Action &actionFunc_,
  85. const Branches &branches_,
  86. const char *oldfusepath_,
  87. const char *newfusepath_,
  88. struct stat *st_)
  89. {
  90. int rv;
  91. string newfusedirpath;
  92. StrVec oldbasepaths;
  93. StrVec newbasepaths;
  94. rv = actionFunc_(branches_,oldfusepath_,&oldbasepaths);
  95. if(rv == -1)
  96. return -errno;
  97. newfusedirpath = fs::path::dirname(newfusepath_);
  98. rv = searchFunc_(branches_,newfusedirpath,&newbasepaths);
  99. if(rv == -1)
  100. return -errno;
  101. return l::link_create_path_loop(oldbasepaths,newbasepaths[0],
  102. oldfusepath_,newfusepath_,
  103. newfusedirpath,
  104. st_);
  105. }
  106. static
  107. int
  108. link_preserve_path_core(const string &oldbasepath_,
  109. const char *oldfusepath_,
  110. const char *newfusepath_,
  111. struct stat *st_,
  112. const int error_)
  113. {
  114. int rv;
  115. string oldfullpath;
  116. string newfullpath;
  117. oldfullpath = fs::path::make(oldbasepath_,oldfusepath_);
  118. newfullpath = fs::path::make(oldbasepath_,newfusepath_);
  119. rv = fs::link(oldfullpath,newfullpath);
  120. if((rv == -1) && (errno == ENOENT))
  121. errno = EXDEV;
  122. if((rv == 0) && (st_->st_ino == 0))
  123. rv = fs::lstat(oldfullpath,st_);
  124. return error::calc(rv,error_,errno);
  125. }
  126. static
  127. int
  128. link_preserve_path_loop(const StrVec &oldbasepaths_,
  129. const char *oldfusepath_,
  130. const char *newfusepath_,
  131. struct stat *st_)
  132. {
  133. int error;
  134. error = -1;
  135. for(auto &oldbasepath : oldbasepaths_)
  136. {
  137. error = l::link_preserve_path_core(oldbasepath,
  138. oldfusepath_,
  139. newfusepath_,
  140. st_,
  141. error);
  142. }
  143. return -error;
  144. }
  145. static
  146. int
  147. link_preserve_path(const Policy::Action &actionFunc_,
  148. const Branches &branches_,
  149. const char *oldfusepath_,
  150. const char *newfusepath_,
  151. struct stat *st_)
  152. {
  153. int rv;
  154. StrVec oldbasepaths;
  155. rv = actionFunc_(branches_,oldfusepath_,&oldbasepaths);
  156. if(rv == -1)
  157. return -errno;
  158. return l::link_preserve_path_loop(oldbasepaths,
  159. oldfusepath_,
  160. newfusepath_,
  161. st_);
  162. }
  163. static
  164. int
  165. link(Config::Read &cfg_,
  166. const char *oldpath_,
  167. const char *newpath_,
  168. struct stat *st_)
  169. {
  170. if(cfg_->func.create.policy.path_preserving() && !cfg_->ignorepponrename)
  171. return l::link_preserve_path(cfg_->func.link.policy,
  172. cfg_->branches,
  173. oldpath_,
  174. newpath_,
  175. st_);
  176. return l::link_create_path(cfg_->func.getattr.policy,
  177. cfg_->func.link.policy,
  178. cfg_->branches,
  179. oldpath_,
  180. newpath_,
  181. st_);
  182. }
  183. static
  184. int
  185. link(Config::Read &cfg_,
  186. const char *oldpath_,
  187. const char *newpath_,
  188. struct stat *st_,
  189. fuse_timeouts_t *timeouts_)
  190. {
  191. int rv;
  192. rv = l::link(cfg_,oldpath_,newpath_,st_);
  193. timeouts_->entry = ((rv >= 0) ?
  194. cfg_->cache_entry :
  195. cfg_->cache_negative_entry);
  196. timeouts_->attr = cfg_->cache_attr;
  197. return rv;
  198. }
  199. static
  200. int
  201. link_exdev_rel_symlink(const char *oldpath_,
  202. const char *newpath_,
  203. struct stat *st_,
  204. fuse_timeouts_t *timeouts_)
  205. {
  206. int rv;
  207. gfs::path target(oldpath_);
  208. gfs::path linkpath(newpath_);
  209. target = target.lexically_relative(linkpath.parent_path());
  210. rv = FUSE::symlink(target.c_str(),linkpath.c_str());
  211. if(rv == 0)
  212. rv = FUSE::getattr(oldpath_,st_,timeouts_);
  213. // Disable attr caching since we created a symlink but should be a regular.
  214. timeouts_->attr = 0;
  215. return rv;
  216. }
  217. static
  218. int
  219. link_exdev_abs_base_symlink(const Policy::Search &openPolicy_,
  220. const Branches::CPtr &branches_,
  221. const char *oldpath_,
  222. const char *newpath_,
  223. struct stat *st_,
  224. fuse_timeouts_t *timeouts_)
  225. {
  226. int rv;
  227. StrVec basepaths;
  228. std::string target;
  229. rv = openPolicy_(branches_,oldpath_,&basepaths);
  230. if(rv == -1)
  231. return -errno;
  232. target = fs::path::make(basepaths[0],oldpath_);
  233. rv = FUSE::symlink(target.c_str(),newpath_);
  234. if(rv == 0)
  235. rv = FUSE::getattr(oldpath_,st_,timeouts_);
  236. // Disable attr caching since we created a symlink but should be a regular.
  237. timeouts_->attr = 0;
  238. return rv;
  239. }
  240. static
  241. int
  242. link_exdev_abs_pool_symlink(const std::string &mount_,
  243. const char *oldpath_,
  244. const char *newpath_,
  245. struct stat *st_,
  246. fuse_timeouts_t *timeouts_)
  247. {
  248. int rv;
  249. StrVec basepaths;
  250. std::string target;
  251. target = fs::path::make(mount_,oldpath_);
  252. rv = FUSE::symlink(target.c_str(),newpath_);
  253. if(rv == 0)
  254. rv = FUSE::getattr(oldpath_,st_,timeouts_);
  255. // Disable attr caching since we created a symlink but should be a regular.
  256. timeouts_->attr = 0;
  257. return rv;
  258. }
  259. static
  260. int
  261. link_exdev(Config::Read &cfg_,
  262. const char *oldpath_,
  263. const char *newpath_,
  264. struct stat *st_,
  265. fuse_timeouts_t *timeouts_)
  266. {
  267. switch(cfg_->link_exdev)
  268. {
  269. case LinkEXDEV::ENUM::PASSTHROUGH:
  270. return -EXDEV;
  271. case LinkEXDEV::ENUM::REL_SYMLINK:
  272. return l::link_exdev_rel_symlink(oldpath_,
  273. newpath_,
  274. st_,
  275. timeouts_);
  276. case LinkEXDEV::ENUM::ABS_BASE_SYMLINK:
  277. return l::link_exdev_abs_base_symlink(cfg_->func.open.policy,
  278. cfg_->branches,
  279. oldpath_,
  280. newpath_,
  281. st_,
  282. timeouts_);
  283. case LinkEXDEV::ENUM::ABS_POOL_SYMLINK:
  284. return l::link_exdev_abs_pool_symlink(cfg_->mount,
  285. oldpath_,
  286. newpath_,
  287. st_,
  288. timeouts_);
  289. }
  290. return -EXDEV;
  291. }
  292. }
  293. namespace FUSE
  294. {
  295. int
  296. link(const char *oldpath_,
  297. const char *newpath_,
  298. struct stat *st_,
  299. fuse_timeouts_t *timeouts_)
  300. {
  301. int rv;
  302. Config::Read cfg;
  303. const fuse_context *fc = fuse_get_context();
  304. const ugid::Set ugid(fc->uid,fc->gid);
  305. rv = l::link(cfg,oldpath_,newpath_,st_,timeouts_);
  306. if(rv == -EXDEV)
  307. return l::link_exdev(cfg,oldpath_,newpath_,st_,timeouts_);
  308. return rv;
  309. }
  310. }