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.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 <fuse.h>
  15. #include <string>
  16. #include <vector>
  17. #include <errno.h>
  18. #include <sys/ioctl.h>
  19. #include <linux/fs.h>
  20. #include "config.hpp"
  21. #include "fileinfo.hpp"
  22. #include "fs_path.hpp"
  23. #include "rwlock.hpp"
  24. #include "ugid.hpp"
  25. using std::string;
  26. using std::vector;
  27. using namespace mergerfs;
  28. static
  29. int
  30. _ioctl(const int fd,
  31. const int cmd,
  32. void *data)
  33. {
  34. int rv;
  35. rv = ::ioctl(fd,cmd,data);
  36. return ((rv == -1) ? -errno : rv);
  37. }
  38. #ifdef FUSE_IOCTL_DIR
  39. #ifndef O_DIRECTORY
  40. #define O_DIRECTORY 0
  41. #endif
  42. #ifndef O_NOATIME
  43. #define O_NOATIME 0
  44. #endif
  45. static
  46. int
  47. _ioctl_dir_base(Policy::Func::Search searchFunc,
  48. const vector<string> &srcmounts,
  49. const size_t minfreespace,
  50. const string &fusepath,
  51. const int cmd,
  52. void *data)
  53. {
  54. int fd;
  55. int rv;
  56. vector<string> path;
  57. rv = searchFunc(srcmounts,fusepath,minfreespace,path);
  58. if(rv == -1)
  59. return -errno;
  60. fs::path::append(path[0],fusepath);
  61. fd = ::open(path[0].c_str(),O_RDWR|O_NOATIME|O_DIRECTORY);
  62. if(fd == -1)
  63. return -errno;
  64. rv = _ioctl(fd,cmd,data);
  65. ::close(fd);
  66. return rv;
  67. }
  68. static
  69. int
  70. _ioctl_dir(const string &fusepath,
  71. const int cmd,
  72. void *data)
  73. {
  74. const fuse_context *fc = fuse_get_context();
  75. const Config &config = Config::get(fc);
  76. const ugid::Set ugid(fc->uid,fc->gid);
  77. const rwlock::ReadGuard readlock(&config.srcmountslock);
  78. return _ioctl_dir_base(config.getattr,
  79. config.srcmounts,
  80. config.minfreespace,
  81. fusepath,
  82. cmd,
  83. data);
  84. }
  85. #endif
  86. namespace mergerfs
  87. {
  88. namespace fuse
  89. {
  90. int
  91. ioctl(const char *fusepath,
  92. int cmd,
  93. void *arg,
  94. fuse_file_info *ffi,
  95. unsigned int flags,
  96. void *data)
  97. {
  98. #ifdef FUSE_IOCTL_DIR
  99. if(flags & FUSE_IOCTL_DIR)
  100. return _ioctl_dir(fusepath,
  101. cmd,
  102. data);
  103. #endif
  104. FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);
  105. return _ioctl(fi->fd,
  106. cmd,
  107. data);
  108. }
  109. }
  110. }