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.

145 lines
3.8 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 "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. int
  31. mkdir_core(const string &fullpath_,
  32. mode_t mode_,
  33. const mode_t umask_)
  34. {
  35. if(!fs::acl::dir_has_defaults(fullpath_))
  36. mode_ &= ~umask_;
  37. return fs::mkdir(fullpath_,mode_);
  38. }
  39. static
  40. int
  41. mkdir_loop_core(const string &createpath_,
  42. const char *fusepath_,
  43. const mode_t mode_,
  44. const mode_t umask_,
  45. const int error_)
  46. {
  47. int rv;
  48. string fullpath;
  49. fullpath = fs::path::make(createpath_,fusepath_);
  50. rv = l::mkdir_core(fullpath,mode_,umask_);
  51. return error::calc(rv,error_,errno);
  52. }
  53. static
  54. int
  55. mkdir_loop(const string &existingpath_,
  56. const vector<string> &createpaths_,
  57. const char *fusepath_,
  58. const string &fusedirpath_,
  59. const mode_t mode_,
  60. const mode_t umask_)
  61. {
  62. int rv;
  63. int error;
  64. error = -1;
  65. for(size_t i = 0, ei = createpaths_.size(); i != ei; i++)
  66. {
  67. rv = fs::clonepath_as_root(existingpath_,createpaths_[i],fusedirpath_);
  68. if(rv == -1)
  69. error = error::calc(rv,error,errno);
  70. else
  71. error = l::mkdir_loop_core(createpaths_[i],
  72. fusepath_,
  73. mode_,
  74. umask_,
  75. error);
  76. }
  77. return -error;
  78. }
  79. static
  80. int
  81. mkdir(Policy::Func::Search searchFunc_,
  82. Policy::Func::Create createFunc_,
  83. const Branches &branches_,
  84. const char *fusepath_,
  85. const mode_t mode_,
  86. const mode_t umask_)
  87. {
  88. int rv;
  89. string fusedirpath;
  90. vector<string> createpaths;
  91. vector<string> existingpaths;
  92. fusedirpath = fs::path::dirname(fusepath_);
  93. rv = searchFunc_(branches_,fusedirpath,&existingpaths);
  94. if(rv == -1)
  95. return -errno;
  96. rv = createFunc_(branches_,fusedirpath,&createpaths);
  97. if(rv == -1)
  98. return -errno;
  99. return l::mkdir_loop(existingpaths[0],
  100. createpaths,
  101. fusepath_,
  102. fusedirpath,
  103. mode_,
  104. umask_);
  105. }
  106. }
  107. namespace FUSE
  108. {
  109. int
  110. mkdir(const char *fusepath_,
  111. mode_t mode_)
  112. {
  113. const fuse_context *fc = fuse_get_context();
  114. const Config &config = Config::ro();
  115. const ugid::Set ugid(fc->uid,fc->gid);
  116. return l::mkdir(config.func.getattr.policy,
  117. config.func.mkdir.policy,
  118. config.branches,
  119. fusepath_,
  120. mode_,
  121. fc->umask);
  122. }
  123. }