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.

127 lines
3.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 "config.hpp"
  15. #include "errno.hpp"
  16. #include "fs_base_symlink.hpp"
  17. #include "fs_clonepath.hpp"
  18. #include "fs_path.hpp"
  19. #include "rv.hpp"
  20. #include "ugid.hpp"
  21. #include <fuse.h>
  22. #include <string>
  23. #include <sys/types.h>
  24. #include <unistd.h>
  25. using std::string;
  26. using std::vector;
  27. namespace l
  28. {
  29. static
  30. int
  31. symlink_loop_core(const string &newbasepath_,
  32. const char *oldpath_,
  33. const char *newpath_,
  34. const int error_)
  35. {
  36. int rv;
  37. string fullnewpath;
  38. fullnewpath = fs::path::make(&newbasepath_,newpath_);
  39. rv = fs::symlink(oldpath_,fullnewpath);
  40. return error::calc(rv,error_,errno);
  41. }
  42. static
  43. int
  44. symlink_loop(const string &existingpath_,
  45. const vector<string> &newbasepaths_,
  46. const char *oldpath_,
  47. const char *newpath_,
  48. const string &newdirpath_)
  49. {
  50. int rv;
  51. int error;
  52. error = -1;
  53. for(size_t i = 0, ei = newbasepaths_.size(); i != ei; i++)
  54. {
  55. rv = fs::clonepath_as_root(existingpath_,newbasepaths_[i],newdirpath_);
  56. if(rv == -1)
  57. error = error::calc(rv,error,errno);
  58. else
  59. error = l::symlink_loop_core(newbasepaths_[i],
  60. oldpath_,
  61. newpath_,
  62. error);
  63. }
  64. return -error;
  65. }
  66. static
  67. int
  68. symlink(Policy::Func::Search searchFunc_,
  69. Policy::Func::Create createFunc_,
  70. const Branches &branches_,
  71. const uint64_t minfreespace_,
  72. const char *oldpath_,
  73. const char *newpath_)
  74. {
  75. int rv;
  76. string newdirpath;
  77. vector<string> newbasepaths;
  78. vector<string> existingpaths;
  79. newdirpath = fs::path::dirname(newpath_);
  80. rv = searchFunc_(branches_,newdirpath,minfreespace_,&existingpaths);
  81. if(rv == -1)
  82. return -errno;
  83. rv = createFunc_(branches_,newdirpath,minfreespace_,&newbasepaths);
  84. if(rv == -1)
  85. return -errno;
  86. return l::symlink_loop(existingpaths[0],newbasepaths,
  87. oldpath_,newpath_,newdirpath);
  88. }
  89. }
  90. namespace FUSE
  91. {
  92. int
  93. symlink(const char *oldpath_,
  94. const char *newpath_)
  95. {
  96. const fuse_context *fc = fuse_get_context();
  97. const Config &config = Config::ro();
  98. const ugid::Set ugid(fc->uid,fc->gid);
  99. return l::symlink(config.func.getattr.policy,
  100. config.func.symlink.policy,
  101. config.branches,
  102. config.minfreespace,
  103. oldpath_,
  104. newpath_);
  105. }
  106. }