1 // Copyright 2020 The Go 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.
14 pollCopyFileRange = poll.CopyFileRange
15 pollSplice = poll.Splice
18 func (f *File) readFrom(r io.Reader) (written int64, handled bool, err error) {
19 // Neither copy_file_range(2) nor splice(2) supports destinations opened with
20 // O_APPEND, so don't bother to try zero-copy with these system calls.
22 // Visit https://man7.org/linux/man-pages/man2/copy_file_range.2.html#ERRORS and
23 // https://man7.org/linux/man-pages/man2/splice.2.html#ERRORS for details.
28 written, handled, err = f.copyFileRange(r)
32 return f.spliceToFile(r)
35 func (f *File) spliceToFile(r io.Reader) (written int64, handled bool, err error) {
40 if lr, r, remain = tryLimitedReader(r); remain <= 0 {
45 // TODO(panjf2000): run some tests to see if we should unlock the non-streams for splice.
46 // Streams benefit the most from the splice(2), non-streams are not even supported in old kernels
47 // where splice(2) will just return EINVAL; newer kernels support non-streams like UDP, but I really
48 // doubt that splice(2) could help non-streams, cuz they usually send small frames respectively
49 // and one splice call would result in one frame.
50 // splice(2) is suitable for large data but the generation of fragments defeats its edge here.
51 // Therefore, don't bother to try splice if the r is not a streaming descriptor.
52 if pfd == nil || !pfd.IsStream {
56 var syscallName string
57 written, handled, syscallName, err = pollSplice(&f.pfd, pfd, remain)
60 lr.N = remain - written
63 return written, handled, wrapSyscallError(syscallName, err)
66 // getPollFD tries to get the poll.FD from the given io.Reader by expecting
67 // the underlying type of r to be the implementation of syscall.Conn that contains
69 func getPollFD(r io.Reader) *poll.FD {
70 sc, ok := r.(syscall.Conn)
74 rc, err := sc.SyscallConn()
78 ipfd, ok := rc.(interface{ PollFD() *poll.FD })
85 func (f *File) copyFileRange(r io.Reader) (written int64, handled bool, err error) {
90 if lr, r, remain = tryLimitedReader(r); remain <= 0 {
98 if src.checkValid("ReadFrom") != nil {
99 // Avoid returning the error as we report handled as false,
100 // leave further error handling as the responsibility of the caller.
104 written, handled, err = pollCopyFileRange(&f.pfd, &src.pfd, remain)
108 return written, handled, wrapSyscallError("copy_file_range", err)
111 // tryLimitedReader tries to assert the io.Reader to io.LimitedReader, it returns the io.LimitedReader,
112 // the underlying io.Reader and the remaining amount of bytes if the assertion succeeds,
113 // otherwise it just returns the original io.Reader and the theoretical unlimited remaining amount of bytes.
114 func tryLimitedReader(r io.Reader) (*io.LimitedReader, io.Reader, int64) {
115 var remain int64 = 1<<63 - 1 // by default, copy until EOF
117 lr, ok := r.(*io.LimitedReader)
119 return nil, r, remain
123 return lr, lr.R, remain