github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/internal/poll/sendfile_bsd.go (about) 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 //go:build darwin || 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) (written int64, err error, handled bool) { 17 if err := dstFD.writeLock(); err != nil { 18 return 0, err, false 19 } 20 defer dstFD.writeUnlock() 21 22 if err := dstFD.pd.prepareWrite(dstFD.isFile); err != nil { 23 return 0, err, false 24 } 25 26 dst := dstFD.Sysfd 27 for remain > 0 { 28 n := maxSendfileSize 29 if int64(n) > remain { 30 n = int(remain) 31 } 32 pos1 := pos 33 n, err = syscall.Sendfile(dst, src, &pos1, n) 34 if n > 0 { 35 pos += int64(n) 36 written += int64(n) 37 remain -= int64(n) 38 } 39 if err == syscall.EINTR { 40 continue 41 } 42 // This includes syscall.ENOSYS (no kernel 43 // support) and syscall.EINVAL (fd types which 44 // don't implement sendfile), and other errors. 45 // We should end the loop when there is no error 46 // returned from sendfile(2) or it is not a retryable error. 47 if err != syscall.EAGAIN { 48 break 49 } 50 if err = dstFD.pd.waitWrite(dstFD.isFile); err != nil { 51 break 52 } 53 } 54 handled = written != 0 || (err != syscall.ENOSYS && err != syscall.EINVAL) 55 return 56 }