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.

70 lines
1.7 KiB

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. )
  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. if wfs.IsOverQuota {
  34. return 0, fuse.EPERM
  35. }
  36. fh := wfs.GetHandle(FileHandleId(in.Fh))
  37. if fh == nil {
  38. return 0, fuse.ENOENT
  39. }
  40. fh.Lock()
  41. defer fh.Unlock()
  42. entry := fh.entry
  43. if entry == nil {
  44. return 0, fuse.OK
  45. }
  46. entry.Content = nil
  47. offset := int64(in.Offset)
  48. entry.Attributes.FileSize = uint64(max(offset+int64(len(data)), int64(entry.Attributes.FileSize)))
  49. // glog.V(4).Infof("%v write [%d,%d) %d", fh.f.fullpath(), req.Offset, req.Offset+int64(len(req.Data)), len(req.Data))
  50. fh.dirtyPages.AddPage(offset, data)
  51. written = uint32(len(data))
  52. if offset == 0 {
  53. // detect mime type
  54. fh.contentType = http.DetectContentType(data)
  55. }
  56. fh.dirtyMetadata = true
  57. return written, fuse.OK
  58. }