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.
13 // maxSendfileSize is the largest chunk size we ask the kernel to copy
15 const maxSendfileSize
int = 4 << 20
17 // sendFile copies the contents of r to c using the sendfile
18 // system call to minimize copies.
20 // if handled == true, sendFile returns the number of bytes copied and any
23 // if handled == false, sendFile performed no work.
24 func sendFile(c
*netFD
, r io
.Reader
) (written
int64, err error
, handled
bool) {
25 // FreeBSD uses 0 as the "until EOF" value. If you pass in more bytes than the
26 // file contains, it will loop back to the beginning ad nauseam until it's sent
27 // exactly the number of bytes told to. As such, we need to know exactly how many
31 lr
, ok
:= r
.(*io
.LimitedReader
)
33 remain
, r
= lr
.N
, lr
.R
52 // The other quirk with FreeBSD's sendfile implementation is that it doesn't
53 // use the current position of the file -- if you pass it offset 0, it starts
54 // from offset 0. There's no way to tell it "start from current position", so
55 // we have to manage that explicitly.
56 pos
, err
:= f
.Seek(0, os
.SEEK_CUR
)
61 if err
:= c
.writeLock(); err
!= nil {
70 if int64(n
) > remain
{
74 n
, err1
:= syscall
.Sendfile(dst
, src
, &pos1
, n
)
80 if n
== 0 && err1
== nil {
83 if err1
== syscall
.EAGAIN
{
84 if err1
= c
.pd
.WaitWrite(); err1
== nil {
88 if err1
== syscall
.EINTR
{
92 // This includes syscall.ENOSYS (no kernel
93 // support) and syscall.EINVAL (fd types which
94 // don't implement sendfile together)
95 err
= &OpError
{"sendfile", c
.net
, c
.raddr
, err1
}
102 return written
, err
, written
> 0