Source file src/os/zero_copy_freebsd.go

     1  // Copyright 2024 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.
     4  
     5  package os
     6  
     7  import (
     8  	"internal/poll"
     9  	"io"
    10  )
    11  
    12  var pollCopyFileRange = poll.CopyFileRange
    13  
    14  func (f *File) writeTo(w io.Writer) (written int64, handled bool, err error) {
    15  	return 0, false, nil
    16  }
    17  
    18  func (f *File) readFrom(r io.Reader) (written int64, handled bool, err error) {
    19  	// copy_file_range(2) doesn't support destinations opened with
    20  	// O_APPEND, so don't bother to try zero-copy with these system calls.
    21  	//
    22  	// Visit https://man.freebsd.org/cgi/man.cgi?copy_file_range(2)#ERRORS for details.
    23  	if f.appendMode {
    24  		return 0, false, nil
    25  	}
    26  
    27  	var (
    28  		remain int64
    29  		lr     *io.LimitedReader
    30  	)
    31  	if lr, r, remain = tryLimitedReader(r); remain <= 0 {
    32  		return 0, true, nil
    33  	}
    34  
    35  	var src *File
    36  	switch v := r.(type) {
    37  	case *File:
    38  		src = v
    39  	case fileWithoutWriteTo:
    40  		src = v.File
    41  	default:
    42  		return 0, false, nil
    43  	}
    44  
    45  	if src.checkValid("ReadFrom") != nil {
    46  		// Avoid returning the error as we report handled as false,
    47  		// leave further error handling as the responsibility of the caller.
    48  		return 0, false, nil
    49  	}
    50  
    51  	written, handled, err = pollCopyFileRange(&f.pfd, &src.pfd, remain)
    52  	if lr != nil {
    53  		lr.N -= written
    54  	}
    55  
    56  	return written, handled, wrapSyscallError("copy_file_range", err)
    57  }
    58  

View as plain text