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.

142 lines
3.7 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 "branches.hpp"
  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_epff.hpp"
  22. #include "policy_error.hpp"
  23. #include "rwlock.hpp"
  24. #include <string>
  25. #include <vector>
  26. using std::string;
  27. using std::vector;
  28. namespace epff
  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. fs::info_t info;
  39. error = ENOENT;
  40. for(auto &branch : *branches_)
  41. {
  42. if(branch.ro_or_nc())
  43. error_and_continue(error,EROFS);
  44. if(!fs::exists(branch.path,fusepath_))
  45. error_and_continue(error,ENOENT);
  46. rv = fs::info(branch.path,&info);
  47. if(rv == -1)
  48. error_and_continue(error,ENOENT);
  49. if(info.readonly)
  50. error_and_continue(error,EROFS);
  51. if(info.spaceavail < branch.minfreespace())
  52. error_and_continue(error,ENOSPC);
  53. paths_->push_back(branch.path);
  54. return 0;
  55. }
  56. return (errno=error,-1);
  57. }
  58. static
  59. int
  60. action(const Branches::CPtr &branches_,
  61. const char *fusepath_,
  62. StrVec *paths_)
  63. {
  64. int rv;
  65. int error;
  66. bool readonly;
  67. error = ENOENT;
  68. for(auto &branch : *branches_)
  69. {
  70. if(branch.ro())
  71. error_and_continue(error,EROFS);
  72. if(!fs::exists(branch.path,fusepath_))
  73. error_and_continue(error,ENOENT);
  74. rv = fs::statvfs_cache_readonly(branch.path,&readonly);
  75. if(rv == -1)
  76. error_and_continue(error,ENOENT);
  77. if(readonly)
  78. error_and_continue(error,EROFS);
  79. paths_->push_back(branch.path);
  80. return 0;
  81. }
  82. return (errno=error,-1);
  83. }
  84. static
  85. int
  86. search(const Branches::CPtr &branches_,
  87. const char *fusepath_,
  88. StrVec *paths_)
  89. {
  90. for(auto &branch : *branches_)
  91. {
  92. if(!fs::exists(branch.path,fusepath_))
  93. continue;
  94. paths_->push_back(branch.path);
  95. return 0;
  96. }
  97. return (errno=ENOENT,-1);
  98. }
  99. }
  100. int
  101. Policy::EPFF::Action::operator()(const Branches::CPtr &branches_,
  102. const char *fusepath_,
  103. StrVec *paths_) const
  104. {
  105. return ::epff::action(branches_,fusepath_,paths_);
  106. }
  107. int
  108. Policy::EPFF::Create::operator()(const Branches::CPtr &branches_,
  109. const char *fusepath_,
  110. StrVec *paths_) const
  111. {
  112. return ::epff::create(branches_,fusepath_,paths_);
  113. }
  114. int
  115. Policy::EPFF::Search::operator()(const Branches::CPtr &branches_,
  116. const char *fusepath_,
  117. StrVec *paths_) const
  118. {
  119. return ::epff::search(branches_,fusepath_,paths_);
  120. }