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.

131 lines
3.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_path.hpp"
  17. #include "fs_rmdir.hpp"
  18. #include "fs_unlink.hpp"
  19. #include "ugid.hpp"
  20. #include "fuse.h"
  21. #include <string>
  22. #include <unistd.h>
  23. using std::string;
  24. using std::vector;
  25. namespace error
  26. {
  27. static
  28. int
  29. calc(const int rv_,
  30. const int prev_,
  31. const int cur_)
  32. {
  33. if(prev_ != 0)
  34. return prev_;
  35. if(rv_ == -1)
  36. return cur_;
  37. return 0;
  38. }
  39. }
  40. namespace l
  41. {
  42. static
  43. int
  44. should_unlink(int rv_,
  45. int errno_,
  46. FollowSymlinks followsymlinks_)
  47. {
  48. return ((rv_ == -1) &&
  49. (errno_ == ENOTDIR) &&
  50. (followsymlinks_ != FollowSymlinks::ENUM::NEVER));
  51. }
  52. static
  53. int
  54. rmdir_core(const string &basepath_,
  55. const char *fusepath_,
  56. const FollowSymlinks followsymlinks_,
  57. const int error_)
  58. {
  59. int rv;
  60. string fullpath;
  61. fullpath = fs::path::make(basepath_,fusepath_);
  62. rv = fs::rmdir(fullpath);
  63. if(l::should_unlink(rv,errno,followsymlinks_))
  64. rv = fs::unlink(fullpath);
  65. return error::calc(rv,error_,errno);
  66. }
  67. static
  68. int
  69. rmdir_loop(const StrVec &basepaths_,
  70. const char *fusepath_,
  71. const FollowSymlinks followsymlinks_)
  72. {
  73. int error;
  74. error = 0;
  75. for(size_t i = 0, ei = basepaths_.size(); i != ei; i++)
  76. {
  77. error = l::rmdir_core(basepaths_[i],fusepath_,followsymlinks_,error);
  78. }
  79. return -error;
  80. }
  81. static
  82. int
  83. rmdir(const Policy::Action &actionFunc_,
  84. const Branches &branches_,
  85. const FollowSymlinks followsymlinks_,
  86. const char *fusepath_)
  87. {
  88. int rv;
  89. vector<string> basepaths;
  90. rv = actionFunc_(branches_,fusepath_,&basepaths);
  91. if(rv == -1)
  92. return -errno;
  93. return l::rmdir_loop(basepaths,fusepath_,followsymlinks_);
  94. }
  95. }
  96. namespace FUSE
  97. {
  98. int
  99. rmdir(const char *fusepath_)
  100. {
  101. Config::Read cfg;
  102. const fuse_context *fc = fuse_get_context();
  103. const ugid::Set ugid(fc->uid,fc->gid);
  104. return l::rmdir(cfg->func.rmdir.policy,
  105. cfg->branches,
  106. cfg->follow_symlinks,
  107. fusepath_);
  108. }
  109. }