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.

275 lines
7.0 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 "to_string.hpp"
  21. #include "version.hpp"
  22. #include <algorithm>
  23. #include <string>
  24. #include <iostream>
  25. #include <stdint.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #define MINFREESPACE_DEFAULT (4294967295ULL)
  29. using std::string;
  30. #define IFERT(S) if(S == s_) return true
  31. namespace l
  32. {
  33. static
  34. bool
  35. readonly(const std::string &s_)
  36. {
  37. IFERT("async_read");
  38. IFERT("cache.symlinks");
  39. IFERT("cache.writeback");
  40. IFERT("fsname");
  41. IFERT("fuse_msg_size");
  42. IFERT("mount");
  43. IFERT("nullrw");
  44. IFERT("pid");
  45. IFERT("readdirplus");
  46. IFERT("threads");
  47. IFERT("version");
  48. return false;
  49. }
  50. }
  51. Config::Config()
  52. :
  53. open_cache(),
  54. controlfile("/.mergerfs"),
  55. async_read(true),
  56. auto_cache(false),
  57. branches(minfreespace),
  58. cache_attr(1),
  59. cache_entry(1),
  60. cache_files(CacheFiles::ENUM::LIBFUSE),
  61. cache_negative_entry(0),
  62. cache_readdir(false),
  63. cache_statfs(0),
  64. cache_symlinks(false),
  65. category(func),
  66. direct_io(false),
  67. dropcacheonclose(false),
  68. fsname(),
  69. func(),
  70. fuse_msg_size(FUSE_MAX_MAX_PAGES),
  71. ignorepponrename(false),
  72. inodecalc("hybrid-hash"),
  73. link_cow(false),
  74. minfreespace(MINFREESPACE_DEFAULT),
  75. mount(),
  76. moveonenospc(false),
  77. nfsopenhack(NFSOpenHack::ENUM::OFF),
  78. nullrw(false),
  79. pid(::getpid()),
  80. posix_acl(false),
  81. readdir(ReadDir::ENUM::POSIX),
  82. readdirplus(false),
  83. security_capability(true),
  84. srcmounts(branches),
  85. statfs(StatFS::ENUM::BASE),
  86. statfs_ignore(StatFSIgnore::ENUM::NONE),
  87. symlinkify(false),
  88. symlinkify_timeout(3600),
  89. threads(0),
  90. version(MERGERFS_VERSION),
  91. writeback_cache(false),
  92. xattr(XAttr::ENUM::PASSTHROUGH)
  93. {
  94. _map["async_read"] = &async_read;
  95. _map["auto_cache"] = &auto_cache;
  96. _map["branches"] = &branches;
  97. _map["cache.attr"] = &cache_attr;
  98. _map["cache.entry"] = &cache_entry;
  99. _map["cache.files"] = &cache_files;
  100. _map["cache.negative_entry"] = &cache_negative_entry;
  101. _map["cache.readdir"] = &cache_readdir;
  102. _map["cache.statfs"] = &cache_statfs;
  103. _map["cache.symlinks"] = &cache_symlinks;
  104. _map["cache.writeback"] = &writeback_cache;
  105. _map["category.action"] = &category.action;
  106. _map["category.create"] = &category.create;
  107. _map["category.search"] = &category.search;
  108. _map["direct_io"] = &direct_io;
  109. _map["dropcacheonclose"] = &dropcacheonclose;
  110. _map["fsname"] = &fsname;
  111. _map["func.access"] = &func.access;
  112. _map["func.chmod"] = &func.chmod;
  113. _map["func.chown"] = &func.chown;
  114. _map["func.create"] = &func.create;
  115. _map["func.getattr"] = &func.getattr;
  116. _map["func.getxattr"] = &func.getxattr;
  117. _map["func.link"] = &func.link;
  118. _map["func.listxattr"] = &func.listxattr;
  119. _map["func.mkdir"] = &func.mkdir;
  120. _map["func.mknod"] = &func.mknod;
  121. _map["func.open"] = &func.open;
  122. _map["func.readlink"] = &func.readlink;
  123. _map["func.removexattr"] = &func.removexattr;
  124. _map["func.rename"] = &func.rename;
  125. _map["func.rmdir"] = &func.rmdir;
  126. _map["func.setxattr"] = &func.setxattr;
  127. _map["func.symlink"] = &func.symlink;
  128. _map["func.truncate"] = &func.truncate;
  129. _map["func.unlink"] = &func.unlink;
  130. _map["func.utimens"] = &func.utimens;
  131. _map["fuse_msg_size"] = &fuse_msg_size;
  132. _map["ignorepponrename"] = &ignorepponrename;
  133. _map["inodecalc"] = &inodecalc;
  134. _map["kernel_cache"] = &kernel_cache;
  135. _map["link_cow"] = &link_cow;
  136. _map["minfreespace"] = &minfreespace;
  137. _map["mount"] = &mount;
  138. _map["moveonenospc"] = &moveonenospc;
  139. _map["nfsopenhack"] = &nfsopenhack;
  140. _map["nullrw"] = &nullrw;
  141. _map["pid"] = &pid;
  142. _map["posix_acl"] = &posix_acl;
  143. // _map["readdir"] = &readdir;
  144. _map["readdirplus"] = &readdirplus;
  145. _map["security_capability"] = &security_capability;
  146. _map["srcmounts"] = &srcmounts;
  147. _map["statfs"] = &statfs;
  148. _map["statfs_ignore"] = &statfs_ignore;
  149. _map["symlinkify"] = &symlinkify;
  150. _map["symlinkify_timeout"] = &symlinkify_timeout;
  151. _map["threads"] = &threads;
  152. _map["version"] = &version;
  153. _map["xattr"] = &xattr;
  154. }
  155. const
  156. Config&
  157. Config::ro(void)
  158. {
  159. return *((Config*)fuse_get_context()->private_data);
  160. }
  161. Config&
  162. Config::rw(void)
  163. {
  164. return *((Config*)fuse_get_context()->private_data);
  165. }
  166. bool
  167. Config::has_key(const std::string &key_) const
  168. {
  169. return _map.count(key_);
  170. }
  171. void
  172. Config::keys(std::string &s_) const
  173. {
  174. Str2TFStrMap::const_iterator i;
  175. Str2TFStrMap::const_iterator ei;
  176. s_.reserve(512);
  177. i = _map.begin();
  178. ei = _map.end();
  179. for(; i != ei; ++i)
  180. {
  181. s_ += i->first;
  182. s_ += '\0';
  183. }
  184. s_.resize(s_.size() - 1);
  185. }
  186. void
  187. Config::keys_xattr(std::string &s_) const
  188. {
  189. Str2TFStrMap::const_iterator i;
  190. Str2TFStrMap::const_iterator ei;
  191. s_.reserve(1024);
  192. for(i = _map.begin(), ei = _map.end(); i != ei; ++i)
  193. {
  194. s_ += "user.mergerfs.";
  195. s_ += i->first;
  196. s_ += '\0';
  197. }
  198. }
  199. int
  200. Config::get(const std::string &key_,
  201. std::string *val_) const
  202. {
  203. Str2TFStrMap::const_iterator i;
  204. i = _map.find(key_);
  205. if(i == _map.end())
  206. return -ENOATTR;
  207. *val_ = i->second->to_string();
  208. return 0;
  209. }
  210. int
  211. Config::set_raw(const std::string &key_,
  212. const std::string &value_)
  213. {
  214. Str2TFStrMap::iterator i;
  215. i = _map.find(key_);
  216. if(i == _map.end())
  217. return -ENOATTR;
  218. return i->second->from_string(value_);
  219. }
  220. int
  221. Config::set(const std::string &key_,
  222. const std::string &value_)
  223. {
  224. if(l::readonly(key_))
  225. return -EINVAL;
  226. return set_raw(key_,value_);
  227. }
  228. std::ostream&
  229. operator<<(std::ostream &os_,
  230. const Config &c_)
  231. {
  232. Str2TFStrMap::const_iterator i;
  233. Str2TFStrMap::const_iterator ei;
  234. for(i = c_._map.begin(), ei = c_._map.end(); i != ei; ++i)
  235. {
  236. os_ << i->first << '=' << i->second << '\n';
  237. }
  238. return os_;
  239. }