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.

134 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_lstat.hpp"
  17. #include "fs_path.hpp"
  18. #include "fs_readlink.hpp"
  19. #include "symlinkify.hpp"
  20. #include "ugid.hpp"
  21. #include "fuse.h"
  22. #include <string.h>
  23. using std::string;
  24. namespace l
  25. {
  26. static
  27. int
  28. readlink_core_standard(const string &fullpath_,
  29. char *buf_,
  30. const size_t size_)
  31. {
  32. int rv;
  33. rv = fs::readlink(fullpath_,buf_,size_);
  34. if(rv == -1)
  35. return -errno;
  36. buf_[rv] = '\0';
  37. return 0;
  38. }
  39. static
  40. int
  41. readlink_core_symlinkify(const string &fullpath_,
  42. char *buf_,
  43. const size_t size_,
  44. const time_t symlinkify_timeout_)
  45. {
  46. int rv;
  47. struct stat st;
  48. rv = fs::lstat(fullpath_,&st);
  49. if(rv == -1)
  50. return -errno;
  51. if(!symlinkify::can_be_symlink(st,symlinkify_timeout_))
  52. return l::readlink_core_standard(fullpath_,buf_,size_);
  53. strncpy(buf_,fullpath_.c_str(),size_);
  54. return 0;
  55. }
  56. static
  57. int
  58. readlink_core(const string &basepath_,
  59. const char *fusepath_,
  60. char *buf_,
  61. const size_t size_,
  62. const bool symlinkify_,
  63. const time_t symlinkify_timeout_)
  64. {
  65. string fullpath;
  66. fullpath = fs::path::make(basepath_,fusepath_);
  67. if(symlinkify_)
  68. return l::readlink_core_symlinkify(fullpath,buf_,size_,symlinkify_timeout_);
  69. return l::readlink_core_standard(fullpath,buf_,size_);
  70. }
  71. static
  72. int
  73. readlink(const Policy::Search &searchFunc_,
  74. const Branches &branches_,
  75. const char *fusepath_,
  76. char *buf_,
  77. const size_t size_,
  78. const bool symlinkify_,
  79. const time_t symlinkify_timeout_)
  80. {
  81. int rv;
  82. StrVec basepaths;
  83. rv = searchFunc_(branches_,fusepath_,&basepaths);
  84. if(rv == -1)
  85. return -errno;
  86. return l::readlink_core(basepaths[0],fusepath_,buf_,size_,
  87. symlinkify_,symlinkify_timeout_);
  88. }
  89. }
  90. namespace FUSE
  91. {
  92. int
  93. readlink(const char *fusepath_,
  94. char *buf_,
  95. size_t size_)
  96. {
  97. Config::Read cfg;
  98. const fuse_context *fc = fuse_get_context();
  99. const ugid::Set ugid(fc->uid,fc->gid);
  100. return l::readlink(cfg->func.readlink.policy,
  101. cfg->branches,
  102. fusepath_,
  103. buf_,
  104. size_,
  105. cfg->symlinkify,
  106. cfg->symlinkify_timeout);
  107. }
  108. }