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.

141 lines
3.8 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <sys/statvfs.h>
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #include <string>
  26. #include <vector>
  27. #include "policy.hpp"
  28. using std::string;
  29. using std::vector;
  30. using std::size_t;
  31. using mergerfs::Policy;
  32. using mergerfs::Category;
  33. static
  34. int
  35. _lfs_create(const Category::Enum::Type type,
  36. const vector<string> &basepaths,
  37. const string &fusepath,
  38. const size_t minfreespace,
  39. vector<string> &paths)
  40. {
  41. fsblkcnt_t lfs;
  42. string lfsstr;
  43. lfs = -1;
  44. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  45. {
  46. int rv;
  47. struct statvfs fsstats;
  48. const string &basepath = basepaths[i];
  49. rv = ::statvfs(basepath.c_str(),&fsstats);
  50. if(rv == 0)
  51. {
  52. fsblkcnt_t spaceavail;
  53. spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
  54. if((spaceavail > minfreespace) &&
  55. (spaceavail < lfs))
  56. {
  57. lfs = spaceavail;
  58. lfsstr = basepath;
  59. }
  60. }
  61. }
  62. if(lfsstr.empty())
  63. return Policy::Func::mfs(type,basepaths,fusepath,minfreespace,paths);
  64. paths.push_back(lfsstr);
  65. return 0;
  66. }
  67. static
  68. int
  69. _lfs(const Category::Enum::Type type,
  70. const vector<string> &basepaths,
  71. const string &fusepath,
  72. const size_t minfreespace,
  73. vector<string> &paths)
  74. {
  75. fsblkcnt_t lfs;
  76. string lfsstr;
  77. lfs = -1;
  78. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  79. {
  80. int rv;
  81. string fullpath;
  82. struct statvfs fsstats;
  83. const string &basepath = basepaths[i];
  84. fullpath = fs::path::make(basepath,fusepath);
  85. rv = ::statvfs(fullpath.c_str(),&fsstats);
  86. if(rv == 0)
  87. {
  88. fsblkcnt_t spaceavail;
  89. spaceavail = (fsstats.f_frsize * fsstats.f_bavail);
  90. if((spaceavail > minfreespace) &&
  91. (spaceavail < lfs))
  92. {
  93. lfs = spaceavail;
  94. lfsstr = basepath;
  95. }
  96. }
  97. }
  98. if(lfsstr.empty())
  99. return Policy::Func::mfs(type,basepaths,fusepath,minfreespace,paths);
  100. paths.push_back(lfsstr);
  101. return 0;
  102. }
  103. namespace mergerfs
  104. {
  105. int
  106. Policy::Func::lfs(const Category::Enum::Type type,
  107. const vector<string> &basepaths,
  108. const string &fusepath,
  109. const size_t minfreespace,
  110. vector<string> &paths)
  111. {
  112. if(type == Category::Enum::create)
  113. return _lfs_create(type,basepaths,fusepath,minfreespace,paths);
  114. return _lfs(type,basepaths,fusepath,minfreespace,paths);
  115. }
  116. }