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.

337 lines
9.0 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <fuse.h>
  21. #include <string>
  22. #include <vector>
  23. #include <sstream>
  24. #include <errno.h>
  25. #include <sys/types.h>
  26. #include <string.h>
  27. #include "config.hpp"
  28. #include "fs.hpp"
  29. #include "ugid.hpp"
  30. #include "rwlock.hpp"
  31. #include "xattr.hpp"
  32. #include "str.hpp"
  33. #include "num.hpp"
  34. using std::string;
  35. using std::vector;
  36. using mergerfs::Policy;
  37. using mergerfs::FuseFunc;
  38. using namespace mergerfs;
  39. static
  40. int
  41. _add_srcmounts(vector<string> &srcmounts,
  42. pthread_rwlock_t &srcmountslock,
  43. const string &destmount,
  44. const string &values,
  45. vector<string>::iterator pos)
  46. {
  47. vector<string> patterns;
  48. vector<string> additions;
  49. str::split(patterns,values,':');
  50. fs::glob(patterns,additions);
  51. if(!additions.empty())
  52. {
  53. const rwlock::WriteGuard wrg(&srcmountslock);
  54. srcmounts.insert(pos,
  55. additions.begin(),
  56. additions.end());
  57. }
  58. return 0;
  59. }
  60. static
  61. int
  62. _erase_srcmounts(vector<string> &srcmounts,
  63. pthread_rwlock_t &srcmountslock,
  64. const string &values)
  65. {
  66. if(srcmounts.empty())
  67. return 0;
  68. vector<string> patterns;
  69. str::split(patterns,values,':');
  70. {
  71. const rwlock::WriteGuard wrg(&srcmountslock);
  72. str::erase_fnmatches(patterns,srcmounts);
  73. }
  74. return 0;
  75. }
  76. static
  77. int
  78. _replace_srcmounts(vector<string> &srcmounts,
  79. pthread_rwlock_t &srcmountslock,
  80. const string &destmount,
  81. const string &values)
  82. {
  83. vector<string> patterns;
  84. vector<string> newmounts;
  85. str::split(patterns,values,':');
  86. fs::glob(patterns,newmounts);
  87. {
  88. const rwlock::WriteGuard wrg(&srcmountslock);
  89. srcmounts.swap(newmounts);
  90. }
  91. return 0;
  92. }
  93. static
  94. void
  95. _split_attrval(const string &attrval,
  96. string &instruction,
  97. string &values)
  98. {
  99. size_t offset;
  100. offset = attrval.find_first_of('/');
  101. instruction = attrval.substr(0,offset);
  102. if(offset != string::npos)
  103. values = attrval.substr(offset);
  104. }
  105. static
  106. int
  107. _setxattr_srcmounts(vector<string> &srcmounts,
  108. pthread_rwlock_t &srcmountslock,
  109. const string &destmount,
  110. const string &attrval,
  111. const int flags)
  112. {
  113. string instruction;
  114. string values;
  115. if((flags & XATTR_CREATE) == XATTR_CREATE)
  116. return -EEXIST;
  117. _split_attrval(attrval,instruction,values);
  118. if(instruction == "+")
  119. return _add_srcmounts(srcmounts,srcmountslock,destmount,values,srcmounts.end());
  120. else if(instruction == "+<")
  121. return _add_srcmounts(srcmounts,srcmountslock,destmount,values,srcmounts.begin());
  122. else if(instruction == "+>")
  123. return _add_srcmounts(srcmounts,srcmountslock,destmount,values,srcmounts.end());
  124. else if(instruction == "-")
  125. return _erase_srcmounts(srcmounts,srcmountslock,values);
  126. else if(instruction == "-<")
  127. return _erase_srcmounts(srcmounts,srcmountslock,srcmounts.front());
  128. else if(instruction == "->")
  129. return _erase_srcmounts(srcmounts,srcmountslock,srcmounts.back());
  130. else if(instruction == "=")
  131. return _replace_srcmounts(srcmounts,srcmountslock,destmount,values);
  132. else if(instruction.empty())
  133. return _replace_srcmounts(srcmounts,srcmountslock,destmount,values);
  134. return -EINVAL;
  135. }
  136. static
  137. int
  138. _setxattr_minfreespace(Config &config,
  139. const string &attrval,
  140. const int flags)
  141. {
  142. int rv;
  143. if((flags & XATTR_CREATE) == XATTR_CREATE)
  144. return -EEXIST;
  145. rv = num::to_size_t(attrval,config.minfreespace);
  146. if(rv == -1)
  147. return -EINVAL;
  148. return 0;
  149. }
  150. static
  151. int
  152. _setxattr_controlfile_func_policy(Config &config,
  153. const char *funcname,
  154. const string &attrval,
  155. const int flags)
  156. {
  157. int rv;
  158. if((flags & XATTR_CREATE) == XATTR_CREATE)
  159. return -EEXIST;
  160. rv = config.set_func_policy(funcname,attrval);
  161. if(rv == -1)
  162. return -errno;
  163. return 0;
  164. }
  165. static
  166. int
  167. _setxattr_controlfile_category_policy(Config &config,
  168. const char *categoryname,
  169. const string &attrval,
  170. const int flags)
  171. {
  172. int rv;
  173. if((flags & XATTR_CREATE) == XATTR_CREATE)
  174. return -EEXIST;
  175. rv = config.set_category_policy(categoryname,attrval);
  176. if(rv == -1)
  177. return -errno;
  178. return 0;
  179. }
  180. static
  181. int
  182. _setxattr_controlfile(Config &config,
  183. const char *attrname,
  184. const string &attrval,
  185. const int flags)
  186. {
  187. const char *attrbasename = &attrname[sizeof("user.mergerfs.")-1];
  188. if(strncmp("user.mergerfs.",attrname,sizeof("user.mergerfs.")-1))
  189. return -ENOATTR;
  190. if(attrval.empty())
  191. return -EINVAL;
  192. if(!strcmp("srcmounts",attrbasename))
  193. return _setxattr_srcmounts(config.srcmounts,
  194. config.srcmountslock,
  195. config.destmount,
  196. attrval,
  197. flags);
  198. else if(!strcmp("minfreespace",attrbasename))
  199. return _setxattr_minfreespace(config,
  200. attrval,
  201. flags);
  202. else if(!strncmp("category.",attrbasename,sizeof("category.")-1))
  203. return _setxattr_controlfile_category_policy(config,
  204. &attrbasename[sizeof("category.")-1],
  205. attrval,
  206. flags);
  207. else if(!strncmp("func.",attrbasename,sizeof("func.")-1))
  208. return _setxattr_controlfile_func_policy(config,
  209. &attrbasename[sizeof("func.")-1],
  210. attrval,
  211. flags);
  212. return -EINVAL;
  213. }
  214. static
  215. int
  216. _setxattr(Policy::Func::Action actionFunc,
  217. const vector<string> &srcmounts,
  218. const size_t minfreespace,
  219. const string &fusepath,
  220. const char *attrname,
  221. const char *attrval,
  222. const size_t attrvalsize,
  223. const int flags)
  224. {
  225. #ifndef WITHOUT_XATTR
  226. int rv;
  227. int error;
  228. vector<string> paths;
  229. rv = actionFunc(srcmounts,fusepath,minfreespace,paths);
  230. if(rv == -1)
  231. return -errno;
  232. error = 0;
  233. for(size_t i = 0, ei = paths.size(); i != ei; i++)
  234. {
  235. fs::path::append(paths[i],fusepath);
  236. rv = ::lsetxattr(paths[i].c_str(),attrname,attrval,attrvalsize,flags);
  237. if(rv == -1)
  238. error = errno;
  239. }
  240. return -error;
  241. #else
  242. return -ENOTSUP;
  243. #endif
  244. }
  245. namespace mergerfs
  246. {
  247. namespace fuse
  248. {
  249. int
  250. setxattr(const char *fusepath,
  251. const char *attrname,
  252. const char *attrval,
  253. size_t attrvalsize,
  254. int flags)
  255. {
  256. const fuse_context *fc = fuse_get_context();
  257. const Config &config = Config::get(fc);
  258. if(fusepath == config.controlfile)
  259. {
  260. if((fc->uid != ::getuid()) && (fc->gid != ::getgid()))
  261. return -EPERM;
  262. return _setxattr_controlfile(Config::get_writable(),
  263. attrname,
  264. string(attrval,attrvalsize),
  265. flags);
  266. }
  267. const ugid::SetResetGuard ugid(fc->uid,fc->gid);
  268. const rwlock::ReadGuard readlock(&config.srcmountslock);
  269. return _setxattr(config.setxattr,
  270. config.srcmounts,
  271. config.minfreespace,
  272. fusepath,
  273. attrname,
  274. attrval,
  275. attrvalsize,
  276. flags);
  277. }
  278. }
  279. }