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.

146 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 "errno.hpp"
  15. #include "fs.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_error.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 Branches &branches_,
  31. const char *fusepath,
  32. const uint64_t minfreespace,
  33. vector<const string*> &paths)
  34. {
  35. int rv;
  36. int error;
  37. fs::info_t info;
  38. const Branch *branch;
  39. error = ENOENT;
  40. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  41. {
  42. branch = &branches_[i];
  43. if(!fs::exists(branch->path,fusepath))
  44. error_and_continue(error,ENOENT);
  45. if(branch->ro_or_nc())
  46. error_and_continue(error,EROFS);
  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 < minfreespace)
  53. error_and_continue(error,ENOSPC);
  54. paths.push_back(&branch->path);
  55. return 0;
  56. }
  57. return (errno=error,-1);
  58. }
  59. static
  60. int
  61. action(const Branches &branches_,
  62. const char *fusepath,
  63. vector<const string*> &paths)
  64. {
  65. int rv;
  66. int error;
  67. bool readonly;
  68. const Branch *branch;
  69. error = ENOENT;
  70. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  71. {
  72. branch = &branches_[i];
  73. if(!fs::exists(branch->path,fusepath))
  74. error_and_continue(error,ENOENT);
  75. if(branch->ro())
  76. error_and_continue(error,EROFS);
  77. rv = fs::statvfs_cache_readonly(branch->path,&readonly);
  78. if(rv == -1)
  79. error_and_continue(error,ENOENT);
  80. if(readonly)
  81. error_and_continue(error,EROFS);
  82. paths.push_back(&branch->path);
  83. return 0;
  84. }
  85. return (errno=error,-1);
  86. }
  87. static
  88. int
  89. search(const Branches &branches_,
  90. const char *fusepath,
  91. vector<const string*> &paths)
  92. {
  93. const Branch *branch;
  94. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  95. {
  96. branch = &branches_[i];
  97. if(!fs::exists(branch->path,fusepath))
  98. continue;
  99. paths.push_back(&branch->path);
  100. return 0;
  101. }
  102. return (errno=ENOENT,-1);
  103. }
  104. }
  105. int
  106. Policy::Func::epff(const Category::Enum::Type type,
  107. const Branches &branches_,
  108. const char *fusepath,
  109. const uint64_t minfreespace,
  110. vector<const string*> &paths)
  111. {
  112. switch(type)
  113. {
  114. case Category::Enum::create:
  115. return epff::create(branches_,fusepath,minfreespace,paths);
  116. case Category::Enum::action:
  117. return epff::action(branches_,fusepath,paths);
  118. case Category::Enum::search:
  119. default:
  120. return epff::search(branches_,fusepath,paths);
  121. }
  122. }