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.

401 lines
9.3 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 "config.hpp"
  15. #include "ef.hpp"
  16. #include "errno.hpp"
  17. #include "from_string.hpp"
  18. #include "num.hpp"
  19. #include "rwlock.hpp"
  20. #include "str.hpp"
  21. #include "to_string.hpp"
  22. #include "version.hpp"
  23. #include <algorithm>
  24. #include <cstdint>
  25. #include <fstream>
  26. #include <iostream>
  27. #include <string>
  28. #include <pthread.h>
  29. #include <string.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #define MINFREESPACE_DEFAULT (4294967295ULL)
  33. using std::string;
  34. #define IFERT(S) if(S == s_) return true
  35. const std::string CONTROLFILE = "/.mergerfs";
  36. Config Config::_singleton;
  37. namespace l
  38. {
  39. static
  40. bool
  41. readonly(const std::string &s_)
  42. {
  43. IFERT("async_read");
  44. IFERT("cache.symlinks");
  45. IFERT("cache.writeback");
  46. IFERT("fsname");
  47. IFERT("fuse_msg_size");
  48. IFERT("mount");
  49. IFERT("nullrw");
  50. IFERT("pid");
  51. IFERT("readdirplus");
  52. IFERT("threads");
  53. IFERT("version");
  54. return false;
  55. }
  56. }
  57. Config::Config()
  58. : async_read(true),
  59. auto_cache(false),
  60. minfreespace(MINFREESPACE_DEFAULT),
  61. branches(minfreespace),
  62. cache_attr(1),
  63. cache_entry(1),
  64. cache_files(CacheFiles::ENUM::LIBFUSE),
  65. cache_negative_entry(0),
  66. cache_readdir(false),
  67. cache_statfs(0),
  68. cache_symlinks(false),
  69. category(func),
  70. direct_io(false),
  71. dropcacheonclose(false),
  72. fsname(),
  73. follow_symlinks(FollowSymlinks::ENUM::NEVER),
  74. func(),
  75. fuse_msg_size(FUSE_MAX_MAX_PAGES),
  76. ignorepponrename(false),
  77. inodecalc("hybrid-hash"),
  78. link_cow(false),
  79. link_exdev(LinkEXDEV::ENUM::PASSTHROUGH),
  80. log_metrics(false),
  81. mount(),
  82. moveonenospc(false),
  83. nfsopenhack(NFSOpenHack::ENUM::OFF),
  84. nullrw(false),
  85. pid(::getpid()),
  86. posix_acl(false),
  87. readdir(ReadDir::ENUM::POSIX),
  88. readdirplus(false),
  89. rename_exdev(RenameEXDEV::ENUM::PASSTHROUGH),
  90. security_capability(true),
  91. srcmounts(branches),
  92. statfs(StatFS::ENUM::BASE),
  93. statfs_ignore(StatFSIgnore::ENUM::NONE),
  94. symlinkify(false),
  95. symlinkify_timeout(3600),
  96. threads(0),
  97. version(MERGERFS_VERSION),
  98. writeback_cache(false),
  99. xattr(XAttr::ENUM::PASSTHROUGH)
  100. {
  101. _map["async_read"] = &async_read;
  102. _map["auto_cache"] = &auto_cache;
  103. _map["branches"] = &branches;
  104. _map["cache.attr"] = &cache_attr;
  105. _map["cache.entry"] = &cache_entry;
  106. _map["cache.files"] = &cache_files;
  107. _map["cache.negative_entry"] = &cache_negative_entry;
  108. _map["cache.readdir"] = &cache_readdir;
  109. _map["cache.statfs"] = &cache_statfs;
  110. _map["cache.symlinks"] = &cache_symlinks;
  111. _map["cache.writeback"] = &writeback_cache;
  112. _map["category.action"] = &category.action;
  113. _map["category.create"] = &category.create;
  114. _map["category.search"] = &category.search;
  115. _map["direct_io"] = &direct_io;
  116. _map["dropcacheonclose"] = &dropcacheonclose;
  117. _map["follow-symlinks"] = &follow_symlinks;
  118. _map["fsname"] = &fsname;
  119. _map["func.access"] = &func.access;
  120. _map["func.chmod"] = &func.chmod;
  121. _map["func.chown"] = &func.chown;
  122. _map["func.create"] = &func.create;
  123. _map["func.getattr"] = &func.getattr;
  124. _map["func.getxattr"] = &func.getxattr;
  125. _map["func.link"] = &func.link;
  126. _map["func.listxattr"] = &func.listxattr;
  127. _map["func.mkdir"] = &func.mkdir;
  128. _map["func.mknod"] = &func.mknod;
  129. _map["func.open"] = &func.open;
  130. _map["func.readlink"] = &func.readlink;
  131. _map["func.removexattr"] = &func.removexattr;
  132. _map["func.rename"] = &func.rename;
  133. _map["func.rmdir"] = &func.rmdir;
  134. _map["func.setxattr"] = &func.setxattr;
  135. _map["func.symlink"] = &func.symlink;
  136. _map["func.truncate"] = &func.truncate;
  137. _map["func.unlink"] = &func.unlink;
  138. _map["func.utimens"] = &func.utimens;
  139. _map["fuse_msg_size"] = &fuse_msg_size;
  140. _map["ignorepponrename"] = &ignorepponrename;
  141. _map["inodecalc"] = &inodecalc;
  142. _map["kernel_cache"] = &kernel_cache;
  143. _map["link_cow"] = &link_cow;
  144. _map["link-exdev"] = &link_exdev;
  145. _map["log.metrics"] = &log_metrics;
  146. _map["minfreespace"] = &minfreespace;
  147. _map["mount"] = &mount;
  148. _map["moveonenospc"] = &moveonenospc;
  149. _map["nfsopenhack"] = &nfsopenhack;
  150. _map["nullrw"] = &nullrw;
  151. _map["pid"] = &pid;
  152. _map["posix_acl"] = &posix_acl;
  153. // _map["readdir"] = &readdir;
  154. _map["readdirplus"] = &readdirplus;
  155. _map["rename-exdev"] = &rename_exdev;
  156. _map["security_capability"] = &security_capability;
  157. _map["srcmounts"] = &srcmounts;
  158. _map["statfs"] = &statfs;
  159. _map["statfs_ignore"] = &statfs_ignore;
  160. _map["symlinkify"] = &symlinkify;
  161. _map["symlinkify_timeout"] = &symlinkify_timeout;
  162. _map["threads"] = &threads;
  163. _map["version"] = &version;
  164. _map["xattr"] = &xattr;
  165. }
  166. Config&
  167. Config::operator=(const Config &cfg_)
  168. {
  169. int rv;
  170. std::string val;
  171. for(auto &kv : _map)
  172. {
  173. rv = cfg_.get(kv.first,&val);
  174. if(rv)
  175. continue;
  176. kv.second->from_string(val);
  177. }
  178. return *this;
  179. }
  180. bool
  181. Config::has_key(const std::string &key_) const
  182. {
  183. return _map.count(key_);
  184. }
  185. void
  186. Config::keys(std::string &s_) const
  187. {
  188. Str2TFStrMap::const_iterator i;
  189. Str2TFStrMap::const_iterator ei;
  190. s_.reserve(512);
  191. i = _map.begin();
  192. ei = _map.end();
  193. for(; i != ei; ++i)
  194. {
  195. s_ += i->first;
  196. s_ += '\0';
  197. }
  198. s_.resize(s_.size() - 1);
  199. }
  200. void
  201. Config::keys_xattr(std::string &s_) const
  202. {
  203. Str2TFStrMap::const_iterator i;
  204. Str2TFStrMap::const_iterator ei;
  205. s_.reserve(1024);
  206. for(i = _map.begin(), ei = _map.end(); i != ei; ++i)
  207. {
  208. s_ += "user.mergerfs.";
  209. s_ += i->first;
  210. s_ += '\0';
  211. }
  212. }
  213. int
  214. Config::get(const std::string &key_,
  215. std::string *val_) const
  216. {
  217. Str2TFStrMap::const_iterator i;
  218. i = _map.find(key_);
  219. if(i == _map.end())
  220. return -ENOATTR;
  221. *val_ = i->second->to_string();
  222. return 0;
  223. }
  224. int
  225. Config::set_raw(const std::string &key_,
  226. const std::string &value_)
  227. {
  228. Str2TFStrMap::iterator i;
  229. i = _map.find(key_);
  230. if(i == _map.end())
  231. return -ENOATTR;
  232. return i->second->from_string(value_);
  233. }
  234. int
  235. Config::set(const std::string &key_,
  236. const std::string &value_)
  237. {
  238. if(l::readonly(key_))
  239. return -EROFS;
  240. return set_raw(key_,value_);
  241. }
  242. int
  243. Config::set(const std::string &kv_)
  244. {
  245. std::string key;
  246. std::string val;
  247. str::splitkv(kv_,'=',&key,&val);
  248. key = str::trim(key);
  249. val = str::trim(val);
  250. return set(key,val);
  251. }
  252. int
  253. Config::from_stream(std::istream &istrm_,
  254. ErrVec *errs_)
  255. {
  256. int rv;
  257. std::string line;
  258. std::string key;
  259. std::string val;
  260. Config newcfg;
  261. newcfg = *this;
  262. while(std::getline(istrm_,line,'\n'))
  263. {
  264. line = str::trim(line);
  265. if(!line.empty() && (line[0] == '#'))
  266. continue;
  267. str::splitkv(line,'=',&key,&val);
  268. key = str::trim(key);
  269. val = str::trim(val);
  270. rv = newcfg.set(key,val);
  271. if(rv < 0)
  272. errs_->push_back({rv,key});
  273. }
  274. if(!errs_->empty())
  275. return -EINVAL;
  276. *this = newcfg;
  277. return 0;
  278. }
  279. int
  280. Config::from_file(const std::string &filepath_,
  281. ErrVec *errs_)
  282. {
  283. int rv;
  284. std::ifstream ifstrm;
  285. ifstrm.open(filepath_);
  286. if(!ifstrm.good())
  287. {
  288. errs_->push_back({-errno,filepath_});
  289. return -errno;
  290. }
  291. rv = from_stream(ifstrm,errs_);
  292. ifstrm.close();
  293. return rv;
  294. }
  295. std::ostream&
  296. operator<<(std::ostream &os_,
  297. const Config &c_)
  298. {
  299. Str2TFStrMap::const_iterator i;
  300. Str2TFStrMap::const_iterator ei;
  301. for(i = c_._map.begin(), ei = c_._map.end(); i != ei; ++i)
  302. {
  303. os_ << i->first << '=' << i->second->to_string() << std::endl;
  304. }
  305. return os_;
  306. }
  307. static
  308. std::string
  309. err2str(const int err_)
  310. {
  311. switch(err_)
  312. {
  313. case 0:
  314. return std::string();
  315. case -EINVAL:
  316. return "invalid value";
  317. case -ENOATTR:
  318. return "unknown option";
  319. case -EROFS:
  320. return "read-only option";
  321. default:
  322. return strerror(-err_);
  323. }
  324. return std::string();
  325. }
  326. std::ostream&
  327. operator<<(std::ostream &os_,
  328. const Config::ErrVec &ev_)
  329. {
  330. std::string errstr;
  331. for(auto &err : ev_)
  332. {
  333. os_ << "* ERROR: ";
  334. errstr = err2str(err.err);
  335. if(!errstr.empty())
  336. os_ << errstr << " - ";
  337. os_ << err.str << std::endl;
  338. }
  339. return os_;
  340. }