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.

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