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.3 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 <string.h>
  18. #include "buildvector.hpp"
  19. #include "category.hpp"
  20. #include "config.hpp"
  21. #include "errno.hpp"
  22. #include "fs_base_listxattr.hpp"
  23. #include "fs_path.hpp"
  24. #include "rwlock.hpp"
  25. #include "ugid.hpp"
  26. #include "xattr.hpp"
  27. using std::string;
  28. using std::vector;
  29. using namespace mergerfs;
  30. static
  31. int
  32. _listxattr_controlfile(char *list,
  33. const size_t size)
  34. {
  35. string xattrs;
  36. const vector<string> strs =
  37. buildvector<string>
  38. ("user.mergerfs.srcmounts")
  39. ("user.mergerfs.minfreespace")
  40. ("user.mergerfs.moveonenospc")
  41. ("user.mergerfs.policies")
  42. ("user.mergerfs.version")
  43. ("user.mergerfs.pid");
  44. xattrs.reserve(1024);
  45. for(size_t i = 0; i < strs.size(); i++)
  46. xattrs += (strs[i] + '\0');
  47. for(size_t i = Category::Enum::BEGIN; i < Category::Enum::END; i++)
  48. xattrs += ("user.mergerfs.category." + (std::string)*Category::categories[i] + '\0');
  49. for(size_t i = FuseFunc::Enum::BEGIN; i < FuseFunc::Enum::END; i++)
  50. xattrs += ("user.mergerfs.func." + (std::string)*FuseFunc::fusefuncs[i] + '\0');
  51. if(size == 0)
  52. return xattrs.size();
  53. if(size < xattrs.size())
  54. return -ERANGE;
  55. memcpy(list,xattrs.c_str(),xattrs.size());
  56. return xattrs.size();
  57. }
  58. static
  59. int
  60. _listxattr(Policy::Func::Search searchFunc,
  61. const vector<string> &srcmounts,
  62. const uint64_t minfreespace,
  63. const char *fusepath,
  64. char *list,
  65. const size_t size)
  66. {
  67. int rv;
  68. string fullpath;
  69. vector<const string*> basepaths;
  70. rv = searchFunc(srcmounts,fusepath,minfreespace,basepaths);
  71. if(rv == -1)
  72. return -errno;
  73. fs::path::make(basepaths[0],fusepath,fullpath);
  74. rv = fs::llistxattr(fullpath,list,size);
  75. return ((rv == -1) ? -errno : rv);
  76. }
  77. namespace mergerfs
  78. {
  79. namespace fuse
  80. {
  81. int
  82. listxattr(const char *fusepath,
  83. char *list,
  84. size_t size)
  85. {
  86. const fuse_context *fc = fuse_get_context();
  87. const Config &config = Config::get(fc);
  88. if(fusepath == config.controlfile)
  89. return _listxattr_controlfile(list,size);
  90. const ugid::Set ugid(fc->uid,fc->gid);
  91. const rwlock::ReadGuard readlock(&config.srcmountslock);
  92. return _listxattr(config.listxattr,
  93. config.srcmounts,
  94. config.minfreespace,
  95. fusepath,
  96. list,
  97. size);
  98. }
  99. }
  100. }