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.

128 lines
3.5 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 <errno.h>
  24. #include <unistd.h>
  25. #include "config.hpp"
  26. #include "fs_clonepath.hpp"
  27. #include "fs_path.hpp"
  28. #include "rwlock.hpp"
  29. #include "ugid.hpp"
  30. using std::string;
  31. using std::vector;
  32. using mergerfs::Policy;
  33. using namespace mergerfs;
  34. static
  35. int
  36. _single_link(Policy::Func::Search searchFunc,
  37. const vector<string> &srcmounts,
  38. const size_t minfreespace,
  39. const string &base,
  40. const string &oldpath,
  41. const string &newpath)
  42. {
  43. int rv;
  44. const string fulloldpath = fs::path::make(base,oldpath);
  45. const string fullnewpath = fs::path::make(base,newpath);
  46. rv = ::link(fulloldpath.c_str(),fullnewpath.c_str());
  47. if(rv == -1 && errno == ENOENT)
  48. {
  49. string newpathdir;
  50. vector<string> foundpath;
  51. newpathdir = fs::path::dirname(newpath);
  52. rv = searchFunc(srcmounts,newpathdir,minfreespace,foundpath);
  53. if(rv == -1)
  54. return -1;
  55. {
  56. const ugid::SetRootGuard ugidGuard;
  57. fs::clonepath(foundpath[0],base,newpathdir);
  58. }
  59. rv = ::link(fulloldpath.c_str(),fullnewpath.c_str());
  60. }
  61. return rv;
  62. }
  63. static
  64. int
  65. _link(Policy::Func::Search searchFunc,
  66. Policy::Func::Action actionFunc,
  67. const vector<string> &srcmounts,
  68. const size_t minfreespace,
  69. const string &oldpath,
  70. const string &newpath)
  71. {
  72. int rv;
  73. int error;
  74. vector<string> oldpaths;
  75. rv = actionFunc(srcmounts,oldpath,minfreespace,oldpaths);
  76. if(rv == -1)
  77. return -errno;
  78. error = 0;
  79. for(size_t i = 0, ei = oldpaths.size(); i != ei; i++)
  80. {
  81. rv = _single_link(searchFunc,srcmounts,minfreespace,oldpaths[i],oldpath,newpath);
  82. if(rv == -1)
  83. error = errno;
  84. }
  85. return -error;
  86. }
  87. namespace mergerfs
  88. {
  89. namespace fuse
  90. {
  91. int
  92. link(const char *from,
  93. const char *to)
  94. {
  95. const fuse_context *fc = fuse_get_context();
  96. const Config &config = Config::get(fc);
  97. const ugid::Set ugid(fc->uid,fc->gid);
  98. const rwlock::ReadGuard readlock(&config.srcmountslock);
  99. return _link(config.getattr,
  100. config.link,
  101. config.srcmounts,
  102. config.minfreespace,
  103. from,
  104. to);
  105. }
  106. }
  107. }