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.

108 lines
2.7 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 <unistd.h>
  16. #include <sys/types.h>
  17. #include <errno.h>
  18. #include <string>
  19. #include <vector>
  20. #include "config.hpp"
  21. #include "fs_path.hpp"
  22. #include "rv.hpp"
  23. #include "rwlock.hpp"
  24. #include "ugid.hpp"
  25. using std::string;
  26. using std::vector;
  27. using mergerfs::Policy;
  28. static
  29. int
  30. _truncate_loop_core(const string *basepath,
  31. const char *fusepath,
  32. const off_t size,
  33. const int error)
  34. {
  35. int rv;
  36. string fullpath;
  37. fs::path::make(basepath,fusepath,fullpath);
  38. rv = ::truncate(fullpath.c_str(),size);
  39. return calc_error(rv,error,errno);
  40. }
  41. static
  42. int
  43. _truncate_loop(const vector<const string*> &basepaths,
  44. const char *fusepath,
  45. const off_t size)
  46. {
  47. int error;
  48. error = -1;
  49. for(size_t i = 0, ei = basepaths.size(); i != ei; i++)
  50. {
  51. error = _truncate_loop_core(basepaths[0],fusepath,size,error);
  52. }
  53. return -error;
  54. }
  55. static
  56. int
  57. _truncate(Policy::Func::Action actionFunc,
  58. const vector<string> &srcmounts,
  59. const uint64_t minfreespace,
  60. const char *fusepath,
  61. const off_t size)
  62. {
  63. int rv;
  64. vector<const string*> basepaths;
  65. rv = actionFunc(srcmounts,fusepath,minfreespace,basepaths);
  66. if(rv == -1)
  67. return -errno;
  68. return _truncate_loop(basepaths,fusepath,size);
  69. }
  70. namespace mergerfs
  71. {
  72. namespace fuse
  73. {
  74. int
  75. truncate(const char *fusepath,
  76. off_t size)
  77. {
  78. const fuse_context *fc = fuse_get_context();
  79. const Config &config = Config::get(fc);
  80. const ugid::Set ugid(fc->uid,fc->gid);
  81. const rwlock::ReadGuard readlock(&config.srcmountslock);
  82. return _truncate(config.truncate,
  83. config.srcmounts,
  84. config.minfreespace,
  85. fusepath,
  86. size);
  87. }
  88. }
  89. }