...
Run Format

Source file src/internal/poll/sendfile_solaris.go

Documentation: internal/poll

     1  // Copyright 2015 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 poll
     6  
     7  import "syscall"
     8  
     9  // Not strictly needed, but very helpful for debugging, see issue #10221.
    10  //go:cgo_import_dynamic _ _ "libsendfile.so"
    11  //go:cgo_import_dynamic _ _ "libsocket.so"
    12  
    13  // maxSendfileSize is the largest chunk size we ask the kernel to copy
    14  // at a time.
    15  const maxSendfileSize int = 4 << 20
    16  
    17  // SendFile wraps the sendfile system call.
    18  func SendFile(dstFD *FD, src int, pos, remain int64) (int64, error) {
    19  	if err := dstFD.writeLock(); err != nil {
    20  		return 0, err
    21  	}
    22  	defer dstFD.writeUnlock()
    23  
    24  	dst := int(dstFD.Sysfd)
    25  	var written int64
    26  	var err error
    27  	for remain > 0 {
    28  		n := maxSendfileSize
    29  		if int64(n) > remain {
    30  			n = int(remain)
    31  		}
    32  		pos1 := pos
    33  		n, err1 := syscall.Sendfile(dst, src, &pos1, n)
    34  		if err1 == syscall.EAGAIN || err1 == syscall.EINTR {
    35  			// partial write may have occurred
    36  			n = int(pos1 - pos)
    37  		}
    38  		if n > 0 {
    39  			pos += int64(n)
    40  			written += int64(n)
    41  			remain -= int64(n)
    42  		}
    43  		if n == 0 && err1 == nil {
    44  			break
    45  		}
    46  		if err1 == syscall.EAGAIN {
    47  			if err1 = dstFD.pd.waitWrite(dstFD.isFile); err1 == nil {
    48  				continue
    49  			}
    50  		}
    51  		if err1 == syscall.EINTR {
    52  			continue
    53  		}
    54  		if err1 != nil {
    55  			// This includes syscall.ENOSYS (no kernel
    56  			// support) and syscall.EINVAL (fd types which
    57  			// don't implement sendfile)
    58  			err = err1
    59  			break
    60  		}
    61  	}
    62  	return written, err
    63  }
    64  

View as plain text