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.

177 lines
4.5 KiB

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