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.

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