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.

66 lines
1.6 KiB

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