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.

176 lines
4.5 KiB

6 years ago
6 years ago
  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 "errno.hpp"
  15. #include "fs_exists.hpp"
  16. #include "fs_info.hpp"
  17. #include "fs_path.hpp"
  18. #include "fs_statvfs_cache.hpp"
  19. #include "policy.hpp"
  20. #include "policy_eplus.hpp"
  21. #include "policy_error.hpp"
  22. #include "rwlock.hpp"
  23. #include <limits>
  24. #include <string>
  25. using std::string;
  26. namespace eplus
  27. {
  28. static
  29. int
  30. create(const Branches::CPtr &branches_,
  31. const char *fusepath_,
  32. StrVec *paths_)
  33. {
  34. int rv;
  35. int error;
  36. uint64_t eplus;
  37. fs::info_t info;
  38. const string *basepath;
  39. error = ENOENT;
  40. eplus = std::numeric_limits<uint64_t>::max();
  41. basepath = NULL;
  42. for(auto &branch : *branches_)
  43. {
  44. if(branch.ro_or_nc())
  45. error_and_continue(error,EROFS);
  46. if(!fs::exists(branch.path,fusepath_))
  47. error_and_continue(error,ENOENT);
  48. rv = fs::info(branch.path,&info);
  49. if(rv == -1)
  50. error_and_continue(error,ENOENT);
  51. if(info.readonly)
  52. error_and_continue(error,EROFS);
  53. if(info.spaceavail < branch.minfreespace())
  54. error_and_continue(error,ENOSPC);
  55. if(info.spaceused >= eplus)
  56. continue;
  57. eplus = info.spaceused;
  58. basepath = &branch.path;
  59. }
  60. if(basepath == NULL)
  61. return (errno=error,-1);
  62. paths_->push_back(*basepath);
  63. return 0;
  64. }
  65. static
  66. int
  67. action(const Branches::CPtr &branches_,
  68. const char *fusepath_,
  69. StrVec *paths_)
  70. {
  71. int rv;
  72. int error;
  73. uint64_t eplus;
  74. fs::info_t info;
  75. const string *basepath;
  76. error = ENOENT;
  77. eplus = std::numeric_limits<uint64_t>::max();
  78. basepath = NULL;
  79. for(auto &branch : *branches_)
  80. {
  81. if(branch.ro())
  82. error_and_continue(error,EROFS);
  83. if(!fs::exists(branch.path,fusepath_))
  84. error_and_continue(error,ENOENT);
  85. rv = fs::info(branch.path,&info);
  86. if(rv == -1)
  87. error_and_continue(error,ENOENT);
  88. if(info.readonly)
  89. error_and_continue(error,EROFS);
  90. if(info.spaceused >= eplus)
  91. continue;
  92. eplus = info.spaceused;
  93. basepath = &branch.path;
  94. }
  95. if(basepath == NULL)
  96. return (errno=error,-1);
  97. paths_->push_back(*basepath);
  98. return 0;
  99. }
  100. static
  101. int
  102. search(const Branches::CPtr &branches_,
  103. const char *fusepath_,
  104. StrVec *paths_)
  105. {
  106. int rv;
  107. uint64_t eplus;
  108. uint64_t spaceused;
  109. const string *basepath;
  110. eplus = 0;
  111. basepath = NULL;
  112. for(auto &branch : *branches_)
  113. {
  114. if(!fs::exists(branch.path,fusepath_))
  115. continue;
  116. rv = fs::statvfs_cache_spaceused(branch.path,&spaceused);
  117. if(rv == -1)
  118. continue;
  119. if(spaceused >= eplus)
  120. continue;
  121. eplus = spaceused;
  122. basepath = &branch.path;
  123. }
  124. if(basepath == NULL)
  125. return (errno=ENOENT,-1);
  126. paths_->push_back(*basepath);
  127. return 0;
  128. }
  129. }
  130. int
  131. Policy::EPLUS::Action::operator()(const Branches::CPtr &branches_,
  132. const char *fusepath_,
  133. StrVec *paths_) const
  134. {
  135. return ::eplus::action(branches_,fusepath_,paths_);
  136. }
  137. int
  138. Policy::EPLUS::Create::operator()(const Branches::CPtr &branches_,
  139. const char *fusepath_,
  140. StrVec *paths_) const
  141. {
  142. return ::eplus::create(branches_,fusepath_,paths_);
  143. }
  144. int
  145. Policy::EPLUS::Search::operator()(const Branches::CPtr &branches_,
  146. const char *fusepath_,
  147. StrVec *paths_) const
  148. {
  149. return ::eplus::search(branches_,fusepath_,paths_);
  150. }