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.

132 lines
3.6 KiB

  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <fuse.h>
  21. #include <string>
  22. #include <vector>
  23. #include <sys/types.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include "buildvector.hpp"
  27. #include "category.hpp"
  28. #include "config.hpp"
  29. #include "fs_path.hpp"
  30. #include "rwlock.hpp"
  31. #include "ugid.hpp"
  32. #include "xattr.hpp"
  33. using std::string;
  34. using std::vector;
  35. using namespace mergerfs;
  36. static
  37. int
  38. _listxattr_controlfile(char *list,
  39. const size_t size)
  40. {
  41. string xattrs;
  42. const vector<string> strs =
  43. buildvector<string>
  44. ("user.mergerfs.srcmounts")
  45. ("user.mergerfs.minfreespace")
  46. ("user.mergerfs.moveonenospc")
  47. ("user.mergerfs.policies")
  48. ("user.mergerfs.version");
  49. xattrs.reserve(1024);
  50. for(size_t i = 0; i < strs.size(); i++)
  51. xattrs += (strs[i] + '\0');
  52. for(size_t i = Category::Enum::BEGIN; i < Category::Enum::END; i++)
  53. xattrs += ("user.mergerfs.category." + (std::string)*Category::categories[i] + '\0');
  54. for(size_t i = FuseFunc::Enum::BEGIN; i < FuseFunc::Enum::END; i++)
  55. xattrs += ("user.mergerfs.func." + (std::string)*FuseFunc::fusefuncs[i] + '\0');
  56. if(size == 0)
  57. return xattrs.size();
  58. if(size < xattrs.size())
  59. return -ERANGE;
  60. memcpy(list,xattrs.c_str(),xattrs.size());
  61. return xattrs.size();
  62. }
  63. static
  64. int
  65. _listxattr(Policy::Func::Search searchFunc,
  66. const vector<string> &srcmounts,
  67. const size_t minfreespace,
  68. const string &fusepath,
  69. char *list,
  70. const size_t size)
  71. {
  72. #ifndef WITHOUT_XATTR
  73. int rv;
  74. vector<string> path;
  75. rv = searchFunc(srcmounts,fusepath,minfreespace,path);
  76. if(rv == -1)
  77. return -errno;
  78. fs::path::append(path[0],fusepath);
  79. rv = ::llistxattr(path[0].c_str(),list,size);
  80. return ((rv == -1) ? -errno : rv);
  81. #else
  82. return -ENOTSUP;
  83. #endif
  84. }
  85. namespace mergerfs
  86. {
  87. namespace fuse
  88. {
  89. int
  90. listxattr(const char *fusepath,
  91. char *list,
  92. size_t size)
  93. {
  94. const fuse_context *fc = fuse_get_context();
  95. const Config &config = Config::get(fc);
  96. if(fusepath == config.controlfile)
  97. return _listxattr_controlfile(list,size);
  98. const ugid::Set ugid(fc->uid,fc->gid);
  99. const rwlock::ReadGuard readlock(&config.srcmountslock);
  100. return _listxattr(config.listxattr,
  101. config.srcmounts,
  102. config.minfreespace,
  103. fusepath,
  104. list,
  105. size);
  106. }
  107. }
  108. }