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.

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