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.

97 lines
2.5 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. static
  23. int
  24. _all_create(const vector<string> &basepaths,
  25. const uint64_t minfreespace,
  26. vector<const string*> &paths)
  27. {
  28. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  29. {
  30. bool readonly;
  31. uint64_t spaceavail;
  32. uint64_t _spaceused;
  33. const string *basepath = &basepaths[i];
  34. if(!fs::info(*basepath,readonly,spaceavail,_spaceused))
  35. continue;
  36. if(readonly)
  37. continue;
  38. if(spaceavail < minfreespace)
  39. continue;
  40. paths.push_back(basepath);
  41. }
  42. if(paths.empty())
  43. return POLICY_FAIL_ENOENT;
  44. return POLICY_SUCCESS;
  45. }
  46. static
  47. int
  48. _all_other(const vector<string> &basepaths,
  49. const char *fusepath,
  50. vector<const string*> &paths)
  51. {
  52. string fullpath;
  53. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  54. {
  55. const string *basepath = &basepaths[i];
  56. fs::path::make(basepath,fusepath,fullpath);
  57. if(!fs::exists(fullpath))
  58. continue;
  59. paths.push_back(basepath);
  60. }
  61. if(paths.empty())
  62. return POLICY_FAIL_ENOENT;
  63. return POLICY_SUCCESS;
  64. }
  65. namespace mergerfs
  66. {
  67. int
  68. Policy::Func::all(const Category::Enum::Type type,
  69. const vector<string> &basepaths,
  70. const char *fusepath,
  71. const uint64_t minfreespace,
  72. vector<const string*> &paths)
  73. {
  74. if(type == Category::Enum::create)
  75. return _all_create(basepaths,minfreespace,paths);
  76. return _all_other(basepaths,fusepath,paths);
  77. }
  78. }