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.

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