Source file
src/internal/poll/sendfile_linux.go
1
2
3
4
5 package poll
6
7 import "syscall"
8
9
10
11 const maxSendfileSize int = 4 << 20
12
13
14 func SendFile(dstFD *FD, src int, remain int64) (int64, error) {
15 if err := dstFD.writeLock(); err != nil {
16 return 0, err
17 }
18 defer dstFD.writeUnlock()
19 if err := dstFD.pd.prepareWrite(dstFD.isFile); err != nil {
20 return 0, err
21 }
22
23 dst := dstFD.Sysfd
24 var written int64
25 var err error
26 for remain > 0 {
27 n := maxSendfileSize
28 if int64(n) > remain {
29 n = int(remain)
30 }
31 n, err1 := syscall.Sendfile(dst, src, nil, n)
32 if n > 0 {
33 written += int64(n)
34 remain -= int64(n)
35 } else if n == 0 && err1 == nil {
36 break
37 }
38 if err1 == syscall.EINTR {
39 continue
40 }
41 if err1 == syscall.EAGAIN {
42 if err1 = dstFD.pd.waitWrite(dstFD.isFile); err1 == nil {
43 continue
44 }
45 }
46 if err1 != nil {
47
48
49
50 err = err1
51 break
52 }
53 }
54 return written, err
55 }
56
View as plain text