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.

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