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.

134 lines
3.4 KiB

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 <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. int
  31. _create_core(const string &fullpath,
  32. mode_t mode,
  33. const mode_t umask,
  34. const int flags)
  35. {
  36. if(!fs::acl::dir_has_defaults(fullpath))
  37. mode &= ~umask;
  38. return fs::open(fullpath,flags,mode);
  39. }
  40. static
  41. int
  42. _create_core(const string &createpath,
  43. const char *fusepath,
  44. const mode_t mode,
  45. const mode_t umask,
  46. const int flags,
  47. uint64_t &fh)
  48. {
  49. int rv;
  50. string fullpath;
  51. fs::path::make(&createpath,fusepath,fullpath);
  52. rv = _create_core(fullpath,mode,umask,flags);
  53. if(rv == -1)
  54. return -errno;
  55. fh = reinterpret_cast<uint64_t>(new FileInfo(rv,fusepath));
  56. return 0;
  57. }
  58. static
  59. int
  60. _create(Policy::Func::Search searchFunc,
  61. Policy::Func::Create createFunc,
  62. const Branches &branches_,
  63. const uint64_t minfreespace,
  64. const char *fusepath,
  65. const mode_t mode,
  66. const mode_t umask,
  67. const int flags,
  68. uint64_t &fh)
  69. {
  70. int rv;
  71. string fullpath;
  72. string fusedirpath;
  73. vector<const string*> createpaths;
  74. vector<const string*> existingpaths;
  75. fusedirpath = fs::path::dirname(fusepath);
  76. rv = searchFunc(branches_,fusedirpath,minfreespace,existingpaths);
  77. if(rv == -1)
  78. return -errno;
  79. rv = createFunc(branches_,fusedirpath,minfreespace,createpaths);
  80. if(rv == -1)
  81. return -errno;
  82. rv = fs::clonepath_as_root(*existingpaths[0],*createpaths[0],fusedirpath);
  83. if(rv == -1)
  84. return -errno;
  85. return _create_core(*createpaths[0],
  86. fusepath,
  87. mode,umask,flags,fh);
  88. }
  89. namespace mergerfs
  90. {
  91. namespace fuse
  92. {
  93. int
  94. create(const char *fusepath,
  95. mode_t mode,
  96. fuse_file_info *ffi)
  97. {
  98. const fuse_context *fc = fuse_get_context();
  99. const Config &config = Config::get(fc);
  100. const ugid::Set ugid(fc->uid,fc->gid);
  101. const rwlock::ReadGuard readlock(&config.branches_lock);
  102. return _create(config.getattr,
  103. config.create,
  104. config.branches,
  105. config.minfreespace,
  106. fusepath,
  107. mode,
  108. fc->umask,
  109. ffi->flags,
  110. ffi->fh);
  111. }
  112. }
  113. }