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.

96 lines
2.5 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. #if WRITE_BUF
  15. #include <fuse.h>
  16. #include <stdlib.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <string>
  20. #include <vector>
  21. #include "config.hpp"
  22. #include "fileinfo.hpp"
  23. #include "fs_movefile.hpp"
  24. #include "policy.hpp"
  25. #include "rwlock.hpp"
  26. #include "ugid.hpp"
  27. #include "write.hpp"
  28. using std::string;
  29. using std::vector;
  30. using namespace mergerfs;
  31. static
  32. int
  33. _write_buf(const int fd,
  34. fuse_bufvec &src,
  35. const off_t offset)
  36. {
  37. size_t size = fuse_buf_size(&src);
  38. fuse_bufvec dst = FUSE_BUFVEC_INIT(size);
  39. const fuse_buf_copy_flags cpflags =
  40. (fuse_buf_copy_flags)(FUSE_BUF_SPLICE_MOVE|FUSE_BUF_SPLICE_NONBLOCK);
  41. dst.buf->flags = (fuse_buf_flags)(FUSE_BUF_IS_FD|FUSE_BUF_FD_SEEK);
  42. dst.buf->fd = fd;
  43. dst.buf->pos = offset;
  44. return fuse_buf_copy(&dst,&src,cpflags);
  45. }
  46. namespace mergerfs
  47. {
  48. namespace fuse
  49. {
  50. int
  51. write_buf(const char *fusepath,
  52. fuse_bufvec *src,
  53. off_t offset,
  54. fuse_file_info *ffi)
  55. {
  56. int rv;
  57. FileInfo *fi = reinterpret_cast<FileInfo*>(ffi->fh);
  58. rv = _write_buf(fi->fd,*src,offset);
  59. if(rv == -ENOSPC)
  60. {
  61. const fuse_context *fc = fuse_get_context();
  62. const Config &config = Config::get(fc);
  63. if(config.moveonenospc)
  64. {
  65. size_t extra;
  66. const ugid::Set ugid(0,0);
  67. const rwlock::ReadGuard readlock(&config.srcmountslock);
  68. extra = fuse_buf_size(src);
  69. rv = fs::movefile(config.srcmounts,fusepath,extra,fi->fd);
  70. if(rv == -1)
  71. return -ENOSPC;
  72. rv = _write_buf(fi->fd,*src,offset);
  73. }
  74. }
  75. return rv;
  76. }
  77. }
  78. }
  79. #endif