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.

190 lines
4.7 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_clonepath.hpp"
  17. #include "fs_lstat.hpp"
  18. #include "fs_path.hpp"
  19. #include "fs_inode.hpp"
  20. #include "fs_symlink.hpp"
  21. #include "fuse_getattr.hpp"
  22. #include "ugid.hpp"
  23. #include "fuse.h"
  24. #include <string>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. using std::string;
  28. namespace error
  29. {
  30. static
  31. inline
  32. int
  33. calc(const int rv_,
  34. const int prev_,
  35. const int cur_)
  36. {
  37. if(rv_ == -1)
  38. {
  39. if(prev_ == 0)
  40. return 0;
  41. return cur_;
  42. }
  43. return 0;
  44. }
  45. }
  46. namespace l
  47. {
  48. static
  49. int
  50. symlink_loop_core(const string &newbasepath_,
  51. const char *target_,
  52. const char *linkpath_,
  53. struct stat *st_,
  54. const int error_)
  55. {
  56. int rv;
  57. string fullnewpath;
  58. fullnewpath = fs::path::make(newbasepath_,linkpath_);
  59. rv = fs::symlink(target_,fullnewpath);
  60. if((rv != -1) && (st_ != NULL) && (st_->st_ino == 0))
  61. {
  62. fs::lstat(fullnewpath,st_);
  63. if(st_->st_ino != 0)
  64. fs::inode::calc(linkpath_,st_);
  65. }
  66. return error::calc(rv,error_,errno);
  67. }
  68. static
  69. int
  70. symlink_loop(const string &existingpath_,
  71. const StrVec &newbasepaths_,
  72. const char *target_,
  73. const char *linkpath_,
  74. const string &newdirpath_,
  75. struct stat *st_)
  76. {
  77. int rv;
  78. int error;
  79. error = -1;
  80. for(size_t i = 0, ei = newbasepaths_.size(); i != ei; i++)
  81. {
  82. rv = fs::clonepath_as_root(existingpath_,newbasepaths_[i],newdirpath_);
  83. if(rv == -1)
  84. error = error::calc(rv,error,errno);
  85. else
  86. error = l::symlink_loop_core(newbasepaths_[i],
  87. target_,
  88. linkpath_,
  89. st_,
  90. error);
  91. }
  92. return -error;
  93. }
  94. static
  95. int
  96. symlink(const Policy::Search &searchFunc_,
  97. const Policy::Create &createFunc_,
  98. const Branches &branches_,
  99. const char *target_,
  100. const char *linkpath_,
  101. struct stat *st_)
  102. {
  103. int rv;
  104. string newdirpath;
  105. StrVec newbasepaths;
  106. StrVec existingpaths;
  107. newdirpath = fs::path::dirname(linkpath_);
  108. rv = searchFunc_(branches_,newdirpath,&existingpaths);
  109. if(rv == -1)
  110. return -errno;
  111. rv = createFunc_(branches_,newdirpath,&newbasepaths);
  112. if(rv == -1)
  113. return -errno;
  114. return l::symlink_loop(existingpaths[0],newbasepaths,
  115. target_,linkpath_,newdirpath,st_);
  116. }
  117. }
  118. namespace FUSE
  119. {
  120. int
  121. symlink(const char *target_,
  122. const char *linkpath_,
  123. struct stat *st_,
  124. fuse_timeouts_t *timeouts_)
  125. {
  126. int rv;
  127. Config::Read cfg;
  128. const fuse_context *fc = fuse_get_context();
  129. const ugid::Set ugid(fc->uid,fc->gid);
  130. rv = l::symlink(cfg->func.getattr.policy,
  131. cfg->func.symlink.policy,
  132. cfg->branches,
  133. target_,
  134. linkpath_,
  135. st_);
  136. if(rv == -EROFS)
  137. {
  138. Config::Write()->branches.find_and_set_mode_ro();
  139. rv = l::symlink(cfg->func.getattr.policy,
  140. cfg->func.symlink.policy,
  141. cfg->branches,
  142. target_,
  143. linkpath_,
  144. st_);
  145. }
  146. if(timeouts_ != NULL)
  147. {
  148. switch(cfg->follow_symlinks)
  149. {
  150. case FollowSymlinks::ENUM::NEVER:
  151. timeouts_->entry = ((rv >= 0) ?
  152. cfg->cache_entry :
  153. cfg->cache_negative_entry);
  154. timeouts_->attr = cfg->cache_attr;
  155. break;
  156. default:
  157. timeouts_->entry = 0;
  158. timeouts_->attr = 0;
  159. break;
  160. }
  161. }
  162. return rv;
  163. }
  164. }