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.

303 lines
8.7 KiB

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