1
2
3
4
5
6
7 package poll
8
9 import "syscall"
10
11
12
13 const maxSendfileSize int = 4 << 20
14
15
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 if err := dstFD.pd.prepareWrite(dstFD.isFile); err != nil {
22 return 0, err
23 }
24
25 dst := dstFD.Sysfd
26 var written int64
27 var err error
28 for remain > 0 {
29 n := maxSendfileSize
30 if int64(n) > remain {
31 n = int(remain)
32 }
33 pos1 := pos
34 n, err1 := syscall.Sendfile(dst, src, &pos1, n)
35 if n > 0 {
36 pos += int64(n)
37 written += int64(n)
38 remain -= int64(n)
39 } else if n == 0 && err1 == nil {
40 break
41 }
42 if err1 == syscall.EINTR {
43 continue
44 }
45 if err1 == syscall.EAGAIN {
46 if err1 = dstFD.pd.waitWrite(dstFD.isFile); err1 == nil {
47 continue
48 }
49 }
50 if err1 != nil {
51
52
53
54 err = err1
55 break
56 }
57 }
58 return written, err
59 }
60
View as plain text