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.

152 lines
4.0 KiB

9 months ago
9 months ago
9 months ago
9 months 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 "config.hpp"
  15. #include "errno.hpp"
  16. #include "fileinfo.hpp"
  17. #include "fs_acl.hpp"
  18. #include "fs_clonepath.hpp"
  19. #include "fs_open.hpp"
  20. #include "fs_path.hpp"
  21. #include "ugid.hpp"
  22. #include "func_open_create_utils.hpp"
  23. #include "fuse.h"
  24. #include <string>
  25. #include <vector>
  26. namespace l
  27. {
  28. static
  29. int
  30. create_core(const std::string &fullpath_,
  31. mode_t mode_,
  32. const mode_t umask_,
  33. const int flags_)
  34. {
  35. if(!fs::acl::dir_has_defaults(fullpath_))
  36. mode_ &= ~umask_;
  37. return fs::open(fullpath_,flags_,mode_);
  38. }
  39. static
  40. int
  41. create_core(const std::string &createpath_,
  42. const char *fusepath_,
  43. fuse_file_info_t *ffi_,
  44. const mode_t mode_,
  45. const mode_t umask_)
  46. {
  47. int rv;
  48. FileInfo *fi;
  49. std::string fullpath;
  50. fullpath = fs::path::make(createpath_,fusepath_);
  51. rv = l::create_core(fullpath,mode_,umask_,ffi_->flags);
  52. if(rv == -1)
  53. return -errno;
  54. fi = new FileInfo(rv,fusepath_,ffi_->direct_io);
  55. ffi_->fh = reinterpret_cast<uint64_t>(fi);
  56. return 0;
  57. }
  58. static
  59. int
  60. create(const Policy::Search &searchFunc_,
  61. const Policy::Create &createFunc_,
  62. const Branches &branches_,
  63. const char *fusepath_,
  64. fuse_file_info_t *ffi_,
  65. const mode_t mode_,
  66. const mode_t umask_)
  67. {
  68. int rv;
  69. std::string fullpath;
  70. std::string fusedirpath;
  71. StrVec createpaths;
  72. StrVec existingpaths;
  73. fusedirpath = fs::path::dirname(fusepath_);
  74. rv = searchFunc_(branches_,fusedirpath,&existingpaths);
  75. if(rv == -1)
  76. return -errno;
  77. rv = createFunc_(branches_,fusedirpath,&createpaths);
  78. if(rv == -1)
  79. return -errno;
  80. rv = fs::clonepath_as_root(existingpaths[0],createpaths[0],fusedirpath);
  81. if(rv == -1)
  82. return -errno;
  83. return l::create_core(createpaths[0],
  84. fusepath_,
  85. ffi_,
  86. mode_,
  87. umask_);
  88. }
  89. }
  90. namespace FUSE
  91. {
  92. int
  93. create(const char *fusepath_,
  94. mode_t mode_,
  95. fuse_file_info_t *ffi_)
  96. {
  97. int rv;
  98. Config::Read cfg;
  99. const fuse_context *fc = fuse_get_context();
  100. const ugid::Set ugid(fc->uid,fc->gid);
  101. utils::cfg_to_ffi_flags(cfg,fc->pid,ffi_);
  102. if(cfg->writeback_cache)
  103. utils::tweak_flags_writeback_cache(&ffi_->flags);
  104. ffi_->noflush = !utils::calculate_flush(cfg->flushonclose,
  105. ffi_->flags);
  106. rv = l::create(cfg->func.getattr.policy,
  107. cfg->func.create.policy,
  108. cfg->branches,
  109. fusepath_,
  110. ffi_,
  111. mode_,
  112. fc->umask);
  113. if(rv == -EROFS)
  114. {
  115. Config::Write()->branches.find_and_set_mode_ro();
  116. rv = l::create(cfg->func.getattr.policy,
  117. cfg->func.create.policy,
  118. cfg->branches,
  119. fusepath_,
  120. ffi_,
  121. mode_,
  122. fc->umask);
  123. }
  124. return rv;
  125. }
  126. }