OSDN Git Service

Update Go library to r60.
[pf3gnuchains/gcc-fork.git] / libgo / go / net / sendfile_linux.go
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 package net
6
7 import (
8         "io"
9         "os"
10         "syscall"
11 )
12
13 // maxSendfileSize is the largest chunk size we ask the kernel to copy
14 // at a time.
15 const maxSendfileSize int = 4 << 20
16
17 // sendFile copies the contents of r to c using the sendfile
18 // system call to minimize copies.
19 //
20 // if handled == true, sendFile returns the number of bytes copied and any
21 // non-EOF error.
22 //
23 // if handled == false, sendFile performed no work.
24 func sendFile(c *netFD, r io.Reader) (written int64, err os.Error, handled bool) {
25         var remain int64 = 1 << 62 // by default, copy until EOF
26
27         lr, ok := r.(*io.LimitedReader)
28         if ok {
29                 remain, r = lr.N, lr.R
30                 if remain <= 0 {
31                         return 0, nil, true
32                 }
33         }
34         f, ok := r.(*os.File)
35         if !ok {
36                 return 0, nil, false
37         }
38
39         c.wio.Lock()
40         defer c.wio.Unlock()
41         c.incref()
42         defer c.decref()
43         if c.wdeadline_delta > 0 {
44                 // This is a little odd that we're setting the timeout
45                 // for the entire file but Write has the same issue
46                 // (if one slurps the whole file into memory and
47                 // do one large Write). At least they're consistent.
48                 c.wdeadline = pollserver.Now() + c.wdeadline_delta
49         } else {
50                 c.wdeadline = 0
51         }
52
53         dst := c.sysfd
54         src := f.Fd()
55         for remain > 0 {
56                 n := maxSendfileSize
57                 if int64(n) > remain {
58                         n = int(remain)
59                 }
60                 n, errno := syscall.Sendfile(dst, src, nil, n)
61                 if n > 0 {
62                         written += int64(n)
63                         remain -= int64(n)
64                 }
65                 if n == 0 && errno == 0 {
66                         break
67                 }
68                 if errno == syscall.EAGAIN && c.wdeadline >= 0 {
69                         pollserver.WaitWrite(c)
70                         continue
71                 }
72                 if errno != 0 {
73                         // This includes syscall.ENOSYS (no kernel
74                         // support) and syscall.EINVAL (fd types which
75                         // don't implement sendfile together)
76                         err = &OpError{"sendfile", c.net, c.raddr, os.Errno(errno)}
77                         break
78                 }
79         }
80         if lr != nil {
81                 lr.N = remain
82         }
83         return written, err, written > 0
84 }