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.

144 lines
3.7 KiB

  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 <limits>
  15. #include <string>
  16. #include <vector>
  17. #include "errno.hpp"
  18. #include "fs.hpp"
  19. #include "fs_path.hpp"
  20. #include "policy.hpp"
  21. using std::string;
  22. using std::vector;
  23. using mergerfs::Category;
  24. static
  25. int
  26. _epmfs_create(const vector<string> &basepaths,
  27. const char *fusepath,
  28. const uint64_t minfreespace,
  29. vector<const string*> &paths)
  30. {
  31. string fullpath;
  32. uint64_t epmfs;
  33. const string *epmfsbasepath;
  34. epmfs = std::numeric_limits<uint64_t>::min();
  35. epmfsbasepath = NULL;
  36. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  37. {
  38. bool readonly;
  39. uint64_t spaceavail;
  40. uint64_t _spaceused;
  41. const string *basepath = &basepaths[i];
  42. fs::path::make(basepath,fusepath,fullpath);
  43. if(!fs::exists(fullpath))
  44. continue;
  45. if(!fs::info(*basepath,readonly,spaceavail,_spaceused))
  46. continue;
  47. if(readonly)
  48. continue;
  49. if(spaceavail < minfreespace)
  50. continue;
  51. if(spaceavail < epmfs)
  52. continue;
  53. epmfs = spaceavail;
  54. epmfsbasepath = basepath;
  55. }
  56. if(epmfsbasepath == NULL)
  57. return POLICY_FAIL_ENOENT;
  58. paths.push_back(epmfsbasepath);
  59. return POLICY_SUCCESS;
  60. }
  61. static
  62. int
  63. _epmfs_other(const vector<string> &basepaths,
  64. const char *fusepath,
  65. vector<const string*> &paths)
  66. {
  67. string fullpath;
  68. uint64_t epmfs;
  69. const string *epmfsbasepath;
  70. epmfs = 0;
  71. epmfsbasepath = NULL;
  72. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  73. {
  74. uint64_t spaceavail;
  75. const string *basepath = &basepaths[i];
  76. fs::path::make(basepath,fusepath,fullpath);
  77. if(!fs::exists(fullpath))
  78. continue;
  79. if(!fs::spaceavail(*basepath,spaceavail))
  80. continue;
  81. if(spaceavail < epmfs)
  82. continue;
  83. epmfs = spaceavail;
  84. epmfsbasepath = basepath;
  85. }
  86. if(epmfsbasepath == NULL)
  87. return POLICY_FAIL_ENOENT;
  88. paths.push_back(epmfsbasepath);
  89. return POLICY_SUCCESS;
  90. }
  91. static
  92. int
  93. _epmfs(const Category::Enum::Type type,
  94. const vector<string> &basepaths,
  95. const char *fusepath,
  96. const uint64_t minfreespace,
  97. vector<const string*> &paths)
  98. {
  99. if(type == Category::Enum::create)
  100. return _epmfs_create(basepaths,fusepath,minfreespace,paths);
  101. return _epmfs_other(basepaths,fusepath,paths);
  102. }
  103. namespace mergerfs
  104. {
  105. int
  106. Policy::Func::epmfs(const Category::Enum::Type type,
  107. const vector<string> &basepaths,
  108. const char *fusepath,
  109. const uint64_t minfreespace,
  110. vector<const string*> &paths)
  111. {
  112. int rv;
  113. rv = _epmfs(type,basepaths,fusepath,minfreespace,paths);
  114. if(POLICY_FAILED(rv))
  115. rv = Policy::Func::mfs(type,basepaths,fusepath,minfreespace,paths);
  116. return rv;
  117. }
  118. }