file.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //go:build (linux || darwin || dragonfly || freebsd || netbsd || openbsd) && !appengine
  2. // +build linux darwin dragonfly freebsd netbsd openbsd
  3. // +build !appengine
  4. package msgp
  5. import (
  6. "os"
  7. "syscall"
  8. )
  9. // ReadFile reads a file into 'dst' using
  10. // a read-only memory mapping. Consequently,
  11. // the file must be mmap-able, and the
  12. // Unmarshaler should never write to
  13. // the source memory. (Methods generated
  14. // by the msgp tool obey that constraint, but
  15. // user-defined implementations may not.)
  16. //
  17. // Reading and writing through file mappings
  18. // is only efficient for large files; small
  19. // files are best read and written using
  20. // the ordinary streaming interfaces.
  21. func ReadFile(dst Unmarshaler, file *os.File) error {
  22. stat, err := file.Stat()
  23. if err != nil {
  24. return err
  25. }
  26. data, err := syscall.Mmap(int(file.Fd()), 0, int(stat.Size()), syscall.PROT_READ, syscall.MAP_SHARED)
  27. if err != nil {
  28. return err
  29. }
  30. adviseRead(data)
  31. _, err = dst.UnmarshalMsg(data)
  32. uerr := syscall.Munmap(data)
  33. if err == nil {
  34. err = uerr
  35. }
  36. return err
  37. }
  38. // MarshalSizer is the combination
  39. // of the Marshaler and Sizer
  40. // interfaces.
  41. type MarshalSizer interface {
  42. Marshaler
  43. Sizer
  44. }
  45. // WriteFile writes a file from 'src' using
  46. // memory mapping. It overwrites the entire
  47. // contents of the previous file.
  48. // The mapping size is calculated
  49. // using the `Msgsize()` method
  50. // of 'src', so it must produce a result
  51. // equal to or greater than the actual encoded
  52. // size of the object. Otherwise,
  53. // a fault (SIGBUS) will occur.
  54. //
  55. // Reading and writing through file mappings
  56. // is only efficient for large files; small
  57. // files are best read and written using
  58. // the ordinary streaming interfaces.
  59. //
  60. // NOTE: The performance of this call
  61. // is highly OS- and filesystem-dependent.
  62. // Users should take care to test that this
  63. // performs as expected in a production environment.
  64. // (Linux users should run a kernel and filesystem
  65. // that support fallocate(2) for the best results.)
  66. func WriteFile(src MarshalSizer, file *os.File) error {
  67. sz := src.Msgsize()
  68. err := fallocate(file, int64(sz))
  69. if err != nil {
  70. return err
  71. }
  72. data, err := syscall.Mmap(int(file.Fd()), 0, sz, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
  73. if err != nil {
  74. return err
  75. }
  76. adviseWrite(data)
  77. chunk := data[:0]
  78. chunk, err = src.MarshalMsg(chunk)
  79. if err != nil {
  80. return err
  81. }
  82. uerr := syscall.Munmap(data)
  83. if uerr != nil {
  84. return uerr
  85. }
  86. return file.Truncate(int64(len(chunk)))
  87. }