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.

115 lines
2.5 KiB

  1. /*
  2. Copyright (c) 2019, 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_unlink.hpp"
  18. #include "ugid.hpp"
  19. #include "fuse.h"
  20. #include <string>
  21. #include <vector>
  22. #include <unistd.h>
  23. using std::string;
  24. using std::vector;
  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(prev_ != 0)
  35. return prev_;
  36. if(rv_ == -1)
  37. return cur_;
  38. return 0;
  39. }
  40. }
  41. namespace l
  42. {
  43. static
  44. int
  45. unlink_loop_core(const string &basepath_,
  46. const char *fusepath_,
  47. const int error_)
  48. {
  49. int rv;
  50. string fullpath;
  51. fullpath = fs::path::make(basepath_,fusepath_);
  52. rv = fs::unlink(fullpath);
  53. return error::calc(rv,error_,errno);
  54. }
  55. static
  56. int
  57. unlink_loop(const vector<string> &basepaths_,
  58. const char *fusepath_)
  59. {
  60. int error;
  61. error = 0;
  62. for(size_t i = 0, ei = basepaths_.size(); i != ei; i++)
  63. {
  64. error = l::unlink_loop_core(basepaths_[i],fusepath_,error);
  65. }
  66. return -error;
  67. }
  68. static
  69. int
  70. unlink(const Policy::Action &unlinkPolicy_,
  71. const Branches &branches_,
  72. const char *fusepath_)
  73. {
  74. int rv;
  75. vector<string> basepaths;
  76. rv = unlinkPolicy_(branches_,fusepath_,&basepaths);
  77. if(rv == -1)
  78. return -errno;
  79. return l::unlink_loop(basepaths,fusepath_);
  80. }
  81. }
  82. namespace FUSE
  83. {
  84. int
  85. unlink(const char *fusepath_)
  86. {
  87. Config::Read cfg;
  88. const fuse_context *fc = fuse_get_context();
  89. const ugid::Set ugid(fc->uid,fc->gid);
  90. return l::unlink(cfg->func.unlink.policy,
  91. cfg->branches,
  92. fusepath_);
  93. }
  94. }