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.

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