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 <fuse.h>
  15. #include <string>
  16. #include <vector>
  17. #include "config.hpp"
  18. #include "errno.hpp"
  19. #include "fs_base_chmod.hpp"
  20. #include "fs_path.hpp"
  21. #include "rv.hpp"
  22. #include "rwlock.hpp"
  23. #include "ugid.hpp"
  24. using std::string;
  25. using std::vector;
  26. using mergerfs::Policy;
  27. static
  28. int
  29. _chmod_loop_core(const string *basepath,
  30. const char *fusepath,
  31. const mode_t mode,
  32. const int error)
  33. {
  34. int rv;
  35. string fullpath;
  36. fs::path::make(basepath,fusepath,fullpath);
  37. rv = fs::chmod(fullpath,mode);
  38. return error::calc(rv,error,errno);
  39. }
  40. static
  41. int
  42. _chmod_loop(const vector<const string*> &basepaths,
  43. const char *fusepath,
  44. const mode_t mode)
  45. {
  46. int error;
  47. error = -1;
  48. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  49. {
  50. error = _chmod_loop_core(basepaths[i],fusepath,mode,error);
  51. }
  52. return -error;
  53. }
  54. static
  55. int
  56. _chmod(Policy::Func::Action actionFunc,
  57. const Branches &branches_,
  58. const uint64_t minfreespace,
  59. const char *fusepath,
  60. const mode_t mode)
  61. {
  62. int rv;
  63. vector<const string*> basepaths;
  64. rv = actionFunc(branches_,fusepath,minfreespace,basepaths);
  65. if(rv == -1)
  66. return -errno;
  67. return _chmod_loop(basepaths,fusepath,mode);
  68. }
  69. namespace mergerfs
  70. {
  71. namespace fuse
  72. {
  73. int
  74. chmod(const char *fusepath,
  75. mode_t mode)
  76. {
  77. const fuse_context *fc = fuse_get_context();
  78. const Config &config = Config::get(fc);
  79. const ugid::Set ugid(fc->uid,fc->gid);
  80. const rwlock::ReadGuard readlock(&config.branches_lock);
  81. return _chmod(config.chmod,
  82. config.branches,
  83. config.minfreespace,
  84. fusepath,
  85. mode);
  86. }
  87. }
  88. }