OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [sendfile_linux.go] - Blame information for rev 761

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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 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
 
44
        dst := c.sysfd
45
        src := f.Fd()
46
        for remain > 0 {
47
                n := maxSendfileSize
48
                if int64(n) > remain {
49
                        n = int(remain)
50
                }
51
                n, err1 := syscall.Sendfile(dst, src, nil, n)
52
                if n > 0 {
53
                        written += int64(n)
54
                        remain -= int64(n)
55
                }
56
                if n == 0 && err1 == nil {
57
                        break
58
                }
59
                if err1 == syscall.EAGAIN && c.wdeadline >= 0 {
60
                        pollserver.WaitWrite(c)
61
                        continue
62
                }
63
                if err1 != nil {
64
                        // This includes syscall.ENOSYS (no kernel
65
                        // support) and syscall.EINVAL (fd types which
66
                        // don't implement sendfile together)
67
                        err = &OpError{"sendfile", c.net, c.raddr, err1}
68
                        break
69
                }
70
        }
71
        if lr != nil {
72
                lr.N = remain
73
        }
74
        return written, err, written > 0
75
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.