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