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.

179 lines
4.4 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2020, 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 "errno.hpp"
  16. #include "fs_exists.hpp"
  17. #include "fs_info.hpp"
  18. #include "fs_path.hpp"
  19. #include "fs_statvfs_cache.hpp"
  20. #include "policy.hpp"
  21. #include "policy_msppfrd.hpp"
  22. #include "policies.hpp"
  23. #include "policy_error.hpp"
  24. #include "rnd.hpp"
  25. #include <string>
  26. #include <vector>
  27. using std::string;
  28. using std::vector;
  29. struct BranchInfo
  30. {
  31. uint64_t spaceavail;
  32. const string *basepath;
  33. };
  34. typedef vector<BranchInfo> BranchInfoVec;
  35. namespace msppfrd
  36. {
  37. static
  38. int
  39. create_1(const Branches::CPtr &branches_,
  40. const string &fusepath_,
  41. BranchInfoVec *branchinfo_,
  42. uint64_t *sum_)
  43. {
  44. int rv;
  45. int error;
  46. BranchInfo bi;
  47. fs::info_t info;
  48. *sum_ = 0;
  49. error = ENOENT;
  50. for(auto &branch : *branches_)
  51. {
  52. if(branch.ro_or_nc())
  53. error_and_continue(error,EROFS);
  54. if(!fs::exists(branch.path,fusepath_))
  55. error_and_continue(error,ENOENT);
  56. rv = fs::info(branch.path,&info);
  57. if(rv == -1)
  58. error_and_continue(error,ENOENT);
  59. if(info.readonly)
  60. error_and_continue(error,EROFS);
  61. if(info.spaceavail < branch.minfreespace())
  62. error_and_continue(error,ENOSPC);
  63. *sum_ += info.spaceavail;
  64. bi.spaceavail = info.spaceavail;
  65. bi.basepath = &branch.path;
  66. branchinfo_->push_back(bi);
  67. }
  68. return error;
  69. }
  70. static
  71. int
  72. get_branchinfo(const Branches::CPtr &branches_,
  73. const char *fusepath_,
  74. BranchInfoVec *branchinfo_,
  75. uint64_t *sum_)
  76. {
  77. int error;
  78. string fusepath;
  79. fusepath = fusepath_;
  80. for(;;)
  81. {
  82. error = msppfrd::create_1(branches_,fusepath,branchinfo_,sum_);
  83. if(branchinfo_->size())
  84. break;
  85. if(fusepath == "/")
  86. break;
  87. fusepath = fs::path::dirname(fusepath);
  88. }
  89. return error;
  90. }
  91. static
  92. const
  93. string*
  94. get_branch(const BranchInfoVec &branchinfo_,
  95. const uint64_t sum_)
  96. {
  97. uint64_t idx;
  98. uint64_t threshold;
  99. if(sum_ == 0)
  100. return NULL;
  101. idx = 0;
  102. threshold = RND::rand64(sum_);
  103. for(size_t i = 0; i < branchinfo_.size(); i++)
  104. {
  105. idx += branchinfo_[i].spaceavail;
  106. if(idx < threshold)
  107. continue;
  108. return branchinfo_[i].basepath;
  109. }
  110. return NULL;
  111. }
  112. static
  113. int
  114. create(const Branches::CPtr &branches_,
  115. const char *fusepath_,
  116. StrVec *paths_)
  117. {
  118. int error;
  119. uint64_t sum;
  120. const string *basepath;
  121. BranchInfoVec branchinfo;
  122. error = msppfrd::get_branchinfo(branches_,fusepath_,&branchinfo,&sum);
  123. basepath = msppfrd::get_branch(branchinfo,sum);
  124. if(basepath == NULL)
  125. return (errno=error,-1);
  126. paths_->push_back(*basepath);
  127. return 0;
  128. }
  129. }
  130. int
  131. Policy::MSPPFRD::Action::operator()(const Branches::CPtr &branches_,
  132. const char *fusepath_,
  133. StrVec *paths_) const
  134. {
  135. return Policies::Action::eppfrd(branches_,fusepath_,paths_);
  136. }
  137. int
  138. Policy::MSPPFRD::Create::operator()(const Branches::CPtr &branches_,
  139. const char *fusepath_,
  140. StrVec *paths_) const
  141. {
  142. return ::msppfrd::create(branches_,fusepath_,paths_);
  143. }
  144. int
  145. Policy::MSPPFRD::Search::operator()(const Branches::CPtr &branches_,
  146. const char *fusepath_,
  147. StrVec *paths_) const
  148. {
  149. return Policies::Search::eppfrd(branches_,fusepath_,paths_);
  150. }