github.com/hanwen/go-fuse@v1.0.0/fuse/read.go (about) 1 // Copyright 2016 the Go-FUSE Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package fuse 6 7 import ( 8 "io" 9 "syscall" 10 ) 11 12 // ReadResultData is the read return for returning bytes directly. 13 type readResultData struct { 14 // Raw bytes for the read. 15 Data []byte 16 } 17 18 func (r *readResultData) Size() int { 19 return len(r.Data) 20 } 21 22 func (r *readResultData) Done() { 23 } 24 25 func (r *readResultData) Bytes(buf []byte) ([]byte, Status) { 26 return r.Data, OK 27 } 28 29 func ReadResultData(b []byte) ReadResult { 30 return &readResultData{b} 31 } 32 33 func ReadResultFd(fd uintptr, off int64, sz int) ReadResult { 34 return &readResultFd{fd, off, sz} 35 } 36 37 // ReadResultFd is the read return for zero-copy file data. 38 type readResultFd struct { 39 // Splice from the following file. 40 Fd uintptr 41 42 // Offset within Fd, or -1 to use current offset. 43 Off int64 44 45 // Size of data to be loaded. Actual data available may be 46 // less at the EOF. 47 Sz int 48 } 49 50 // Reads raw bytes from file descriptor if necessary, using the passed 51 // buffer as storage. 52 func (r *readResultFd) Bytes(buf []byte) ([]byte, Status) { 53 sz := r.Sz 54 if len(buf) < sz { 55 sz = len(buf) 56 } 57 58 n, err := syscall.Pread(int(r.Fd), buf[:sz], r.Off) 59 if err == io.EOF { 60 err = nil 61 } 62 63 if n < 0 { 64 n = 0 65 } 66 67 return buf[:n], ToStatus(err) 68 } 69 70 func (r *readResultFd) Size() int { 71 return r.Sz 72 } 73 74 func (r *readResultFd) Done() { 75 }