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.

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