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.

150 lines
3.4 KiB

4 years ago
  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 "config.hpp"
  15. #include "errno.hpp"
  16. #include "fileinfo.hpp"
  17. #include "fs_movefile.hpp"
  18. #include "fs_write.hpp"
  19. #include "ugid.hpp"
  20. #include "fuse.h"
  21. #include <string>
  22. #include <vector>
  23. using std::string;
  24. using std::vector;
  25. typedef int (*WriteFunc)(const int,const void*,const size_t,const off_t);
  26. namespace l
  27. {
  28. static
  29. bool
  30. out_of_space(const int error_)
  31. {
  32. return ((error_ == ENOSPC) ||
  33. (error_ == EDQUOT));
  34. }
  35. static
  36. int
  37. write_regular(const int fd_,
  38. const void *buf_,
  39. const size_t count_,
  40. const off_t offset_)
  41. {
  42. int rv;
  43. rv = fs::pwrite(fd_,buf_,count_,offset_);
  44. if(rv == -1)
  45. return -errno;
  46. if(rv == 0)
  47. return 0;
  48. return count_;
  49. }
  50. static
  51. int
  52. write_direct_io(const int fd_,
  53. const void *buf_,
  54. const size_t count_,
  55. const off_t offset_)
  56. {
  57. int rv;
  58. rv = fs::pwrite(fd_,buf_,count_,offset_);
  59. if(rv == -1)
  60. return -errno;
  61. return rv;
  62. }
  63. static
  64. int
  65. move_and_write(WriteFunc func_,
  66. const char *buf_,
  67. const size_t count_,
  68. const off_t offset_,
  69. FileInfo *fi_,
  70. int err_)
  71. {
  72. int rv;
  73. Config::Read cfg;
  74. if(cfg->moveonenospc.enabled == false)
  75. return err_;
  76. rv = fs::movefile_as_root(cfg->moveonenospc.policy,
  77. cfg->branches,
  78. fi_->fusepath,
  79. &fi_->fd);
  80. if(rv == -1)
  81. return err_;
  82. return func_(fi_->fd,buf_,count_,offset_);
  83. }
  84. static
  85. int
  86. write(const fuse_file_info_t *ffi_,
  87. WriteFunc func_,
  88. const char *buf_,
  89. const size_t count_,
  90. const off_t offset_)
  91. {
  92. int rv;
  93. FileInfo* fi;
  94. fi = reinterpret_cast<FileInfo*>(ffi_->fh);
  95. rv = func_(fi->fd,buf_,count_,offset_);
  96. if(l::out_of_space(-rv))
  97. rv = l::move_and_write(func_,buf_,count_,offset_,fi,rv);
  98. return rv;
  99. }
  100. }
  101. namespace FUSE::WRITE
  102. {
  103. int
  104. write(const fuse_file_info_t *ffi_,
  105. const char *buf_,
  106. size_t count_,
  107. off_t offset_)
  108. {
  109. WriteFunc wf;
  110. wf = ((ffi_->direct_io) ?
  111. l::write_direct_io :
  112. l::write_regular);
  113. return l::write(ffi_,wf,buf_,count_,offset_);
  114. }
  115. int
  116. write_null(const fuse_file_info_t *ffi_,
  117. const char *buf_,
  118. size_t count_,
  119. off_t offset_)
  120. {
  121. return count_;
  122. }
  123. }