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.

122 lines
3.1 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_inode.hpp"
  17. #include "fs_lstat.hpp"
  18. #include "fs_path.hpp"
  19. #include "symlinkify.hpp"
  20. #include "ugid.hpp"
  21. #include <fuse.h>
  22. #include <string>
  23. #include <vector>
  24. using std::string;
  25. using std::vector;
  26. namespace l
  27. {
  28. static
  29. int
  30. getattr_controlfile(struct stat *st_)
  31. {
  32. static const uid_t uid = ::getuid();
  33. static const gid_t gid = ::getgid();
  34. static const time_t now = ::time(NULL);
  35. st_->st_dev = 0;
  36. st_->st_ino = fs::inode::MAGIC;
  37. st_->st_mode = (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
  38. st_->st_nlink = 1;
  39. st_->st_uid = uid;
  40. st_->st_gid = gid;
  41. st_->st_rdev = 0;
  42. st_->st_size = 0;
  43. st_->st_blksize = 512;
  44. st_->st_blocks = 0;
  45. st_->st_atime = now;
  46. st_->st_mtime = now;
  47. st_->st_ctime = now;
  48. return 0;
  49. }
  50. static
  51. int
  52. getattr(Policy::Func::Search searchFunc_,
  53. const Branches &branches_,
  54. const char *fusepath_,
  55. struct stat *st_,
  56. const bool symlinkify_,
  57. const time_t symlinkify_timeout_)
  58. {
  59. int rv;
  60. string fullpath;
  61. vector<string> basepaths;
  62. rv = searchFunc_(branches_,fusepath_,&basepaths);
  63. if(rv == -1)
  64. return -errno;
  65. fullpath = fs::path::make(basepaths[0],fusepath_);
  66. rv = fs::lstat(fullpath,st_);
  67. if(rv == -1)
  68. return -errno;
  69. if(symlinkify_ && symlinkify::can_be_symlink(*st_,symlinkify_timeout_))
  70. st_->st_mode = symlinkify::convert(st_->st_mode);
  71. fs::inode::calc(fusepath_,st_);
  72. return 0;
  73. }
  74. }
  75. namespace FUSE
  76. {
  77. int
  78. getattr(const char *fusepath_,
  79. struct stat *st_,
  80. fuse_timeouts_t *timeout_)
  81. {
  82. int rv;
  83. const Config &config = Config::ro();
  84. if(fusepath_ == config.controlfile)
  85. return l::getattr_controlfile(st_);
  86. const fuse_context *fc = fuse_get_context();
  87. const ugid::Set ugid(fc->uid,fc->gid);
  88. rv = l::getattr(config.func.getattr.policy,
  89. config.branches,
  90. fusepath_,
  91. st_,
  92. config.symlinkify,
  93. config.symlinkify_timeout);
  94. timeout_->entry = ((rv >= 0) ?
  95. config.cache_entry :
  96. config.cache_negative_entry);
  97. timeout_->attr = config.cache_attr;
  98. return rv;
  99. }
  100. }