Skip to content

Commit 2c35900

Browse files
qmuntalgopherbot
authored andcommitted
net: deduplicate sendfile files
The sendfile implementation for platforms supporting it is now in net/sendfile.go, rather than being duplicated in separate files for each platform. The only difference between the implementations was the poll.SendFile parameters, which have been harmonized, and also linux strictly asserting for os.File, which now have been relaxed to allow any type implementing syscall.Conn. Change-Id: Ia1a2d5ee7380710a36fc555dbf681f7e996ea2ec Reviewed-on: https://go-review.googlesource.com/c/go/+/664075 Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Quim Muntal <[email protected]>
1 parent 9aad012 commit 2c35900

11 files changed

+23
-138
lines changed

src/internal/poll/sendfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
package poll
66

7-
var TestHookDidSendFile = func(dstFD *FD, src int, written int64, err error, handled bool) {}
7+
var TestHookDidSendFile = func(dstFD *FD, src uintptr, written int64, err error, handled bool) {}

src/internal/poll/sendfile_unix.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,29 @@ import (
2727
// If handled is false, sendfile was unable to perform the copy,
2828
// has not modified the source or destination,
2929
// and the caller should perform the copy using a fallback implementation.
30-
func SendFile(dstFD *FD, src int, size int64) (n int64, err error, handled bool) {
30+
func SendFile(dstFD *FD, src uintptr, size int64) (n int64, err error, handled bool) {
3131
if goos := runtime.GOOS; goos == "linux" || goos == "android" {
3232
// Linux's sendfile doesn't require any setup:
3333
// It sends from the current position of the source file and
3434
// updates the position of the source after sending.
35-
return sendFile(dstFD, src, nil, size)
35+
return sendFile(dstFD, int(src), nil, size)
3636
}
3737

3838
// Non-Linux sendfile implementations don't use the current position of the source file,
3939
// so we need to look up the position, pass it explicitly, and adjust it after
4040
// sendfile returns.
4141
start, err := ignoringEINTR2(func() (int64, error) {
42-
return syscall.Seek(src, 0, io.SeekCurrent)
42+
return syscall.Seek(int(src), 0, io.SeekCurrent)
4343
})
4444
if err != nil {
4545
return 0, err, false
4646
}
4747

4848
pos := start
49-
n, err, handled = sendFile(dstFD, src, &pos, size)
49+
n, err, handled = sendFile(dstFD, int(src), &pos, size)
5050
if n > 0 {
5151
ignoringEINTR2(func() (int64, error) {
52-
return syscall.Seek(src, start+n, io.SeekStart)
52+
return syscall.Seek(int(src), start+n, io.SeekStart)
5353
})
5454
}
5555
return n, err, handled
@@ -58,7 +58,7 @@ func SendFile(dstFD *FD, src int, size int64) (n int64, err error, handled bool)
5858
// sendFile wraps the sendfile system call.
5959
func sendFile(dstFD *FD, src int, offset *int64, size int64) (written int64, err error, handled bool) {
6060
defer func() {
61-
TestHookDidSendFile(dstFD, src, written, err, handled)
61+
TestHookDidSendFile(dstFD, uintptr(src), written, err, handled)
6262
}()
6363
if err := dstFD.writeLock(); err != nil {
6464
return 0, err, false

src/internal/poll/sendfile_windows.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import (
1010
)
1111

1212
// SendFile wraps the TransmitFile call.
13-
func SendFile(fd *FD, src syscall.Handle, size int64) (written int64, err error, handled bool) {
13+
func SendFile(fd *FD, src uintptr, size int64) (written int64, err error, handled bool) {
1414
defer func() {
1515
TestHookDidSendFile(fd, 0, written, err, written > 0)
1616
}()
1717
if fd.kind == kindPipe {
1818
// TransmitFile does not work with pipes
1919
return 0, syscall.ESPIPE, false
2020
}
21-
if ft, _ := syscall.GetFileType(src); ft == syscall.FILE_TYPE_PIPE {
21+
hsrc := syscall.Handle(src)
22+
if ft, _ := syscall.GetFileType(hsrc); ft == syscall.FILE_TYPE_PIPE {
2223
return 0, syscall.ESPIPE, false
2324
}
2425

@@ -29,11 +30,11 @@ func SendFile(fd *FD, src syscall.Handle, size int64) (written int64, err error,
2930

3031
// Get the file size so we don't read past the end of the file.
3132
var fi syscall.ByHandleFileInformation
32-
if err := syscall.GetFileInformationByHandle(src, &fi); err != nil {
33+
if err := syscall.GetFileInformationByHandle(hsrc, &fi); err != nil {
3334
return 0, err, false
3435
}
3536
fileSize := int64(fi.FileSizeHigh)<<32 + int64(fi.FileSizeLow)
36-
startpos, err := syscall.Seek(src, 0, io.SeekCurrent)
37+
startpos, err := syscall.Seek(hsrc, 0, io.SeekCurrent)
3738
if err != nil {
3839
return 0, err, false
3940
}
@@ -49,7 +50,7 @@ func SendFile(fd *FD, src syscall.Handle, size int64) (written int64, err error,
4950
// Some versions of Windows (Windows 10 1803) do not set
5051
// file position after TransmitFile completes.
5152
// So just use Seek to set file position.
52-
_, serr := syscall.Seek(src, startpos+written, io.SeekStart)
53+
_, serr := syscall.Seek(hsrc, startpos+written, io.SeekStart)
5354
if err != nil {
5455
err = serr
5556
}
@@ -62,7 +63,7 @@ func SendFile(fd *FD, src syscall.Handle, size int64) (written int64, err error,
6263
const maxChunkSizePerCall = int64(0x7fffffff - 1)
6364

6465
o := &fd.wop
65-
o.handle = src
66+
o.handle = hsrc
6667
for size > 0 {
6768
chunkSize := maxChunkSizePerCall
6869
if chunkSize > size {

src/net/sendfile_unix_alt.go renamed to src/net/sendfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build (darwin && !ios) || dragonfly || freebsd || solaris
5+
//go:build linux || (darwin && !ios) || dragonfly || freebsd || solaris || windows
66

77
package net
88

@@ -44,7 +44,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) {
4444

4545
var werr error
4646
err = sc.Read(func(fd uintptr) bool {
47-
written, werr, handled = poll.SendFile(&c.pfd, int(fd), remain)
47+
written, werr, handled = poll.SendFile(&c.pfd, fd, remain)
4848
return true
4949
})
5050
if err == nil {

src/net/sendfile_linux.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/net/sendfile_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func expectSendfile(t *testing.T, wantConn Conn, f func()) {
4949
gotFD *poll.FD
5050
gotErr error
5151
)
52-
poll.TestHookDidSendFile = func(dstFD *poll.FD, src int, written int64, err error, handled bool) {
52+
poll.TestHookDidSendFile = func(dstFD *poll.FD, src uintptr, written int64, err error, handled bool) {
5353
if called {
5454
t.Error("internal/poll.SendFile called multiple times, want one call")
5555
}

src/net/sendfile_windows.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/os/readfrom_solaris_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func hookSendFileTB(tb testing.TB) *copyFileHook {
4848
tb.Cleanup(func() {
4949
poll.TestHookDidSendFile = orig
5050
})
51-
poll.TestHookDidSendFile = func(dstFD *poll.FD, src int, written int64, err error, handled bool) {
51+
poll.TestHookDidSendFile = func(dstFD *poll.FD, src uintptr, written int64, err error, handled bool) {
5252
hook.called = true
5353
hook.dstfd = dstFD.Sysfd
54-
hook.srcfd = src
54+
hook.srcfd = int(src)
5555
hook.written = written
5656
hook.err = err
5757
hook.handled = handled

src/os/writeto_linux_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ func hookSendFile(t *testing.T) *sendFileHook {
111111
t.Cleanup(func() {
112112
poll.TestHookDidSendFile = orig
113113
})
114-
poll.TestHookDidSendFile = func(dstFD *poll.FD, src int, written int64, err error, handled bool) {
114+
poll.TestHookDidSendFile = func(dstFD *poll.FD, src uintptr, written int64, err error, handled bool) {
115115
h.called = true
116116
h.dstfd = dstFD.Sysfd
117-
h.srcfd = src
117+
h.srcfd = int(src)
118118
h.written = written
119119
h.err = err
120120
h.handled = handled

src/os/zero_copy_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (f *File) writeTo(w io.Writer) (written int64, handled bool, err error) {
2828
}
2929

3030
rerr := sc.Read(func(fd uintptr) (done bool) {
31-
written, err, handled = poll.SendFile(pfd, int(fd), 0)
31+
written, err, handled = poll.SendFile(pfd, fd, 0)
3232
return true
3333
})
3434

src/os/zero_copy_solaris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (f *File) readFrom(r io.Reader) (written int64, handled bool, err error) {
7878
// https://docs.oracle.com/cd/E88353_01/html/E37843/sendfile-3c.html and
7979
// https://illumos.org/man/3EXT/sendfile for more details.
8080
rerr := sc.Read(func(fd uintptr) bool {
81-
written, err, handled = poll.SendFile(&f.pfd, int(fd), remain)
81+
written, err, handled = poll.SendFile(&f.pfd, fd, remain)
8282
return true
8383
})
8484
if lr != nil {

0 commit comments

Comments
 (0)