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.

408 lines
10 KiB

6 years ago
6 years 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_clonepath.hpp"
  17. #include "fs_link.hpp"
  18. #include "fs_mkdir_as_root.hpp"
  19. #include "fs_path.hpp"
  20. #include "fs_remove.hpp"
  21. #include "fs_rename.hpp"
  22. #include "fs_symlink.hpp"
  23. #include "fs_unlink.hpp"
  24. #include "fuse_symlink.hpp"
  25. #include "ugid.hpp"
  26. #include "ghc/filesystem.hpp"
  27. #include <algorithm>
  28. #include <string>
  29. #include <vector>
  30. #include <iostream>
  31. using std::string;
  32. namespace gfs = ghc::filesystem;
  33. namespace error
  34. {
  35. static
  36. inline
  37. int
  38. calc(const int rv_,
  39. const int prev_,
  40. const int cur_)
  41. {
  42. if(rv_ == -1)
  43. {
  44. if(prev_ == 0)
  45. return 0;
  46. return cur_;
  47. }
  48. return 0;
  49. }
  50. }
  51. namespace l
  52. {
  53. static
  54. bool
  55. contains(const StrVec &haystack_,
  56. const char *needle_)
  57. {
  58. for(auto &hay : haystack_)
  59. {
  60. if(hay == needle_)
  61. return true;
  62. }
  63. return false;
  64. }
  65. static
  66. bool
  67. contains(const StrVec &haystack_,
  68. const string &needle_)
  69. {
  70. return l::contains(haystack_,needle_.c_str());
  71. }
  72. static
  73. void
  74. remove(const StrVec &toremove_)
  75. {
  76. for(auto &path : toremove_)
  77. fs::remove(path);
  78. }
  79. static
  80. void
  81. remove(const Branches::CPtr &branches_,
  82. const std::string &relpath_)
  83. {
  84. std::string fullpath;
  85. for(auto &branch : *branches_)
  86. {
  87. fullpath = fs::path::make(branch.path,relpath_);
  88. fs::remove(fullpath);
  89. }
  90. }
  91. static
  92. int
  93. rename_create_path(const Policy::Search &searchPolicy_,
  94. const Policy::Action &actionPolicy_,
  95. const Branches::CPtr &branches_,
  96. const gfs::path &oldfusepath_,
  97. const gfs::path &newfusepath_)
  98. {
  99. int rv;
  100. int error;
  101. StrVec toremove;
  102. StrVec newbasepath;
  103. StrVec oldbasepaths;
  104. gfs::path oldfullpath;
  105. gfs::path newfullpath;
  106. rv = actionPolicy_(branches_,oldfusepath_,&oldbasepaths);
  107. if(rv == -1)
  108. return -errno;
  109. rv = searchPolicy_(branches_,newfusepath_.parent_path(),&newbasepath);
  110. if(rv == -1)
  111. return -errno;
  112. error = -1;
  113. for(auto &branch : *branches_)
  114. {
  115. newfullpath = branch.path;
  116. newfullpath += newfusepath_;
  117. if(!l::contains(oldbasepaths,branch.path))
  118. {
  119. toremove.push_back(newfullpath);
  120. continue;
  121. }
  122. oldfullpath = branch.path;
  123. oldfullpath += oldfusepath_;
  124. rv = fs::rename(oldfullpath,newfullpath);
  125. if(rv == -1)
  126. {
  127. rv = fs::clonepath_as_root(newbasepath[0],branch.path,newfusepath_.parent_path());
  128. if(rv == 0)
  129. rv = fs::rename(oldfullpath,newfullpath);
  130. }
  131. error = error::calc(rv,error,errno);
  132. if(rv == -1)
  133. toremove.push_back(oldfullpath);
  134. }
  135. if(error == 0)
  136. l::remove(toremove);
  137. return -error;
  138. }
  139. static
  140. int
  141. rename_preserve_path(const Policy::Action &actionPolicy_,
  142. const Branches::CPtr &branches_,
  143. const gfs::path &oldfusepath_,
  144. const gfs::path &newfusepath_)
  145. {
  146. int rv;
  147. bool success;
  148. StrVec toremove;
  149. StrVec oldbasepaths;
  150. gfs::path oldfullpath;
  151. gfs::path newfullpath;
  152. rv = actionPolicy_(branches_,oldfusepath_,&oldbasepaths);
  153. if(rv == -1)
  154. return -errno;
  155. success = false;
  156. for(auto &branch : *branches_)
  157. {
  158. newfullpath = branch.path;
  159. newfullpath += newfusepath_;
  160. if(!l::contains(oldbasepaths,branch.path))
  161. {
  162. toremove.push_back(newfullpath);
  163. continue;
  164. }
  165. oldfullpath = branch.path;
  166. oldfullpath += oldfusepath_;
  167. rv = fs::rename(oldfullpath,newfullpath);
  168. if(rv == -1)
  169. {
  170. toremove.push_back(oldfullpath);
  171. continue;
  172. }
  173. success = true;
  174. }
  175. // TODO: probably should try to be nuanced here.
  176. if(success == false)
  177. return -EXDEV;
  178. l::remove(toremove);
  179. return 0;
  180. }
  181. static
  182. void
  183. rename_exdev_rename_back(const StrVec &basepaths_,
  184. const gfs::path &oldfusepath_)
  185. {
  186. gfs::path oldpath;
  187. gfs::path newpath;
  188. for(auto &basepath : basepaths_)
  189. {
  190. oldpath = basepath;
  191. oldpath /= ".mergerfs_rename_exdev";
  192. oldpath += oldfusepath_;
  193. newpath = basepath;
  194. newpath += oldfusepath_;
  195. fs::rename(oldpath,newpath);
  196. }
  197. }
  198. static
  199. int
  200. rename_exdev_rename_target(const Policy::Action &actionPolicy_,
  201. const Branches::CPtr &branches_,
  202. const gfs::path &oldfusepath_,
  203. StrVec *basepaths_)
  204. {
  205. int rv;
  206. gfs::path clonesrc;
  207. gfs::path clonetgt;
  208. rv = actionPolicy_(branches_,oldfusepath_,basepaths_);
  209. if(rv == -1)
  210. return -errno;
  211. ugid::SetRootGuard ugidGuard;
  212. for(auto &basepath : *basepaths_)
  213. {
  214. clonesrc = basepath;
  215. clonetgt = basepath;
  216. clonetgt /= ".mergerfs_rename_exdev";
  217. rv = fs::clonepath(clonesrc,clonetgt,oldfusepath_.parent_path());
  218. if((rv == -1) && (errno == ENOENT))
  219. {
  220. fs::mkdir(clonetgt,01777);
  221. rv = fs::clonepath(clonesrc,clonetgt,oldfusepath_.parent_path());
  222. }
  223. if(rv == -1)
  224. goto error;
  225. clonesrc += oldfusepath_;
  226. clonetgt += oldfusepath_;
  227. rv = fs::rename(clonesrc,clonetgt);
  228. if(rv == -1)
  229. goto error;
  230. }
  231. return 0;
  232. error:
  233. l::rename_exdev_rename_back(*basepaths_,oldfusepath_);
  234. return -EXDEV;
  235. }
  236. static
  237. int
  238. rename_exdev_rel_symlink(const Policy::Action &actionPolicy_,
  239. const Branches::CPtr &branches_,
  240. const gfs::path &oldfusepath_,
  241. const gfs::path &newfusepath_)
  242. {
  243. int rv;
  244. StrVec basepaths;
  245. gfs::path target;
  246. gfs::path linkpath;
  247. rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,&basepaths);
  248. if(rv < 0)
  249. return rv;
  250. linkpath = newfusepath_;
  251. target = "/.mergerfs_rename_exdev";
  252. target += oldfusepath_;
  253. target = target.lexically_relative(linkpath.parent_path());
  254. rv = FUSE::symlink(target.c_str(),linkpath.c_str());
  255. if(rv < 0)
  256. l::rename_exdev_rename_back(basepaths,oldfusepath_);
  257. return rv;
  258. }
  259. static
  260. int
  261. rename_exdev_abs_symlink(const Policy::Action &actionPolicy_,
  262. const Branches::CPtr &branches_,
  263. const std::string &mount_,
  264. const gfs::path &oldfusepath_,
  265. const gfs::path &newfusepath_)
  266. {
  267. int rv;
  268. StrVec basepaths;
  269. gfs::path target;
  270. gfs::path linkpath;
  271. rv = l::rename_exdev_rename_target(actionPolicy_,branches_,oldfusepath_,&basepaths);
  272. if(rv < 0)
  273. return rv;
  274. linkpath = newfusepath_;
  275. target = mount_;
  276. target /= ".mergerfs_rename_exdev";
  277. target += oldfusepath_;
  278. rv = FUSE::symlink(target.c_str(),linkpath.c_str());
  279. if(rv < 0)
  280. l::rename_exdev_rename_back(basepaths,oldfusepath_);
  281. return rv;
  282. }
  283. static
  284. int
  285. rename_exdev(Config::Read &cfg_,
  286. const gfs::path &oldfusepath_,
  287. const gfs::path &newfusepath_)
  288. {
  289. switch(cfg_->rename_exdev)
  290. {
  291. case RenameEXDEV::ENUM::PASSTHROUGH:
  292. return -EXDEV;
  293. case RenameEXDEV::ENUM::REL_SYMLINK:
  294. return l::rename_exdev_rel_symlink(cfg_->func.rename.policy,
  295. cfg_->branches,
  296. oldfusepath_,
  297. newfusepath_);
  298. case RenameEXDEV::ENUM::ABS_SYMLINK:
  299. return l::rename_exdev_abs_symlink(cfg_->func.rename.policy,
  300. cfg_->branches,
  301. cfg_->mountpoint,
  302. oldfusepath_,
  303. newfusepath_);
  304. }
  305. return -EXDEV;
  306. }
  307. static
  308. int
  309. rename(Config::Read &cfg_,
  310. const gfs::path &oldpath_,
  311. const gfs::path &newpath_)
  312. {
  313. if(cfg_->func.create.policy.path_preserving() && !cfg_->ignorepponrename)
  314. return l::rename_preserve_path(cfg_->func.rename.policy,
  315. cfg_->branches,
  316. oldpath_,
  317. newpath_);
  318. return l::rename_create_path(cfg_->func.getattr.policy,
  319. cfg_->func.rename.policy,
  320. cfg_->branches,
  321. oldpath_,
  322. newpath_);
  323. }
  324. }
  325. namespace FUSE
  326. {
  327. int
  328. rename(const char *oldfusepath_,
  329. const char *newfusepath_)
  330. {
  331. int rv;
  332. Config::Read cfg;
  333. gfs::path oldfusepath(oldfusepath_);
  334. gfs::path newfusepath(newfusepath_);
  335. const fuse_context *fc = fuse_get_context();
  336. const ugid::Set ugid(fc->uid,fc->gid);
  337. rv = l::rename(cfg,oldfusepath,newfusepath);
  338. if(rv == -EXDEV)
  339. return l::rename_exdev(cfg,oldfusepath,newfusepath);
  340. return rv;
  341. }
  342. }