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.

412 lines
10 KiB

  1. /*
  2. Copyright (c) 2018, 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 <fuse.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <algorithm>
  18. #include <set>
  19. #include <sstream>
  20. #include <string>
  21. #include <vector>
  22. #include "config.hpp"
  23. #include "errno.hpp"
  24. #include "fs_base_getxattr.hpp"
  25. #include "fs_path.hpp"
  26. #include "rwlock.hpp"
  27. #include "str.hpp"
  28. #include "ugid.hpp"
  29. #include "version.hpp"
  30. static const char SECURITY_CAPABILITY[] = "security.capability";
  31. using std::string;
  32. using std::vector;
  33. using std::set;
  34. using namespace mergerfs;
  35. static
  36. bool
  37. _is_attrname_security_capability(const char *attrname_)
  38. {
  39. return (strcmp(attrname_,SECURITY_CAPABILITY) == 0);
  40. }
  41. static
  42. int
  43. _lgetxattr(const string &path,
  44. const char *attrname,
  45. void *value,
  46. const size_t size)
  47. {
  48. int rv;
  49. rv = fs::lgetxattr(path,attrname,value,size);
  50. return ((rv == -1) ? -errno : rv);
  51. }
  52. static
  53. void
  54. _getxattr_controlfile_fusefunc_policy(const Config &config,
  55. const string &attr,
  56. string &attrvalue)
  57. {
  58. FuseFunc fusefunc;
  59. fusefunc = FuseFunc::find(attr);
  60. if(fusefunc != FuseFunc::invalid)
  61. attrvalue = (std::string)*config.policies[(FuseFunc::Enum::Type)*fusefunc];
  62. }
  63. static
  64. void
  65. _getxattr_controlfile_category_policy(const Config &config,
  66. const string &attr,
  67. string &attrvalue)
  68. {
  69. Category cat;
  70. cat = Category::find(attr);
  71. if(cat != Category::invalid)
  72. {
  73. vector<string> policies;
  74. for(int i = FuseFunc::Enum::BEGIN; i < FuseFunc::Enum::END; i++)
  75. {
  76. if(cat == (Category::Enum::Type)*FuseFunc::fusefuncs[i])
  77. policies.push_back(*config.policies[i]);
  78. }
  79. std::sort(policies.begin(),policies.end());
  80. policies.erase(std::unique(policies.begin(),policies.end()),
  81. policies.end());
  82. attrvalue = str::join(policies,',');
  83. }
  84. }
  85. static
  86. void
  87. _getxattr_controlfile_srcmounts(const Config &config,
  88. string &attrvalue)
  89. {
  90. attrvalue = config.branches.to_string();
  91. }
  92. static
  93. void
  94. _getxattr_controlfile_branches(const Config &config,
  95. string &attrvalue)
  96. {
  97. attrvalue = config.branches.to_string(true);
  98. }
  99. static
  100. void
  101. _getxattr_controlfile_uint64_t(const uint64_t uint,
  102. string &attrvalue)
  103. {
  104. std::ostringstream os;
  105. os << uint;
  106. attrvalue = os.str();
  107. }
  108. static
  109. void
  110. _getxattr_controlfile_time_t(const time_t time,
  111. string &attrvalue)
  112. {
  113. std::ostringstream os;
  114. os << time;
  115. attrvalue = os.str();
  116. }
  117. static
  118. void
  119. _getxattr_controlfile_bool(const bool boolvalue,
  120. string &attrvalue)
  121. {
  122. attrvalue = (boolvalue ? "true" : "false");
  123. }
  124. static
  125. void
  126. _getxattr_controlfile_errno(const int errno_,
  127. string &attrvalue)
  128. {
  129. switch(errno_)
  130. {
  131. case 0:
  132. attrvalue = "passthrough";
  133. break;
  134. case ENOATTR:
  135. attrvalue = "noattr";
  136. break;
  137. case ENOTSUP:
  138. attrvalue = "notsup";
  139. break;
  140. default:
  141. attrvalue = "ERROR";
  142. break;
  143. }
  144. }
  145. static
  146. void
  147. _getxattr_controlfile_policies(const Config &config,
  148. string &attrvalue)
  149. {
  150. size_t i = Policy::Enum::begin();
  151. attrvalue = (string)Policy::policies[i];
  152. for(i++; i < Policy::Enum::end(); i++)
  153. attrvalue += ',' + (string)Policy::policies[i];
  154. }
  155. static
  156. void
  157. _getxattr_controlfile_version(string &attrvalue)
  158. {
  159. attrvalue = MERGERFS_VERSION;
  160. if(attrvalue.empty())
  161. attrvalue = "unknown_possible_problem_with_build";
  162. }
  163. static
  164. void
  165. _getxattr_pid(string &attrvalue)
  166. {
  167. int pid;
  168. char buf[32];
  169. pid = getpid();
  170. snprintf(buf,sizeof(buf),"%d",pid);
  171. attrvalue = buf;
  172. }
  173. static
  174. int
  175. _getxattr_controlfile(const Config &config,
  176. const char *attrname,
  177. char *buf,
  178. const size_t count)
  179. {
  180. size_t len;
  181. string attrvalue;
  182. vector<string> attr;
  183. str::split(attr,attrname,'.');
  184. if((attr[0] != "user") || (attr[1] != "mergerfs"))
  185. return -ENOATTR;
  186. switch(attr.size())
  187. {
  188. case 3:
  189. if(attr[2] == "srcmounts")
  190. _getxattr_controlfile_srcmounts(config,attrvalue);
  191. else if(attr[2] == "branches")
  192. _getxattr_controlfile_branches(config,attrvalue);
  193. else if(attr[2] == "minfreespace")
  194. _getxattr_controlfile_uint64_t(config.minfreespace,attrvalue);
  195. else if(attr[2] == "moveonenospc")
  196. _getxattr_controlfile_bool(config.moveonenospc,attrvalue);
  197. else if(attr[2] == "dropcacheonclose")
  198. _getxattr_controlfile_bool(config.dropcacheonclose,attrvalue);
  199. else if(attr[2] == "symlinkify")
  200. _getxattr_controlfile_bool(config.symlinkify,attrvalue);
  201. else if(attr[2] == "symlinkify_timeout")
  202. _getxattr_controlfile_time_t(config.symlinkify_timeout,attrvalue);
  203. else if(attr[2] == "nullrw")
  204. _getxattr_controlfile_bool(config.nullrw,attrvalue);
  205. else if(attr[2] == "ignorepponrename")
  206. _getxattr_controlfile_bool(config.ignorepponrename,attrvalue);
  207. else if(attr[2] == "security_capability")
  208. _getxattr_controlfile_bool(config.security_capability,attrvalue);
  209. else if(attr[2] == "xattr")
  210. _getxattr_controlfile_errno(config.xattr,attrvalue);
  211. else if(attr[2] == "link_cow")
  212. _getxattr_controlfile_bool(config.link_cow,attrvalue);
  213. else if(attr[2] == "policies")
  214. _getxattr_controlfile_policies(config,attrvalue);
  215. else if(attr[2] == "version")
  216. _getxattr_controlfile_version(attrvalue);
  217. else if(attr[2] == "pid")
  218. _getxattr_pid(attrvalue);
  219. break;
  220. case 4:
  221. if(attr[2] == "category")
  222. _getxattr_controlfile_category_policy(config,attr[3],attrvalue);
  223. else if(attr[2] == "func")
  224. _getxattr_controlfile_fusefunc_policy(config,attr[3],attrvalue);
  225. break;
  226. }
  227. if(attrvalue.empty())
  228. return -ENOATTR;
  229. len = attrvalue.size();
  230. if(count == 0)
  231. return len;
  232. if(count < len)
  233. return -ERANGE;
  234. memcpy(buf,attrvalue.c_str(),len);
  235. return (int)len;
  236. }
  237. static
  238. int
  239. _getxattr_from_string(char *destbuf,
  240. const size_t destbufsize,
  241. const string &src)
  242. {
  243. const size_t srcbufsize = src.size();
  244. if(destbufsize == 0)
  245. return srcbufsize;
  246. if(srcbufsize > destbufsize)
  247. return -ERANGE;
  248. memcpy(destbuf,src.data(),srcbufsize);
  249. return srcbufsize;
  250. }
  251. static
  252. int
  253. _getxattr_user_mergerfs_allpaths(const Branches &branches_,
  254. const char *fusepath,
  255. char *buf,
  256. const size_t count)
  257. {
  258. string concated;
  259. vector<string> paths;
  260. vector<string> branches;
  261. branches_.to_paths(branches);
  262. fs::findallfiles(branches,fusepath,paths);
  263. concated = str::join(paths,'\0');
  264. return _getxattr_from_string(buf,count,concated);
  265. }
  266. static
  267. int
  268. _getxattr_user_mergerfs(const string &basepath,
  269. const char *fusepath,
  270. const string &fullpath,
  271. const Branches &branches_,
  272. const char *attrname,
  273. char *buf,
  274. const size_t count)
  275. {
  276. vector<string> attr;
  277. str::split(attr,attrname,'.');
  278. if(attr[2] == "basepath")
  279. return _getxattr_from_string(buf,count,basepath);
  280. else if(attr[2] == "relpath")
  281. return _getxattr_from_string(buf,count,fusepath);
  282. else if(attr[2] == "fullpath")
  283. return _getxattr_from_string(buf,count,fullpath);
  284. else if(attr[2] == "allpaths")
  285. return _getxattr_user_mergerfs_allpaths(branches_,fusepath,buf,count);
  286. return -ENOATTR;
  287. }
  288. static
  289. int
  290. _getxattr(Policy::Func::Search searchFunc,
  291. const Branches &branches_,
  292. const size_t minfreespace,
  293. const char *fusepath,
  294. const char *attrname,
  295. char *buf,
  296. const size_t count)
  297. {
  298. int rv;
  299. string fullpath;
  300. vector<const string*> basepaths;
  301. rv = searchFunc(branches_,fusepath,minfreespace,basepaths);
  302. if(rv == -1)
  303. return -errno;
  304. fs::path::make(basepaths[0],fusepath,fullpath);
  305. if(str::isprefix(attrname,"user.mergerfs."))
  306. rv = _getxattr_user_mergerfs(*basepaths[0],fusepath,fullpath,branches_,attrname,buf,count);
  307. else
  308. rv = _lgetxattr(fullpath,attrname,buf,count);
  309. return rv;
  310. }
  311. namespace mergerfs
  312. {
  313. namespace fuse
  314. {
  315. int
  316. getxattr(const char *fusepath,
  317. const char *attrname,
  318. char *buf,
  319. size_t count)
  320. {
  321. const fuse_context *fc = fuse_get_context();
  322. const Config &config = Config::get(fc);
  323. if(fusepath == config.controlfile)
  324. return _getxattr_controlfile(config,
  325. attrname,
  326. buf,
  327. count);
  328. if((config.security_capability == false) &&
  329. _is_attrname_security_capability(attrname))
  330. return -ENOATTR;
  331. if(config.xattr)
  332. return -config.xattr;
  333. const ugid::Set ugid(fc->uid,fc->gid);
  334. const rwlock::ReadGuard readlock(&config.branches_lock);
  335. return _getxattr(config.getxattr,
  336. config.branches,
  337. config.minfreespace,
  338. fusepath,
  339. attrname,
  340. buf,
  341. count);
  342. }
  343. }
  344. }