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.

305 lines
8.9 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 <algorithm>
  15. #include <set>
  16. #include <string>
  17. #include <vector>
  18. #include "config.hpp"
  19. #include "errno.hpp"
  20. #include "fs_base_rename.hpp"
  21. #include "fs_clonepath.hpp"
  22. #include "fs_path.hpp"
  23. #include "rv.hpp"
  24. #include "rwlock.hpp"
  25. #include "success_fail.hpp"
  26. #include "ugid.hpp"
  27. using std::string;
  28. using std::vector;
  29. using std::set;
  30. using mergerfs::Policy;
  31. using namespace mergerfs;
  32. static
  33. bool
  34. member(const vector<const string*> &haystack,
  35. const string &needle)
  36. {
  37. for(size_t i = 0, ei = haystack.size(); i != ei; i++)
  38. {
  39. if(*haystack[i] == needle)
  40. return true;
  41. }
  42. return false;
  43. }
  44. static
  45. void
  46. _remove(const vector<string> &toremove)
  47. {
  48. for(size_t i = 0, ei = toremove.size(); i != ei; i++)
  49. ::remove(toremove[i].c_str());
  50. }
  51. static
  52. void
  53. _rename_create_path_core(const vector<const string*> &oldbasepaths,
  54. const string &oldbasepath,
  55. const string &newbasepath,
  56. const char *oldfusepath,
  57. const char *newfusepath,
  58. const string &newfusedirpath,
  59. int &error,
  60. vector<string> &tounlink)
  61. {
  62. int rv;
  63. bool ismember;
  64. string oldfullpath;
  65. string newfullpath;
  66. fs::path::make(&oldbasepath,newfusepath,newfullpath);
  67. ismember = member(oldbasepaths,oldbasepath);
  68. if(ismember)
  69. {
  70. if(oldbasepath != newbasepath)
  71. {
  72. const ugid::SetRootGuard ugidGuard;
  73. fs::clonepath(newbasepath,oldbasepath,newfusedirpath.c_str());
  74. }
  75. fs::path::make(&oldbasepath,oldfusepath,oldfullpath);
  76. rv = fs::rename(oldfullpath,newfullpath);
  77. error = calc_error(rv,error,errno);
  78. if(RENAME_FAILED(rv))
  79. tounlink.push_back(oldfullpath);
  80. }
  81. else
  82. {
  83. tounlink.push_back(newfullpath);
  84. }
  85. }
  86. static
  87. int
  88. _rename_create_path(Policy::Func::Search searchFunc,
  89. Policy::Func::Action actionFunc,
  90. const vector<string> &srcmounts,
  91. const uint64_t minfreespace,
  92. const char *oldfusepath,
  93. const char *newfusepath)
  94. {
  95. int rv;
  96. int error;
  97. vector<string> toremove;
  98. vector<const string*> newbasepath;
  99. vector<const string*> oldbasepaths;
  100. rv = actionFunc(srcmounts,oldfusepath,minfreespace,oldbasepaths);
  101. if(POLICY_FAILED(rv))
  102. return -errno;
  103. string newfusedirpath = newfusepath;
  104. fs::path::dirname(newfusedirpath);
  105. rv = searchFunc(srcmounts,newfusedirpath.c_str(),minfreespace,newbasepath);
  106. if(POLICY_FAILED(rv))
  107. return -errno;
  108. error = RENAME_FAIL;
  109. for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
  110. {
  111. const string &oldbasepath = srcmounts[i];
  112. _rename_create_path_core(oldbasepaths,
  113. oldbasepath,*newbasepath[0],
  114. oldfusepath,newfusepath,
  115. newfusedirpath,
  116. error,toremove);
  117. }
  118. if(RENAME_SUCCEEDED(error))
  119. _remove(toremove);
  120. return -error;
  121. }
  122. static
  123. int
  124. _clonepath(Policy::Func::Search searchFunc,
  125. const vector<string> &srcmounts,
  126. const uint64_t minfreespace,
  127. const string &dstbasepath,
  128. const string &fusedirpath)
  129. {
  130. int rv;
  131. vector<const string*> srcbasepath;
  132. rv = searchFunc(srcmounts,fusedirpath.c_str(),minfreespace,srcbasepath);
  133. if(POLICY_FAILED(rv))
  134. return rv;
  135. {
  136. const ugid::SetRootGuard ugidGuard;
  137. fs::clonepath(*srcbasepath[0],dstbasepath,fusedirpath.c_str());
  138. }
  139. return POLICY_SUCCESS;
  140. }
  141. static
  142. int
  143. _clonepath_if_would_create(Policy::Func::Search searchFunc,
  144. Policy::Func::Create createFunc,
  145. const vector<string> &srcmounts,
  146. const uint64_t minfreespace,
  147. const string &oldbasepath,
  148. const char *oldfusepath,
  149. const char *newfusepath)
  150. {
  151. int rv;
  152. string newfusedirpath;
  153. vector<const string*> newbasepath;
  154. newfusedirpath = newfusepath;
  155. fs::path::dirname(newfusedirpath);
  156. rv = createFunc(srcmounts,newfusedirpath.c_str(),minfreespace,newbasepath);
  157. if(POLICY_FAILED(rv))
  158. return rv;
  159. if(oldbasepath == *newbasepath[0])
  160. return _clonepath(searchFunc,srcmounts,minfreespace,oldbasepath,newfusedirpath);
  161. return POLICY_FAIL_ERRNO(EXDEV);
  162. }
  163. static
  164. void
  165. _rename_preserve_path_core(Policy::Func::Search searchFunc,
  166. Policy::Func::Create createFunc,
  167. const vector<string> &srcmounts,
  168. const uint64_t minfreespace,
  169. const vector<const string*> &oldbasepaths,
  170. const string &oldbasepath,
  171. const char *oldfusepath,
  172. const char *newfusepath,
  173. int &error,
  174. vector<string> &toremove)
  175. {
  176. int rv;
  177. bool ismember;
  178. string newfullpath;
  179. fs::path::make(&oldbasepath,newfusepath,newfullpath);
  180. ismember = member(oldbasepaths,oldbasepath);
  181. if(ismember)
  182. {
  183. string oldfullpath;
  184. fs::path::make(&oldbasepath,oldfusepath,oldfullpath);
  185. rv = fs::rename(oldfullpath,newfullpath);
  186. if(RENAME_FAILED_WITH(rv,ENOENT))
  187. {
  188. rv = _clonepath_if_would_create(searchFunc,createFunc,
  189. srcmounts,minfreespace,
  190. oldbasepath,oldfusepath,newfusepath);
  191. if(POLICY_SUCCEEDED(rv))
  192. rv = fs::rename(oldfullpath,newfullpath);
  193. }
  194. error = calc_error(rv,error,errno);
  195. if(RENAME_FAILED(rv))
  196. toremove.push_back(oldfullpath);
  197. }
  198. else
  199. {
  200. toremove.push_back(newfullpath);
  201. }
  202. }
  203. static
  204. int
  205. _rename_preserve_path(Policy::Func::Search searchFunc,
  206. Policy::Func::Action actionFunc,
  207. Policy::Func::Create createFunc,
  208. const vector<string> &srcmounts,
  209. const uint64_t minfreespace,
  210. const char *oldfusepath,
  211. const char *newfusepath)
  212. {
  213. int rv;
  214. int error;
  215. vector<string> toremove;
  216. vector<const string*> oldbasepaths;
  217. rv = actionFunc(srcmounts,oldfusepath,minfreespace,oldbasepaths);
  218. if(POLICY_FAILED(rv))
  219. return -errno;
  220. error = RENAME_FAIL;
  221. for(size_t i = 0, ei = srcmounts.size(); i != ei; i++)
  222. {
  223. const string &oldbasepath = srcmounts[i];
  224. _rename_preserve_path_core(searchFunc,createFunc,
  225. srcmounts,minfreespace,
  226. oldbasepaths,oldbasepath,
  227. oldfusepath,newfusepath,
  228. error,toremove);
  229. }
  230. if(RENAME_SUCCEEDED(error))
  231. _remove(toremove);
  232. return -error;
  233. }
  234. namespace mergerfs
  235. {
  236. namespace fuse
  237. {
  238. int
  239. rename(const char *oldpath,
  240. const char *newpath)
  241. {
  242. const fuse_context *fc = fuse_get_context();
  243. const Config &config = Config::get(fc);
  244. const ugid::Set ugid(fc->uid,fc->gid);
  245. const rwlock::ReadGuard readlock(&config.srcmountslock);
  246. if(config.create->path_preserving())
  247. return _rename_preserve_path(config.getattr,
  248. config.rename,
  249. config.create,
  250. config.srcmounts,
  251. config.minfreespace,
  252. oldpath,
  253. newpath);
  254. return _rename_create_path(config.getattr,
  255. config.rename,
  256. config.srcmounts,
  257. config.minfreespace,
  258. oldpath,
  259. newpath);
  260. }
  261. }
  262. }