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.

139 lines
3.4 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. _lus_create(const vector<string> &basepaths,
  27. const uint64_t minfreespace,
  28. vector<const string*> &paths)
  29. {
  30. string fullpath;
  31. uint64_t lus;
  32. const string *lusbasepath;
  33. lus = std::numeric_limits<uint64_t>::max();
  34. lusbasepath = NULL;
  35. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  36. {
  37. bool readonly;
  38. uint64_t spaceused;
  39. uint64_t spaceavail;
  40. const string *basepath = &basepaths[i];
  41. if(!fs::info(*basepath,readonly,spaceavail,spaceused))
  42. continue;
  43. if(readonly)
  44. continue;
  45. if(spaceavail < minfreespace)
  46. continue;
  47. if(spaceused >= lus)
  48. continue;
  49. lus = spaceused;
  50. lusbasepath = basepath;
  51. }
  52. if(lusbasepath == NULL)
  53. return POLICY_FAIL_ENOENT;
  54. paths.push_back(lusbasepath);
  55. return POLICY_SUCCESS;
  56. }
  57. static
  58. int
  59. _lus_other(const vector<string> &basepaths,
  60. const char *fusepath,
  61. vector<const string*> &paths)
  62. {
  63. string fullpath;
  64. uint64_t lus;
  65. const string *lusbasepath;
  66. lus = 0;
  67. lusbasepath = NULL;
  68. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  69. {
  70. uint64_t spaceused;
  71. const string *basepath = &basepaths[i];
  72. fs::path::make(basepath,fusepath,fullpath);
  73. if(!fs::exists(fullpath))
  74. continue;
  75. if(!fs::spaceused(*basepath,spaceused))
  76. continue;
  77. if(spaceused >= lus)
  78. continue;
  79. lus = spaceused;
  80. lusbasepath = basepath;
  81. }
  82. if(lusbasepath == NULL)
  83. return POLICY_FAIL_ENOENT;
  84. paths.push_back(lusbasepath);
  85. return POLICY_SUCCESS;
  86. }
  87. static
  88. int
  89. _lus(const Category::Enum::Type type,
  90. const vector<string> &basepaths,
  91. const char *fusepath,
  92. const uint64_t minfreespace,
  93. vector<const string*> &paths)
  94. {
  95. if(type == Category::Enum::create)
  96. return _lus_create(basepaths,minfreespace,paths);
  97. return _lus_other(basepaths,fusepath,paths);
  98. }
  99. namespace mergerfs
  100. {
  101. int
  102. Policy::Func::lus(const Category::Enum::Type type,
  103. const vector<string> &basepaths,
  104. const char *fusepath,
  105. const uint64_t minfreespace,
  106. vector<const string*> &paths)
  107. {
  108. int rv;
  109. rv = _lus(type,basepaths,fusepath,minfreespace,paths);
  110. if(POLICY_FAILED(rv))
  111. rv = Policy::Func::mfs(type,basepaths,fusepath,minfreespace,paths);
  112. return rv;
  113. }
  114. }