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.

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