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.

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