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.2 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_error.hpp"
  21. #include "rwlock.hpp"
  22. #include <string>
  23. #include <vector>
  24. using std::string;
  25. using std::vector;
  26. namespace epff
  27. {
  28. static
  29. int
  30. create(const BranchVec &branches_,
  31. const char *fusepath_,
  32. vector<string> *paths_)
  33. {
  34. int rv;
  35. int error;
  36. fs::info_t info;
  37. const Branch *branch;
  38. error = ENOENT;
  39. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  40. {
  41. branch = &branches_[i];
  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. create(const Branches &branches_,
  61. const char *fusepath_,
  62. vector<string> *paths_)
  63. {
  64. rwlock::ReadGuard guard(branches_.lock);
  65. return epff::create(branches_.vec,fusepath_,paths_);
  66. }
  67. static
  68. int
  69. action(const BranchVec &branches_,
  70. const char *fusepath_,
  71. vector<string> *paths_)
  72. {
  73. int rv;
  74. int error;
  75. bool readonly;
  76. const Branch *branch;
  77. error = ENOENT;
  78. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  79. {
  80. branch = &branches_[i];
  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::statvfs_cache_readonly(branch->path,&readonly);
  86. if(rv == -1)
  87. error_and_continue(error,ENOENT);
  88. if(readonly)
  89. error_and_continue(error,EROFS);
  90. paths_->push_back(branch->path);
  91. return 0;
  92. }
  93. return (errno=error,-1);
  94. }
  95. static
  96. int
  97. action(const Branches &branches_,
  98. const char *fusepath_,
  99. vector<string> *paths_)
  100. {
  101. rwlock::ReadGuard guard(branches_.lock);
  102. return epff::action(branches_.vec,fusepath_,paths_);
  103. }
  104. static
  105. int
  106. search(const BranchVec &branches_,
  107. const char *fusepath_,
  108. vector<string> *paths_)
  109. {
  110. const Branch *branch;
  111. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  112. {
  113. branch = &branches_[i];
  114. if(!fs::exists(branch->path,fusepath_))
  115. continue;
  116. paths_->push_back(branch->path);
  117. return 0;
  118. }
  119. return (errno=ENOENT,-1);
  120. }
  121. static
  122. int
  123. search(const Branches &branches_,
  124. const char *fusepath_,
  125. vector<string> *paths_)
  126. {
  127. rwlock::ReadGuard guard(branches_.lock);
  128. return epff::search(branches_.vec,fusepath_,paths_);
  129. }
  130. }
  131. int
  132. Policy::Func::epff(const Category type_,
  133. const Branches &branches_,
  134. const char *fusepath_,
  135. vector<string> *paths_)
  136. {
  137. switch(type_)
  138. {
  139. case Category::CREATE:
  140. return epff::create(branches_,fusepath_,paths_);
  141. case Category::ACTION:
  142. return epff::action(branches_,fusepath_,paths_);
  143. case Category::SEARCH:
  144. default:
  145. return epff::search(branches_,fusepath_,paths_);
  146. }
  147. }