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.6 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. _eplus_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 eplus;
  33. const string *eplusbasepath;
  34. eplus = std::numeric_limits<uint64_t>::max();
  35. eplusbasepath = 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(spaceused >= eplus)
  52. continue;
  53. eplus = spaceused;
  54. eplusbasepath = basepath;
  55. }
  56. if(eplusbasepath == NULL)
  57. return POLICY_FAIL_ENOENT;
  58. paths.push_back(eplusbasepath);
  59. return POLICY_SUCCESS;
  60. }
  61. static
  62. int
  63. _eplus_other(const vector<string> &basepaths,
  64. const char *fusepath,
  65. vector<const string*> &paths)
  66. {
  67. string fullpath;
  68. uint64_t eplus;
  69. const string *eplusbasepath;
  70. eplus = 0;
  71. eplusbasepath = NULL;
  72. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  73. {
  74. uint64_t spaceused;
  75. const string *basepath = &basepaths[i];
  76. fs::path::make(basepath,fusepath,fullpath);
  77. if(!fs::exists(fullpath))
  78. continue;
  79. if(!fs::spaceused(*basepath,spaceused))
  80. continue;
  81. if(spaceused >= eplus)
  82. continue;
  83. eplus = spaceused;
  84. eplusbasepath = basepath;
  85. }
  86. if(eplusbasepath == NULL)
  87. return POLICY_FAIL_ENOENT;
  88. paths.push_back(eplusbasepath);
  89. return POLICY_SUCCESS;
  90. }
  91. static
  92. int
  93. _eplus(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 _eplus_create(basepaths,fusepath,minfreespace,paths);
  101. return _eplus_other(basepaths,fusepath,paths);
  102. }
  103. namespace mergerfs
  104. {
  105. int
  106. Policy::Func::eplus(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 = _eplus(type,basepaths,fusepath,minfreespace,paths);
  114. if(POLICY_FAILED(rv))
  115. rv = Policy::Func::lus(type,basepaths,fusepath,minfreespace,paths);
  116. return rv;
  117. }
  118. }