in_message.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package buffer
  15. import (
  16. "fmt"
  17. "io"
  18. "syscall"
  19. "unsafe"
  20. "github.com/jacobsa/fuse/internal/fusekernel"
  21. )
  22. // All requests read from the kernel, without data, are shorter than
  23. // this.
  24. const pageSize = 4096
  25. func init() {
  26. // Confirm the page size.
  27. if syscall.Getpagesize() != pageSize {
  28. panic(fmt.Sprintf("Page size is unexpectedly %d", syscall.Getpagesize()))
  29. }
  30. }
  31. // We size the buffer to have enough room for a fuse request plus data
  32. // associated with a write request.
  33. const bufSize = pageSize + MaxWriteSize
  34. // An incoming message from the kernel, including leading fusekernel.InHeader
  35. // struct. Provides storage for messages and convenient access to their
  36. // contents.
  37. type InMessage struct {
  38. remaining []byte
  39. storage [bufSize]byte
  40. }
  41. // Initialize with the data read by a single call to r.Read. The first call to
  42. // Consume will consume the bytes directly after the fusekernel.InHeader
  43. // struct.
  44. func (m *InMessage) Init(r io.Reader) (err error) {
  45. n, err := r.Read(m.storage[:])
  46. if err != nil {
  47. return
  48. }
  49. // Make sure the message is long enough.
  50. const headerSize = unsafe.Sizeof(fusekernel.InHeader{})
  51. if uintptr(n) < headerSize {
  52. err = fmt.Errorf("Unexpectedly read only %d bytes.", n)
  53. return
  54. }
  55. m.remaining = m.storage[headerSize:n]
  56. // Check the header's length.
  57. if int(m.Header().Len) != n {
  58. err = fmt.Errorf(
  59. "Header says %d bytes, but we read %d",
  60. m.Header().Len,
  61. n)
  62. return
  63. }
  64. return
  65. }
  66. // Return a reference to the header read in the most recent call to Init.
  67. func (m *InMessage) Header() (h *fusekernel.InHeader) {
  68. h = (*fusekernel.InHeader)(unsafe.Pointer(&m.storage[0]))
  69. return
  70. }
  71. // Return the number of bytes left to consume.
  72. func (m *InMessage) Len() uintptr {
  73. return uintptr(len(m.remaining))
  74. }
  75. // Consume the next n bytes from the message, returning a nil pointer if there
  76. // are fewer than n bytes available.
  77. func (m *InMessage) Consume(n uintptr) (p unsafe.Pointer) {
  78. if m.Len() == 0 || n > m.Len() {
  79. return
  80. }
  81. p = unsafe.Pointer(&m.remaining[0])
  82. m.remaining = m.remaining[n:]
  83. return
  84. }
  85. // Equivalent to Consume, except returns a slice of bytes. The result will be
  86. // nil if Consume would fail.
  87. func (m *InMessage) ConsumeBytes(n uintptr) (b []byte) {
  88. if n > m.Len() {
  89. return
  90. }
  91. b = m.remaining[:n]
  92. m.remaining = m.remaining[n:]
  93. return
  94. }