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.

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