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.

110 lines
2.5 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2020, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "fuse_readdir.hpp"
  16. #include "config.hpp"
  17. #include "fuse_readdir_factory.hpp"
  18. /*
  19. The _initialized stuff is not pretty but easiest way to deal with
  20. the fact that mergerfs is doing arg parsing and setting up of things
  21. (including thread pools) before the daemonizing
  22. */
  23. int
  24. FUSE::readdir(const fuse_file_info_t *ffi_,
  25. fuse_dirents_t *buf_)
  26. {
  27. Config::Write cfg;
  28. return cfg->readdir(ffi_,buf_);
  29. }
  30. FUSE::ReadDir::ReadDir(std::string const s_)
  31. : _initialized(false)
  32. {
  33. from_string(s_);
  34. if(_initialized)
  35. assert(_readdir);
  36. }
  37. std::string
  38. FUSE::ReadDir::to_string() const
  39. {
  40. std::lock_guard<std::mutex> lg(_mutex);
  41. return _type;
  42. }
  43. void
  44. FUSE::ReadDir::initialize()
  45. {
  46. _initialized = true;
  47. from_string(_type);
  48. }
  49. int
  50. FUSE::ReadDir::from_string(std::string const &str_)
  51. {
  52. if(_initialized)
  53. {
  54. std::shared_ptr<FUSE::ReadDirBase> tmp;
  55. tmp = FUSE::ReadDirFactory::make(str_);
  56. if(!tmp)
  57. return -EINVAL;
  58. {
  59. std::lock_guard<std::mutex> lg(_mutex);
  60. _type = str_;
  61. std::swap(_readdir,tmp);
  62. }
  63. }
  64. else
  65. {
  66. std::lock_guard<std::mutex> lg(_mutex);
  67. if(!FUSE::ReadDirFactory::valid(str_))
  68. return -EINVAL;
  69. _type = str_;
  70. }
  71. return 0;
  72. }
  73. /*
  74. Yeah... if not initialized it will crash... ¯\_()_/¯
  75. This will be resolved once initialization of internal objects and
  76. handling of input is better seperated.
  77. */
  78. int
  79. FUSE::ReadDir::operator()(fuse_file_info_t const *ffi_,
  80. fuse_dirents_t *buf_)
  81. {
  82. std::shared_ptr<FUSE::ReadDirBase> readdir;
  83. {
  84. std::lock_guard<std::mutex> lg(_mutex);
  85. readdir = _readdir;
  86. }
  87. return (*readdir)(ffi_,buf_);
  88. }