2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2011-09-16 17:47:21 +02:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-02-07 05:45:01 +01:00
|
|
|
// +build hurd linux
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2017-09-14 19:11:35 +02:00
|
|
|
"internal/poll"
|
2011-09-16 17:47:21 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sendFile copies the contents of r to c using the sendfile
|
|
|
|
// system call to minimize copies.
|
|
|
|
//
|
|
|
|
// if handled == true, sendFile returns the number of bytes copied and any
|
|
|
|
// non-EOF error.
|
|
|
|
//
|
|
|
|
// if handled == false, sendFile performed no work.
|
2011-12-03 03:17:34 +01:00
|
|
|
func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
|
2011-09-16 17:47:21 +02:00
|
|
|
var remain int64 = 1 << 62 // by default, copy until EOF
|
|
|
|
|
|
|
|
lr, ok := r.(*io.LimitedReader)
|
|
|
|
if ok {
|
|
|
|
remain, r = lr.N, lr.R
|
|
|
|
if remain <= 0 {
|
|
|
|
return 0, nil, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f, ok := r.(*os.File)
|
|
|
|
if !ok {
|
|
|
|
return 0, nil, false
|
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
sc, err := f.SyscallConn()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var werr error
|
|
|
|
err = sc.Read(func(fd uintptr) bool {
|
|
|
|
written, werr = poll.SendFile(&c.pfd, int(fd), remain)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if werr == nil {
|
|
|
|
werr = err
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
if lr != nil {
|
2017-09-14 19:11:35 +02:00
|
|
|
lr.N = remain - written
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
return written, wrapSyscallError("sendfile", err), written > 0
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|