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.

139 lines
3.5 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_lremovexattr.hpp"
  17. #include "fs_path.hpp"
  18. #include "policy_rv.hpp"
  19. #include "ugid.hpp"
  20. #include "fuse.h"
  21. #include <string>
  22. #include <vector>
  23. using std::string;
  24. using std::vector;
  25. namespace l
  26. {
  27. static
  28. int
  29. get_error(const PolicyRV &prv_,
  30. const string &basepath_)
  31. {
  32. for(int i = 0, ei = prv_.success.size(); i < ei; i++)
  33. {
  34. if(prv_.success[i].basepath == basepath_)
  35. return prv_.success[i].rv;
  36. }
  37. for(int i = 0, ei = prv_.error.size(); i < ei; i++)
  38. {
  39. if(prv_.error[i].basepath == basepath_)
  40. return prv_.error[i].rv;
  41. }
  42. return 0;
  43. }
  44. static
  45. void
  46. removexattr_loop_core(const string &basepath_,
  47. const char *fusepath_,
  48. const char *attrname_,
  49. PolicyRV *prv_)
  50. {
  51. string fullpath;
  52. fullpath = fs::path::make(basepath_,fusepath_);
  53. errno = 0;
  54. fs::lremovexattr(fullpath,attrname_);
  55. prv_->insert(errno,basepath_);
  56. }
  57. static
  58. void
  59. removexattr_loop(const vector<string> &basepaths_,
  60. const char *fusepath_,
  61. const char *attrname_,
  62. PolicyRV *prv_)
  63. {
  64. for(size_t i = 0, ei = basepaths_.size(); i != ei; i++)
  65. {
  66. l::removexattr_loop_core(basepaths_[i],fusepath_,attrname_,prv_);
  67. }
  68. }
  69. static
  70. int
  71. removexattr(const Policy::Action &actionFunc_,
  72. const Policy::Search &searchFunc_,
  73. const Branches &branches_,
  74. const char *fusepath_,
  75. const char *attrname_)
  76. {
  77. int rv;
  78. PolicyRV prv;
  79. vector<string> basepaths;
  80. rv = actionFunc_(branches_,fusepath_,&basepaths);
  81. if(rv == -1)
  82. return -errno;
  83. l::removexattr_loop(basepaths,fusepath_,attrname_,&prv);
  84. if(prv.error.empty())
  85. return 0;
  86. if(prv.success.empty())
  87. return prv.error[0].rv;
  88. basepaths.clear();
  89. rv = searchFunc_(branches_,fusepath_,&basepaths);
  90. if(rv == -1)
  91. return -errno;
  92. return l::get_error(prv,basepaths[0]);
  93. }
  94. }
  95. namespace FUSE
  96. {
  97. int
  98. removexattr(const char *fusepath_,
  99. const char *attrname_)
  100. {
  101. Config::Read cfg;
  102. if(fusepath_ == CONTROLFILE)
  103. return -ENOATTR;
  104. if(cfg->xattr.to_int())
  105. return -cfg->xattr.to_int();
  106. const fuse_context *fc = fuse_get_context();
  107. const ugid::Set ugid(fc->uid,fc->gid);
  108. return l::removexattr(cfg->func.removexattr.policy,
  109. cfg->func.getxattr.policy,
  110. cfg->branches,
  111. fusepath_,
  112. attrname_);
  113. }
  114. }