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.
5 // +build dragonfly freebsd solaris
15 // sendFile copies the contents of r to c using the sendfile
16 // system call to minimize copies.
18 // if handled == true, sendFile returns the number of bytes copied and any
21 // if handled == false, sendFile performed no work.
22 func sendFile(c
*netFD
, r io
.Reader
) (written
int64, err error
, handled
bool) {
23 // FreeBSD, DragonFly and Solaris use 0 as the "until EOF" value.
24 // If you pass in more bytes than the file contains, it will
25 // loop back to the beginning ad nauseam until it's sent
26 // exactly the number of bytes told to. As such, we need to
27 // know exactly how many bytes to send.
30 lr
, ok
:= r
.(*io
.LimitedReader
)
32 remain
, r
= lr
.N
, lr
.R
51 // The other quirk with FreeBSD/DragonFly/Solaris's sendfile
52 // implementation is that it doesn't use the current position
53 // of the file -- if you pass it offset 0, it starts from
54 // offset 0. There's no way to tell it "start from current
55 // position", so we have to manage that explicitly.
56 pos
, err
:= f
.Seek(0, io
.SeekCurrent
)
61 written
, err
= poll
.SendFile(&c
.pfd
, int(f
.Fd()), pos
, remain
)
64 lr
.N
= remain
- written
67 _
, err1
:= f
.Seek(written
, io
.SeekCurrent
)
68 if err1
!= nil && err
== nil {
69 return written
, err1
, written
> 0
72 return written
, wrapSyscallError("sendfile", err
), written
> 0