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.

315 lines
5.7 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2018, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "branch.hpp"
  16. #include "ef.hpp"
  17. #include "fs.hpp"
  18. #include "fs_glob.hpp"
  19. #include "str.hpp"
  20. #include <string>
  21. #include <errno.h>
  22. #include <fnmatch.h>
  23. using std::string;
  24. using std::vector;
  25. bool
  26. Branch::ro(void) const
  27. {
  28. return (mode == Branch::RO);
  29. }
  30. bool
  31. Branch::nc(void) const
  32. {
  33. return (mode == Branch::NC);
  34. }
  35. bool
  36. Branch::ro_or_nc(void) const
  37. {
  38. return ((mode == Branch::RO) ||
  39. (mode == Branch::NC));
  40. }
  41. static
  42. void
  43. split(const std::string &s_,
  44. std::string *instr_,
  45. std::string *values_)
  46. {
  47. uint64_t offset;
  48. offset = s_.find_first_of('/');
  49. *instr_ = s_.substr(0,offset);
  50. if(offset != std::string::npos)
  51. *values_ = s_.substr(offset);
  52. }
  53. Branches::Branches()
  54. {
  55. pthread_rwlock_init(&lock,NULL);
  56. }
  57. static
  58. void
  59. parse(const string &str_,
  60. Branches &branches_)
  61. {
  62. string str;
  63. Branch branch;
  64. vector<string> globbed;
  65. str = str_;
  66. branch.mode = Branch::INVALID;
  67. if(str::endswith(str,"=RO"))
  68. branch.mode = Branch::RO;
  69. ef(str::endswith(str,"=RW"))
  70. branch.mode = Branch::RW;
  71. ef(str::endswith(str,"=NC"))
  72. branch.mode = Branch::NC;
  73. if(branch.mode != Branch::INVALID)
  74. str.resize(str.size() - 3);
  75. else
  76. branch.mode = Branch::RW;
  77. fs::glob(str,globbed);
  78. fs::realpathize(globbed);
  79. for(size_t i = 0; i < globbed.size(); i++)
  80. {
  81. branch.path = globbed[i];
  82. branches_.push_back(branch);
  83. }
  84. }
  85. static
  86. void
  87. set(Branches &branches_,
  88. const std::string &str_)
  89. {
  90. vector<string> paths;
  91. branches_.clear();
  92. str::split(paths,str_,':');
  93. for(size_t i = 0; i < paths.size(); i++)
  94. {
  95. Branches tmp;
  96. parse(paths[i],tmp);
  97. branches_.insert(branches_.end(),
  98. tmp.begin(),
  99. tmp.end());
  100. }
  101. }
  102. static
  103. void
  104. add_begin(Branches &branches_,
  105. const std::string &str_)
  106. {
  107. vector<string> paths;
  108. str::split(paths,str_,':');
  109. for(size_t i = 0; i < paths.size(); i++)
  110. {
  111. Branches tmp;
  112. parse(paths[i],tmp);
  113. branches_.insert(branches_.begin(),
  114. tmp.begin(),
  115. tmp.end());
  116. }
  117. }
  118. static
  119. void
  120. add_end(Branches &branches_,
  121. const std::string &str_)
  122. {
  123. vector<string> paths;
  124. str::split(paths,str_,':');
  125. for(size_t i = 0; i < paths.size(); i++)
  126. {
  127. Branches tmp;
  128. parse(paths[i],tmp);
  129. branches_.insert(branches_.end(),
  130. tmp.begin(),
  131. tmp.end());
  132. }
  133. }
  134. static
  135. void
  136. erase_begin(Branches &branches_)
  137. {
  138. branches_.erase(branches_.begin());
  139. }
  140. static
  141. void
  142. erase_end(Branches &branches_)
  143. {
  144. branches_.pop_back();
  145. }
  146. static
  147. void
  148. erase_fnmatch(Branches &branches_,
  149. const std::string &str_)
  150. {
  151. vector<string> patterns;
  152. str::split(patterns,str_,':');
  153. for(Branches::iterator i = branches_.begin();
  154. i != branches_.end();)
  155. {
  156. int match = FNM_NOMATCH;
  157. for(vector<string>::const_iterator pi = patterns.begin();
  158. pi != patterns.end() && match != 0;
  159. ++pi)
  160. {
  161. match = ::fnmatch(pi->c_str(),i->path.c_str(),0);
  162. }
  163. i = ((match == 0) ? branches_.erase(i) : (i+1));
  164. }
  165. }
  166. int
  167. Branches::from_string(const std::string &s_)
  168. {
  169. rwlock::WriteGuard guard(&lock);
  170. std::string instr;
  171. std::string values;
  172. ::split(s_,&instr,&values);
  173. if(instr == "+")
  174. ::add_end(*this,values);
  175. ef(instr == "+<")
  176. ::add_begin(*this,values);
  177. ef(instr == "+>")
  178. ::add_end(*this,values);
  179. ef(instr == "-")
  180. ::erase_fnmatch(*this,values);
  181. ef(instr == "-<")
  182. ::erase_begin(*this);
  183. ef(instr == "->")
  184. ::erase_end(*this);
  185. ef(instr == "=")
  186. ::set(*this,values);
  187. ef(instr.empty())
  188. ::set(*this,values);
  189. else
  190. return -EINVAL;
  191. return 0;
  192. }
  193. string
  194. Branches::to_string(void) const
  195. {
  196. rwlock::ReadGuard guard(&lock);
  197. string tmp;
  198. for(size_t i = 0; i < size(); i++)
  199. {
  200. const Branch &branch = (*this)[i];
  201. tmp += branch.path;
  202. tmp += '=';
  203. switch(branch.mode)
  204. {
  205. default:
  206. case Branch::RW:
  207. tmp += "RW";
  208. break;
  209. case Branch::RO:
  210. tmp += "RO";
  211. break;
  212. case Branch::NC:
  213. tmp += "NC";
  214. break;
  215. }
  216. tmp += ':';
  217. }
  218. if(*tmp.rbegin() == ':')
  219. tmp.erase(tmp.size() - 1);
  220. return tmp;
  221. }
  222. void
  223. Branches::to_paths(vector<string> &vec_) const
  224. {
  225. rwlock::ReadGuard guard(&lock);
  226. for(size_t i = 0; i < size(); i++)
  227. {
  228. const Branch &branch = (*this)[i];
  229. vec_.push_back(branch.path);
  230. }
  231. }
  232. SrcMounts::SrcMounts(Branches &b_)
  233. : _branches(b_)
  234. {
  235. }
  236. int
  237. SrcMounts::from_string(const std::string &s_)
  238. {
  239. return _branches.from_string(s_);
  240. }
  241. std::string
  242. SrcMounts::to_string(void) const
  243. {
  244. rwlock::ReadGuard guard(&_branches.lock);
  245. std::string rv;
  246. for(uint64_t i = 0; i < _branches.size(); i++)
  247. {
  248. rv += _branches[i].path;
  249. rv += ':';
  250. }
  251. if(*rv.rbegin() == ':')
  252. rv.erase(rv.size() - 1);
  253. return rv;
  254. }