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.

103 lines
2.4 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 <fuse.h>
  15. #include <unistd.h>
  16. #include <string>
  17. #include <vector>
  18. #include "config.hpp"
  19. #include "errno.hpp"
  20. #include "fs_base_unlink.hpp"
  21. #include "fs_path.hpp"
  22. #include "rv.hpp"
  23. #include "rwlock.hpp"
  24. #include "ugid.hpp"
  25. using std::string;
  26. using std::vector;
  27. using mergerfs::Policy;
  28. static
  29. int
  30. _unlink_loop_core(const string *basepath,
  31. const char *fusepath,
  32. const int error)
  33. {
  34. int rv;
  35. string fullpath;
  36. fs::path::make(basepath,fusepath,fullpath);
  37. rv = fs::unlink(fullpath);
  38. return error::calc(rv,error,errno);
  39. }
  40. static
  41. int
  42. _unlink_loop(const vector<const string*> &basepaths,
  43. const char *fusepath)
  44. {
  45. int error;
  46. error = -1;
  47. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  48. {
  49. error = _unlink_loop_core(basepaths[i],fusepath,error);
  50. }
  51. return -error;
  52. }
  53. static
  54. int
  55. _unlink(Policy::Func::Action actionFunc,
  56. const Branches &branches_,
  57. const uint64_t minfreespace,
  58. const char *fusepath)
  59. {
  60. int rv;
  61. vector<const string*> basepaths;
  62. rv = actionFunc(branches_,fusepath,minfreespace,basepaths);
  63. if(rv == -1)
  64. return -errno;
  65. return _unlink_loop(basepaths,fusepath);
  66. }
  67. namespace mergerfs
  68. {
  69. namespace fuse
  70. {
  71. int
  72. unlink(const char *fusepath)
  73. {
  74. const fuse_context *fc = fuse_get_context();
  75. const Config &config = Config::get(fc);
  76. const ugid::Set ugid(fc->uid,fc->gid);
  77. const rwlock::ReadGuard readlock(&config.branches_lock);
  78. return _unlink(config.unlink,
  79. config.branches,
  80. config.minfreespace,
  81. fusepath);
  82. }
  83. }
  84. }