github.com/bir3/gocompiler@v0.9.2202/src/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) (int64, error, bool) { 17 if err := dstFD.writeLock(); err != nil { 18 return 0, err, false 19 } 20 defer dstFD.writeUnlock() 21 if err := dstFD.pd.prepareWrite(dstFD.isFile); err != nil { 22 return 0, err, false 23 } 24 25 dst := dstFD.Sysfd 26 var ( 27 written int64 28 err error 29 handled = true 30 ) 31 for remain > 0 { 32 n := maxSendfileSize 33 if int64(n) > remain { 34 n = int(remain) 35 } 36 pos1 := pos 37 n, err1 := syscall.Sendfile(dst, src, &pos1, n) 38 if n > 0 { 39 pos += int64(n) 40 written += int64(n) 41 remain -= int64(n) 42 } else if n == 0 && err1 == nil { 43 break 44 } 45 if err1 == syscall.EINTR { 46 continue 47 } 48 if err1 == syscall.EAGAIN { 49 if err1 = dstFD.pd.waitWrite(dstFD.isFile); err1 == nil { 50 continue 51 } 52 } 53 if err1 != nil { 54 // This includes syscall.ENOSYS (no kernel 55 // support) and syscall.EINVAL (fd types which 56 // don't implement sendfile) 57 err = err1 58 handled = false 59 break 60 } 61 } 62 return written, err, handled 63 }