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 5package net 6 7import ( 8 "internal/poll" 9 "io" 10 "os" 11 "syscall" 12) 13 14const supportsSendfile = true 15 16// sendFile copies the contents of r to c using the TransmitFile 17// system call to minimize copies. 18// 19// if handled == true, sendFile returns the number of bytes copied and any 20// non-EOF error. 21// 22// if handled == false, sendFile performed no work. 23func sendFile(fd *netFD, r io.Reader) (written int64, err error, handled bool) { 24 var n int64 = 0 // by default, copy until EOF. 25 26 lr, ok := r.(*io.LimitedReader) 27 if ok { 28 n, r = lr.N, lr.R 29 if n <= 0 { 30 return 0, nil, true 31 } 32 } 33 34 f, ok := r.(*os.File) 35 if !ok { 36 return 0, nil, false 37 } 38 39 written, err = poll.SendFile(&fd.pfd, syscall.Handle(f.Fd()), n) 40 if err != nil { 41 err = wrapSyscallError("transmitfile", err) 42 } 43 44 // If any byte was copied, regardless of any error 45 // encountered mid-way, handled must be set to true. 46 handled = written > 0 47 48 return 49} 50