net: broken sendfile on SmartOS/Solaris

In the event of a partial write on Solaris and some BSDs, the offset
pointer passed to sendfile() will be updated even though the function
returns -1 if errno is set to EAGAIN/EINTR.  In that case, calculate the
bytes written based on the difference between the updated offset and the
original offset.  If no bytes were written, and errno is set to
EAGAIN/EINTR, ignore the errno.

Fixes #13892

Change-Id: I6334b5ef2edcbebdaa7db36fa4f7785967313c2d
Reviewed-on: https://go-review.googlesource.com/21769
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Shawn Walker-Salas 2016-04-08 15:59:04 -07:00 committed by Brad Fitzpatrick
parent b1851a3c11
commit 98080a6c64
2 changed files with 97 additions and 2 deletions

90
src/net/sendfile_test.go Normal file
View file

@ -0,0 +1,90 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"testing"
)
const (
twain = "../compress/testdata/Mark.Twain-Tom.Sawyer.txt"
twainLen = 387851
twainSHA256 = "461eb7cb2d57d293fc680c836464c9125e4382be3596f7d415093ae9db8fcb0e"
)
func TestSendFile(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
errc := make(chan error, 1)
go func(ln Listener) {
// Wait for a connection.
conn, err := ln.Accept()
if err != nil {
errc <- err
close(errc)
return
}
go func() {
defer close(errc)
defer conn.Close()
f, err := os.Open(twain)
if err != nil {
errc <- err
return
}
defer f.Close()
// Return file data using io.Copy, which should use
// sendFile if available.
sbytes, err := io.Copy(conn, f)
if err != nil {
errc <- err
return
}
if sbytes != twainLen {
errc <- fmt.Errorf("sent %d bytes; expected %d", sbytes, twainLen)
return
}
}()
}(ln)
// Connect to listener to retrieve file and verify digest matches
// expected.
c, err := Dial("tcp", ln.Addr().String())
if err != nil {
t.Fatal(err)
}
defer c.Close()
h := sha256.New()
rbytes, err := io.Copy(h, c)
if err != nil {
t.Error(err)
}
if rbytes != twainLen {
t.Errorf("received %d bytes; expected %d", rbytes, twainLen)
}
if res := hex.EncodeToString(h.Sum(nil)); res != twainSHA256 {
t.Error("retrieved data hash did not match")
}
for err := range errc {
t.Error(err)
}
}