Source file src/os/zero_copy_posix.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  //go:build unix || js || wasip1 || windows
     6  
     7  package os
     8  
     9  import (
    10  	"io"
    11  	"syscall"
    12  )
    13  
    14  // wrapSyscallError takes an error and a syscall name. If the error is
    15  // a syscall.Errno, it wraps it in an os.SyscallError using the syscall name.
    16  func wrapSyscallError(name string, err error) error {
    17  	if _, ok := err.(syscall.Errno); ok {
    18  		err = NewSyscallError(name, err)
    19  	}
    20  	return err
    21  }
    22  
    23  // tryLimitedReader tries to assert the io.Reader to io.LimitedReader, it returns the io.LimitedReader,
    24  // the underlying io.Reader and the remaining amount of bytes if the assertion succeeds,
    25  // otherwise it just returns the original io.Reader and the theoretical unlimited remaining amount of bytes.
    26  func tryLimitedReader(r io.Reader) (*io.LimitedReader, io.Reader, int64) {
    27  	var remain int64 = 1<<63 - 1 // by default, copy until EOF
    28  
    29  	lr, ok := r.(*io.LimitedReader)
    30  	if !ok {
    31  		return nil, r, remain
    32  	}
    33  
    34  	remain = lr.N
    35  	return lr, lr.R, remain
    36  }
    37  

View as plain text