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.

144 lines
3.7 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 "fileinfo.hpp"
  20. #include "fs_acl.hpp"
  21. #include "fs_base_open.hpp"
  22. #include "fs_clonepath.hpp"
  23. #include "fs_path.hpp"
  24. #include "rwlock.hpp"
  25. #include "ugid.hpp"
  26. using std::string;
  27. using std::vector;
  28. using namespace mergerfs;
  29. static
  30. inline
  31. int
  32. _create_core(const string &fullpath,
  33. mode_t mode,
  34. const mode_t umask,
  35. const int flags)
  36. {
  37. if(!fs::acl::dir_has_defaults(fullpath))
  38. mode &= ~umask;
  39. return fs::open(fullpath,flags,mode);
  40. }
  41. static
  42. int
  43. _create_core(const string &existingpath,
  44. const string &createpath,
  45. const char *fusepath,
  46. const char *fusedirpath,
  47. const mode_t mode,
  48. const mode_t umask,
  49. const int flags,
  50. uint64_t &fh)
  51. {
  52. int rv;
  53. string fullpath;
  54. if(createpath != existingpath)
  55. {
  56. const ugid::SetRootGuard ugidGuard;
  57. rv = fs::clonepath(existingpath,createpath,fusedirpath);
  58. if(rv == -1)
  59. return -errno;
  60. }
  61. fs::path::make(&createpath,fusepath,fullpath);
  62. rv = _create_core(fullpath,mode,umask,flags);
  63. if(rv == -1)
  64. return -errno;
  65. fh = reinterpret_cast<uint64_t>(new FileInfo(rv));
  66. return 0;
  67. }
  68. static
  69. int
  70. _create(Policy::Func::Search searchFunc,
  71. Policy::Func::Create createFunc,
  72. const vector<string> &srcmounts,
  73. const uint64_t minfreespace,
  74. const char *fusepath,
  75. const mode_t mode,
  76. const mode_t umask,
  77. const int flags,
  78. uint64_t &fh)
  79. {
  80. int rv;
  81. string fullpath;
  82. string fusedirpath;
  83. const char *fusedirpathcstr;
  84. vector<const string*> createpaths;
  85. vector<const string*> existingpaths;
  86. fusedirpath = fusepath;
  87. fs::path::dirname(fusedirpath);
  88. fusedirpathcstr = fusedirpath.c_str();
  89. rv = searchFunc(srcmounts,fusedirpathcstr,minfreespace,existingpaths);
  90. if(rv == -1)
  91. return -errno;
  92. rv = createFunc(srcmounts,fusedirpathcstr,minfreespace,createpaths);
  93. if(rv == -1)
  94. return -errno;
  95. return _create_core(*existingpaths[0],*createpaths[0],
  96. fusepath,fusedirpathcstr,
  97. mode,umask,flags,fh);
  98. }
  99. namespace mergerfs
  100. {
  101. namespace fuse
  102. {
  103. int
  104. create(const char *fusepath,
  105. mode_t mode,
  106. fuse_file_info *ffi)
  107. {
  108. const fuse_context *fc = fuse_get_context();
  109. const Config &config = Config::get(fc);
  110. const ugid::Set ugid(fc->uid,fc->gid);
  111. const rwlock::ReadGuard readlock(&config.srcmountslock);
  112. return _create(config.getattr,
  113. config.create,
  114. config.srcmounts,
  115. config.minfreespace,
  116. fusepath,
  117. mode,
  118. fc->umask,
  119. ffi->flags,
  120. ffi->fh);
  121. }
  122. }
  123. }