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.

166 lines
4.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 "config.hpp"
  15. #include "errno.hpp"
  16. #include "fileinfo.hpp"
  17. #include "fs_acl.hpp"
  18. #include "fs_base_open.hpp"
  19. #include "fs_clonepath.hpp"
  20. #include "fs_path.hpp"
  21. #include "rwlock.hpp"
  22. #include "ugid.hpp"
  23. #include <fuse.h>
  24. #include <string>
  25. #include <vector>
  26. using std::string;
  27. using std::vector;
  28. typedef Config::CacheFiles CacheFiles;
  29. namespace l
  30. {
  31. static
  32. int
  33. create_core(const string &fullpath_,
  34. mode_t mode_,
  35. const mode_t umask_,
  36. const int flags_)
  37. {
  38. if(!fs::acl::dir_has_defaults(fullpath_))
  39. mode_ &= ~umask_;
  40. return fs::open(fullpath_,flags_,mode_);
  41. }
  42. static
  43. int
  44. create_core(const string &createpath_,
  45. const char *fusepath_,
  46. const mode_t mode_,
  47. const mode_t umask_,
  48. const int flags_,
  49. uint64_t *fh_)
  50. {
  51. int rv;
  52. string fullpath;
  53. fullpath = fs::path::make(createpath_,fusepath_);
  54. rv = l::create_core(fullpath,mode_,umask_,flags_);
  55. if(rv == -1)
  56. return -errno;
  57. *fh_ = reinterpret_cast<uint64_t>(new FileInfo(rv,fusepath_));
  58. return 0;
  59. }
  60. static
  61. int
  62. create(Policy::Func::Search searchFunc_,
  63. Policy::Func::Create createFunc_,
  64. const Branches &branches_,
  65. const uint64_t minfreespace_,
  66. const char *fusepath_,
  67. const mode_t mode_,
  68. const mode_t umask_,
  69. const int flags_,
  70. uint64_t *fh_)
  71. {
  72. int rv;
  73. string fullpath;
  74. string fusedirpath;
  75. vector<const string*> createpaths;
  76. vector<const string*> existingpaths;
  77. fusedirpath = fs::path::dirname(fusepath_);
  78. rv = searchFunc_(branches_,fusedirpath,minfreespace_,existingpaths);
  79. if(rv == -1)
  80. return -errno;
  81. rv = createFunc_(branches_,fusedirpath,minfreespace_,createpaths);
  82. if(rv == -1)
  83. return -errno;
  84. rv = fs::clonepath_as_root(*existingpaths[0],*createpaths[0],fusedirpath);
  85. if(rv == -1)
  86. return -errno;
  87. return l::create_core(*createpaths[0],
  88. fusepath_,
  89. mode_,
  90. umask_,
  91. flags_,
  92. fh_);
  93. }
  94. }
  95. namespace FUSE
  96. {
  97. int
  98. create(const char *fusepath_,
  99. mode_t mode_,
  100. fuse_file_info *ffi_)
  101. {
  102. const fuse_context *fc = fuse_get_context();
  103. const Config &config = Config::get(fc);
  104. const ugid::Set ugid(fc->uid,fc->gid);
  105. const rwlock::ReadGuard readlock(&config.branches_lock);
  106. switch(config.cache_files)
  107. {
  108. case CacheFiles::LIBFUSE:
  109. ffi_->direct_io = config.direct_io;
  110. ffi_->keep_cache = config.kernel_cache;
  111. ffi_->auto_cache = config.auto_cache;
  112. break;
  113. case CacheFiles::OFF:
  114. ffi_->direct_io = 1;
  115. ffi_->keep_cache = 0;
  116. ffi_->auto_cache = 0;
  117. break;
  118. case CacheFiles::PARTIAL:
  119. ffi_->direct_io = 0;
  120. ffi_->keep_cache = 0;
  121. ffi_->auto_cache = 0;
  122. break;
  123. case CacheFiles::FULL:
  124. ffi_->direct_io = 0;
  125. ffi_->keep_cache = 1;
  126. ffi_->auto_cache = 0;
  127. break;
  128. case CacheFiles::AUTO_FULL:
  129. ffi_->direct_io = 0;
  130. ffi_->keep_cache = 0;
  131. ffi_->auto_cache = 1;
  132. break;
  133. }
  134. return l::create(config.getattr,
  135. config.create,
  136. config.branches,
  137. config.minfreespace,
  138. fusepath_,
  139. mode_,
  140. fc->umask,
  141. ffi_->flags,
  142. &ffi_->fh);
  143. }
  144. }