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.

127 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 "errno.hpp"
  15. #include "fs_exists.hpp"
  16. #include "fs_info.hpp"
  17. #include "fs_path.hpp"
  18. #include "fs_statvfs_cache.hpp"
  19. #include "policies.hpp"
  20. #include "policy.hpp"
  21. #include "policy_error.hpp"
  22. #include "policy_mspmfs.hpp"
  23. #include <limits>
  24. #include <string>
  25. #include <vector>
  26. using std::string;
  27. using std::vector;
  28. namespace mspmfs
  29. {
  30. static
  31. const
  32. string*
  33. create_1(const Branches::CPtr &branches_,
  34. const string &fusepath_,
  35. int *err_)
  36. {
  37. int rv;
  38. uint64_t mfs;
  39. fs::info_t info;
  40. const string *basepath;
  41. basepath = NULL;
  42. mfs = std::numeric_limits<uint64_t>::min();
  43. for(const auto &branch : *branches_)
  44. {
  45. if(branch.ro_or_nc())
  46. error_and_continue(*err_,EROFS);
  47. if(!fs::exists(branch.path,fusepath_))
  48. error_and_continue(*err_,ENOENT);
  49. rv = fs::info(branch.path,&info);
  50. if(rv == -1)
  51. error_and_continue(*err_,ENOENT);
  52. if(info.readonly)
  53. error_and_continue(*err_,EROFS);
  54. if(info.spaceavail < branch.minfreespace())
  55. error_and_continue(*err_,ENOSPC);
  56. if(info.spaceavail < mfs)
  57. continue;
  58. mfs = info.spaceavail;
  59. basepath = &branch.path;
  60. }
  61. return basepath;
  62. }
  63. static
  64. int
  65. create(const Branches::CPtr &branches_,
  66. const char *fusepath_,
  67. StrVec *paths_)
  68. {
  69. int error;
  70. string fusepath;
  71. const string *basepath;
  72. error = ENOENT;
  73. fusepath = fusepath_;
  74. for(;;)
  75. {
  76. basepath = mspmfs::create_1(branches_,fusepath,&error);
  77. if(basepath)
  78. break;
  79. if(fusepath == "/")
  80. break;
  81. fusepath = fs::path::dirname(fusepath);
  82. }
  83. if(basepath == NULL)
  84. return (errno=error,-1);
  85. paths_->push_back(*basepath);
  86. return 0;
  87. }
  88. }
  89. int
  90. Policy::MSPMFS::Action::operator()(const Branches::CPtr &branches_,
  91. const char *fusepath_,
  92. StrVec *paths_) const
  93. {
  94. return Policies::Action::epmfs(branches_,fusepath_,paths_);
  95. }
  96. int
  97. Policy::MSPMFS::Create::operator()(const Branches::CPtr &branches_,
  98. const char *fusepath_,
  99. StrVec *paths_) const
  100. {
  101. return ::mspmfs::create(branches_,fusepath_,paths_);
  102. }
  103. int
  104. Policy::MSPMFS::Search::operator()(const Branches::CPtr &branches_,
  105. const char *fusepath_,
  106. StrVec *paths_) const
  107. {
  108. return Policies::Search::epmfs(branches_,fusepath_,paths_);
  109. }