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.

135 lines
3.5 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 <sys/stat.h>
  17. #include <sys/types.h>
  18. #include <string>
  19. #include <vector>
  20. #include "config.hpp"
  21. #include "fs_clonepath.hpp"
  22. #include "fs_path.hpp"
  23. #include "rv.hpp"
  24. #include "rwlock.hpp"
  25. #include "ugid.hpp"
  26. using std::string;
  27. using std::vector;
  28. using mergerfs::Policy;
  29. using namespace mergerfs;
  30. static
  31. int
  32. _mkdir_loop_core(const string &existingpath,
  33. const string &createpath,
  34. const char *fusepath,
  35. const char *fusedirpath,
  36. const mode_t mode,
  37. const int error)
  38. {
  39. int rv;
  40. string fullpath;
  41. if(createpath != existingpath)
  42. {
  43. const ugid::SetRootGuard ugidGuard;
  44. fs::clonepath(existingpath,createpath,fusedirpath);
  45. }
  46. fs::path::make(&createpath,fusepath,fullpath);
  47. rv = ::mkdir(fullpath.c_str(),mode);
  48. return calc_error(rv,error,errno);
  49. }
  50. static
  51. int
  52. _mkdir_loop(const string &existingpath,
  53. const vector<const string*> &createpaths,
  54. const char *fusepath,
  55. const char *fusedirpath,
  56. const mode_t mode)
  57. {
  58. int error;
  59. error = -1;
  60. for(size_t i = 0, ei = createpaths.size(); i != ei; i++)
  61. {
  62. error = _mkdir_loop_core(existingpath,*createpaths[i],
  63. fusepath,fusedirpath,mode,error);
  64. }
  65. return -error;
  66. }
  67. static
  68. int
  69. _mkdir(Policy::Func::Search searchFunc,
  70. Policy::Func::Create createFunc,
  71. const vector<string> &srcmounts,
  72. const uint64_t minfreespace,
  73. const char *fusepath,
  74. const mode_t mode)
  75. {
  76. int rv;
  77. string fusedirpath;
  78. const char *fusedirpathcstr;
  79. vector<const string*> createpaths;
  80. vector<const string*> existingpaths;
  81. fusedirpath = fusepath;
  82. fs::path::dirname(fusedirpath);
  83. fusedirpathcstr = fusedirpath.c_str();
  84. rv = searchFunc(srcmounts,fusedirpathcstr,minfreespace,existingpaths);
  85. if(rv == -1)
  86. return -errno;
  87. rv = createFunc(srcmounts,fusedirpathcstr,minfreespace,createpaths);
  88. if(rv == -1)
  89. return -errno;
  90. return _mkdir_loop(*existingpaths[0],createpaths,
  91. fusepath,fusedirpathcstr,mode);
  92. }
  93. namespace mergerfs
  94. {
  95. namespace fuse
  96. {
  97. int
  98. mkdir(const char *fusepath,
  99. mode_t mode)
  100. {
  101. const fuse_context *fc = fuse_get_context();
  102. const Config &config = Config::get(fc);
  103. const ugid::Set ugid(fc->uid,fc->gid);
  104. const rwlock::ReadGuard readlock(&config.srcmountslock);
  105. return _mkdir(config.getattr,
  106. config.mkdir,
  107. config.srcmounts,
  108. config.minfreespace,
  109. fusepath,
  110. (mode & ~fc->umask));
  111. }
  112. }
  113. }