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.

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