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.

98 lines
2.5 KiB

6 years ago
6 years ago
  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 "policy.hpp"
  19. #include "policy_error.hpp"
  20. #include "rwlock.hpp"
  21. #include <limits>
  22. #include <string>
  23. #include <vector>
  24. using std::string;
  25. using std::vector;
  26. namespace lfs
  27. {
  28. static
  29. int
  30. create(const BranchVec &branches_,
  31. vector<string> *paths_)
  32. {
  33. int rv;
  34. int error;
  35. uint64_t lfs;
  36. fs::info_t info;
  37. const Branch *branch;
  38. const string *basepath;
  39. error = ENOENT;
  40. lfs = std::numeric_limits<uint64_t>::max();
  41. basepath = NULL;
  42. for(size_t i = 0, ei = branches_.size(); i != ei; i++)
  43. {
  44. branch = &branches_[i];
  45. if(branch->ro_or_nc())
  46. error_and_continue(error,EROFS);
  47. rv = fs::info(branch->path,&info);
  48. if(rv == -1)
  49. error_and_continue(error,ENOENT);
  50. if(info.readonly)
  51. error_and_continue(error,EROFS);
  52. if(info.spaceavail < branch->minfreespace())
  53. error_and_continue(error,ENOSPC);
  54. if(info.spaceavail > lfs)
  55. continue;
  56. lfs = info.spaceavail;
  57. basepath = &branch->path;
  58. }
  59. if(basepath == NULL)
  60. return (errno=error,-1);
  61. paths_->push_back(*basepath);
  62. return 0;
  63. }
  64. static
  65. int
  66. create(const Branches &branches_,
  67. vector<string> *paths_)
  68. {
  69. rwlock::ReadGuard guard(branches_.lock);
  70. return lfs::create(branches_.vec,paths_);
  71. }
  72. }
  73. int
  74. Policy::Func::lfs(const Category type_,
  75. const Branches &branches_,
  76. const char *fusepath_,
  77. vector<string> *paths_)
  78. {
  79. if(type_ == Category::CREATE)
  80. return lfs::create(branches_,paths_);
  81. return Policy::Func::eplfs(type_,branches_,fusepath_,paths_);
  82. }