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.

196 lines
5.5 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. #ifndef __POLICY_HPP__
  15. #define __POLICY_HPP__
  16. #include <string>
  17. #include <vector>
  18. #include <map>
  19. #include "fs.hpp"
  20. #include "category.hpp"
  21. #define POLICY_SUCCESS 0
  22. #define POLICY_FAIL -1
  23. #define POLICY_SUCCEEDED(RV) ((RV) == POLICY_SUCCESS)
  24. #define POLICY_FAILED(RV) ((RV) == POLICY_FAIL)
  25. namespace mergerfs
  26. {
  27. class Policy
  28. {
  29. public:
  30. struct Enum
  31. {
  32. enum Type
  33. {
  34. invalid = -1,
  35. BEGIN = 0,
  36. all = BEGIN,
  37. einval,
  38. enosys,
  39. enotsup,
  40. eplfs,
  41. epmfs,
  42. erofs,
  43. exdev,
  44. ff,
  45. ffwp,
  46. fwfs,
  47. lfs,
  48. mfs,
  49. newest,
  50. rand,
  51. END
  52. };
  53. static size_t begin() { return BEGIN; }
  54. static size_t end() { return END; }
  55. };
  56. struct Func
  57. {
  58. typedef std::string string;
  59. typedef std::size_t size_t;
  60. typedef std::vector<string> strvec;
  61. typedef std::vector<const string*> cstrptrvec;
  62. typedef const string cstring;
  63. typedef const size_t csize_t;
  64. typedef const strvec cstrvec;
  65. typedef const Category::Enum::Type CType;
  66. typedef int (*Ptr)(CType,cstrvec &,const char *,csize_t,cstrptrvec &);
  67. template <CType T>
  68. class Base
  69. {
  70. public:
  71. Base(const Policy *p)
  72. : func(p->_func)
  73. {}
  74. int
  75. operator()(cstrvec &b,const char *c,csize_t d,cstrptrvec &e)
  76. {
  77. return func(T,b,c,d,e);
  78. }
  79. private:
  80. const Ptr func;
  81. };
  82. typedef Base<Category::Enum::action> Action;
  83. typedef Base<Category::Enum::create> Create;
  84. typedef Base<Category::Enum::search> Search;
  85. static int all(CType,cstrvec&,const char*,csize_t,cstrptrvec&);
  86. static int einval(CType,cstrvec&,const char*,csize_t,cstrptrvec&);
  87. static int enosys(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  88. static int enotsup(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  89. static int eplfs(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  90. static int epmfs(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  91. static int erofs(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  92. static int exdev(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  93. static int ff(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  94. static int ffwp(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  95. static int fwfs(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  96. static int invalid(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  97. static int lfs(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  98. static int mfs(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  99. static int newest(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  100. static int rand(CType,cstrvec&,const char *,csize_t,cstrptrvec&);
  101. };
  102. private:
  103. Enum::Type _enum;
  104. std::string _str;
  105. Func::Ptr _func;
  106. bool _path_preserving;
  107. public:
  108. Policy()
  109. : _enum(invalid),
  110. _str(invalid),
  111. _func(invalid),
  112. _path_preserving(false)
  113. {
  114. }
  115. Policy(const Enum::Type enum_,
  116. const std::string &str_,
  117. const Func::Ptr func_,
  118. const bool path_preserving_)
  119. : _enum(enum_),
  120. _str(str_),
  121. _func(func_),
  122. _path_preserving(path_preserving_)
  123. {
  124. }
  125. bool
  126. path_preserving() const
  127. {
  128. return _path_preserving;
  129. }
  130. public:
  131. operator const Enum::Type() const { return _enum; }
  132. operator const std::string&() const { return _str; }
  133. operator const Func::Ptr() const { return _func; }
  134. operator const Policy*() const { return this; }
  135. bool operator==(const Enum::Type enum_) const
  136. { return _enum == enum_; }
  137. bool operator==(const std::string &str_) const
  138. { return _str == str_; }
  139. bool operator==(const Func::Ptr func_) const
  140. { return _func == func_; }
  141. bool operator!=(const Policy &r) const
  142. { return _enum != r._enum; }
  143. bool operator<(const Policy &r) const
  144. { return _enum < r._enum; }
  145. public:
  146. static const Policy &find(const std::string&);
  147. static const Policy &find(const Enum::Type);
  148. public:
  149. static const std::vector<Policy> _policies_;
  150. static const Policy * const policies;
  151. static const Policy &invalid;
  152. static const Policy &all;
  153. static const Policy &einval;
  154. static const Policy &enosys;
  155. static const Policy &enotsup;
  156. static const Policy &eplfs;
  157. static const Policy &epmfs;
  158. static const Policy &erofs;
  159. static const Policy &exdev;
  160. static const Policy &ff;
  161. static const Policy &ffwp;
  162. static const Policy &fwfs;
  163. static const Policy &lfs;
  164. static const Policy &mfs;
  165. static const Policy &newest;
  166. static const Policy &rand;
  167. };
  168. }
  169. #endif