sendfile_glibc.go 1.1 KB

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