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.

149 lines
4.0 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 "fs_acl.hpp"
  17. #include "fs_base_mknod.hpp"
  18. #include "fs_clonepath.hpp"
  19. #include "fs_path.hpp"
  20. #include "rv.hpp"
  21. #include "ugid.hpp"
  22. #include <fuse.h>
  23. #include <string>
  24. #include <vector>
  25. using std::string;
  26. using std::vector;
  27. namespace l
  28. {
  29. static
  30. inline
  31. int
  32. mknod_core(const string &fullpath_,
  33. mode_t mode_,
  34. const mode_t umask_,
  35. const dev_t dev_)
  36. {
  37. if(!fs::acl::dir_has_defaults(fullpath_))
  38. mode_ &= ~umask_;
  39. return fs::mknod(fullpath_,mode_,dev_);
  40. }
  41. static
  42. int
  43. mknod_loop_core(const string &createpath_,
  44. const char *fusepath_,
  45. const mode_t mode_,
  46. const mode_t umask_,
  47. const dev_t dev_,
  48. const int error_)
  49. {
  50. int rv;
  51. string fullpath;
  52. fullpath = fs::path::make(&createpath_,fusepath_);
  53. rv = l::mknod_core(fullpath,mode_,umask_,dev_);
  54. return error::calc(rv,error_,errno);
  55. }
  56. static
  57. int
  58. mknod_loop(const string &existingpath_,
  59. const vector<string> &createpaths_,
  60. const char *fusepath_,
  61. const string &fusedirpath_,
  62. const mode_t mode_,
  63. const mode_t umask_,
  64. const dev_t dev_)
  65. {
  66. int rv;
  67. int error;
  68. error = -1;
  69. for(size_t i = 0, ei = createpaths_.size(); i != ei; i++)
  70. {
  71. rv = fs::clonepath_as_root(existingpath_,createpaths_[i],fusedirpath_);
  72. if(rv == -1)
  73. error = error::calc(rv,error,errno);
  74. else
  75. error = l::mknod_loop_core(createpaths_[i],
  76. fusepath_,
  77. mode_,umask_,dev_,error);
  78. }
  79. return -error;
  80. }
  81. static
  82. int
  83. mknod(Policy::Func::Search searchFunc_,
  84. Policy::Func::Create createFunc_,
  85. const Branches &branches_,
  86. const uint64_t minfreespace_,
  87. const char *fusepath_,
  88. const mode_t mode_,
  89. const mode_t umask_,
  90. const dev_t dev_)
  91. {
  92. int rv;
  93. string fusedirpath;
  94. vector<string> createpaths;
  95. vector<string> existingpaths;
  96. fusedirpath = fs::path::dirname(fusepath_);
  97. rv = searchFunc_(branches_,fusedirpath,minfreespace_,&existingpaths);
  98. if(rv == -1)
  99. return -errno;
  100. rv = createFunc_(branches_,fusedirpath,minfreespace_,&createpaths);
  101. if(rv == -1)
  102. return -errno;
  103. return l::mknod_loop(existingpaths[0],createpaths,
  104. fusepath_,fusedirpath,
  105. mode_,umask_,dev_);
  106. }
  107. }
  108. namespace FUSE
  109. {
  110. int
  111. mknod(const char *fusepath_,
  112. mode_t mode_,
  113. dev_t rdev_)
  114. {
  115. const fuse_context *fc = fuse_get_context();
  116. const Config &config = Config::ro();
  117. const ugid::Set ugid(fc->uid,fc->gid);
  118. return l::mknod(config.func.getattr.policy,
  119. config.func.mknod.policy,
  120. config.branches,
  121. config.minfreespace,
  122. fusepath_,
  123. mode_,
  124. fc->umask,
  125. rdev_);
  126. }
  127. }