Home | History | Annotate | Download | only in net
      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 
      5 package net
      6 
      7 import (
      8 	"internal/poll"
      9 	"io"
     10 	"os"
     11 )
     12 
     13 // sendFile copies the contents of r to c using the sendfile
     14 // system call to minimize copies.
     15 //
     16 // if handled == true, sendFile returns the number of bytes copied and any
     17 // non-EOF error.
     18 //
     19 // if handled == false, sendFile performed no work.
     20 func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
     21 	var remain int64 = 1 << 62 // by default, copy until EOF
     22 
     23 	lr, ok := r.(*io.LimitedReader)
     24 	if ok {
     25 		remain, r = lr.N, lr.R
     26 		if remain <= 0 {
     27 			return 0, nil, true
     28 		}
     29 	}
     30 	f, ok := r.(*os.File)
     31 	if !ok {
     32 		return 0, nil, false
     33 	}
     34 
     35 	written, err = poll.SendFile(&c.pfd, int(f.Fd()), remain)
     36 
     37 	if lr != nil {
     38 		lr.N = remain - written
     39 	}
     40 	return written, wrapSyscallError("sendfile", err), written > 0
     41 }
     42