...
Run Format

Source file src/internal/poll/sendfile_bsd.go

Documentation: internal/poll

     1  // Copyright 2011 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  // +build dragonfly freebsd
     6  
     7  package poll
     8  
     9  import "syscall"
    10  
    11  // maxSendfileSize is the largest chunk size we ask the kernel to copy
    12  // at a time.
    13  const maxSendfileSize int = 4 << 20
    14  
    15  // SendFile wraps the sendfile system call.
    16  func SendFile(dstFD *FD, src int, pos, remain int64) (int64, error) {
    17  	if err := dstFD.writeLock(); err != nil {
    18  		return 0, err
    19  	}
    20  	defer dstFD.writeUnlock()
    21  	dst := int(dstFD.Sysfd)
    22  	var written int64
    23  	var err error
    24  	for remain > 0 {
    25  		n := maxSendfileSize
    26  		if int64(n) > remain {
    27  			n = int(remain)
    28  		}
    29  		pos1 := pos
    30  		n, err1 := syscall.Sendfile(dst, src, &pos1, n)
    31  		if n > 0 {
    32  			pos += int64(n)
    33  			written += int64(n)
    34  			remain -= int64(n)
    35  		}
    36  		if n == 0 && err1 == nil {
    37  			break
    38  		}
    39  		if err1 == syscall.EAGAIN {
    40  			if err1 = dstFD.pd.waitWrite(dstFD.isFile); err1 == nil {
    41  				continue
    42  			}
    43  		}
    44  		if err1 != nil {
    45  			// This includes syscall.ENOSYS (no kernel
    46  			// support) and syscall.EINVAL (fd types which
    47  			// don't implement sendfile)
    48  			err = err1
    49  			break
    50  		}
    51  	}
    52  	return written, err
    53  }
    54  

View as plain text