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.

73 lines
1.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "github.com/hanwen/go-fuse/v2/fuse"
  4. "net/http"
  5. "syscall"
  6. )
  7. /**
  8. * Write data
  9. *
  10. * Write should return exactly the number of bytes requested
  11. * except on error. An exception to this is when the file has
  12. * been opened in 'direct_io' mode, in which case the return value
  13. * of the write system call will reflect the return value of this
  14. * operation.
  15. *
  16. * Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
  17. * expected to reset the setuid and setgid bits.
  18. *
  19. * fi->fh will contain the value set by the open method, or will
  20. * be undefined if the open method didn't set any value.
  21. *
  22. * Valid replies:
  23. * fuse_reply_write
  24. * fuse_reply_err
  25. *
  26. * @param req request handle
  27. * @param ino the inode number
  28. * @param buf data to write
  29. * @param size number of bytes to write
  30. * @param off offset to write to
  31. * @param fi file information
  32. */
  33. func (wfs *WFS) Write(cancel <-chan struct{}, in *fuse.WriteIn, data []byte) (written uint32, code fuse.Status) {
  34. if wfs.IsOverQuota {
  35. return 0, fuse.Status(syscall.ENOSPC)
  36. }
  37. fh := wfs.GetHandle(FileHandleId(in.Fh))
  38. if fh == nil {
  39. return 0, fuse.ENOENT
  40. }
  41. fh.dirtyPages.writerPattern.MonitorWriteAt(int64(in.Offset), int(in.Size))
  42. fh.Lock()
  43. defer fh.Unlock()
  44. entry := fh.entry
  45. if entry == nil {
  46. return 0, fuse.OK
  47. }
  48. entry.Content = nil
  49. offset := int64(in.Offset)
  50. entry.Attributes.FileSize = uint64(max(offset+int64(len(data)), int64(entry.Attributes.FileSize)))
  51. // glog.V(4).Infof("%v write [%d,%d) %d", fh.f.fullpath(), req.Offset, req.Offset+int64(len(req.Data)), len(req.Data))
  52. fh.dirtyPages.AddPage(offset, data, fh.dirtyPages.writerPattern.IsSequentialMode())
  53. written = uint32(len(data))
  54. if offset == 0 {
  55. // detect mime type
  56. fh.contentType = http.DetectContentType(data)
  57. }
  58. fh.dirtyMetadata = true
  59. return written, fuse.OK
  60. }