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 <fuse.h>
  15. #include <string.h>
  16. #include "config.hpp"
  17. #include "errno.hpp"
  18. #include "fs_base_readlink.hpp"
  19. #include "fs_base_stat.hpp"
  20. #include "fs_path.hpp"
  21. #include "rwlock.hpp"
  22. #include "symlinkify.hpp"
  23. #include "ugid.hpp"
  24. using std::string;
  25. using std::vector;
  26. using mergerfs::Policy;
  27. static
  28. int
  29. _readlink_core_standard(const string &fullpath,
  30. char *buf,
  31. const size_t size)
  32. {
  33. int rv;
  34. rv = fs::readlink(fullpath,buf,size);
  35. if(rv == -1)
  36. return -errno;
  37. buf[rv] = '\0';
  38. return 0;
  39. }
  40. static
  41. int
  42. _readlink_core_symlinkify(const string &fullpath,
  43. char *buf,
  44. const size_t size,
  45. const time_t symlinkify_timeout)
  46. {
  47. int rv;
  48. struct stat st;
  49. rv = fs::stat(fullpath,st);
  50. if(rv == -1)
  51. return -errno;
  52. if(!symlinkify::can_be_symlink(st,symlinkify_timeout))
  53. return _readlink_core_standard(fullpath,buf,size);
  54. strncpy(buf,fullpath.c_str(),size);
  55. return 0;
  56. }
  57. static
  58. int
  59. _readlink_core(const string *basepath,
  60. const char *fusepath,
  61. char *buf,
  62. const size_t size,
  63. const bool symlinkify,
  64. const time_t symlinkify_timeout)
  65. {
  66. string fullpath;
  67. fs::path::make(basepath,fusepath,fullpath);
  68. if(symlinkify)
  69. return _readlink_core_symlinkify(fullpath,buf,size,symlinkify_timeout);
  70. return _readlink_core_standard(fullpath,buf,size);
  71. }
  72. static
  73. int
  74. _readlink(Policy::Func::Search searchFunc,
  75. const Branches &branches_,
  76. const uint64_t minfreespace,
  77. const char *fusepath,
  78. char *buf,
  79. const size_t size,
  80. const bool symlinkify,
  81. const time_t symlinkify_timeout)
  82. {
  83. int rv;
  84. vector<const string*> basepaths;
  85. rv = searchFunc(branches_,fusepath,minfreespace,basepaths);
  86. if(rv == -1)
  87. return -errno;
  88. return _readlink_core(basepaths[0],fusepath,buf,size,
  89. symlinkify,symlinkify_timeout);
  90. }
  91. namespace mergerfs
  92. {
  93. namespace fuse
  94. {
  95. int
  96. readlink(const char *fusepath,
  97. char *buf,
  98. size_t size)
  99. {
  100. const fuse_context *fc = fuse_get_context();
  101. const Config &config = Config::get(fc);
  102. const ugid::Set ugid(fc->uid,fc->gid);
  103. const rwlock::ReadGuard readlock(&config.branches_lock);
  104. return _readlink(config.readlink,
  105. config.branches,
  106. config.minfreespace,
  107. fusepath,
  108. buf,
  109. size,
  110. config.symlinkify,
  111. config.symlinkify_timeout);
  112. }
  113. }
  114. }