Skip to content

Commit 853fc12

Browse files
committed
internal/poll: set the correct file offset in FD.Seek for Windows overlapped handles
Windows doesn't keep the file pointer for overlapped file handles. To work around this, we keep track of the current offset ourselves and use it on every Read/Write operation. When the user calls File.Seek with whence == io.SeekCurrent, it expects that the offset we keep track of is also accounted for, else the the seek'ed value won't match the file pointer seen by the user. Updates #74951. Fixes #75081. Change-Id: Ieca7c3779e5349292883ffc293a8474088a4dec7 Reviewed-on: https://go-review.googlesource.com/c/go/+/697275 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
1 parent bd88540 commit 853fc12

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/internal/poll/fd_windows.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,12 @@ func (fd *FD) Seek(offset int64, whence int) (int64, error) {
11071107
fd.l.Lock()
11081108
defer fd.l.Unlock()
11091109

1110+
if !fd.isBlocking && whence == io.SeekCurrent {
1111+
// Windows doesn't keep the file pointer for overlapped file handles.
1112+
// We do it ourselves in case to account for any read or write
1113+
// operations that may have occurred.
1114+
offset += fd.offset
1115+
}
11101116
n, err := syscall.Seek(fd.Sysfd, offset, whence)
11111117
fd.setOffset(n)
11121118
return n, err

src/os/os_windows_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,44 @@ func TestFile(t *testing.T) {
18451845
}
18461846
}
18471847

1848+
func TestFileOverlappedSeek(t *testing.T) {
1849+
t.Parallel()
1850+
name := filepath.Join(t.TempDir(), "foo")
1851+
f := newFileOverlapped(t, name, true)
1852+
content := []byte("foo")
1853+
if _, err := f.Write(content); err != nil {
1854+
t.Fatal(err)
1855+
}
1856+
// Check that the file pointer is at the expected offset.
1857+
n, err := f.Seek(0, io.SeekCurrent)
1858+
if err != nil {
1859+
t.Fatal(err)
1860+
}
1861+
if n != int64(len(content)) {
1862+
t.Errorf("expected file pointer to be at offset %d, got %d", len(content), n)
1863+
}
1864+
// Set the file pointer to the start of the file.
1865+
if _, err := f.Seek(0, io.SeekStart); err != nil {
1866+
t.Fatal(err)
1867+
}
1868+
// Read the first byte.
1869+
var buf [1]byte
1870+
if _, err := f.Read(buf[:]); err != nil {
1871+
t.Fatal(err)
1872+
}
1873+
if !bytes.Equal(buf[:], content[:len(buf)]) {
1874+
t.Errorf("expected %q, got %q", content[:len(buf)], buf[:])
1875+
}
1876+
// Check that the file pointer is at the expected offset.
1877+
n, err = f.Seek(0, io.SeekCurrent)
1878+
if err != nil {
1879+
t.Fatal(err)
1880+
}
1881+
if n != int64(len(buf)) {
1882+
t.Errorf("expected file pointer to be at offset %d, got %d", len(buf), n)
1883+
}
1884+
}
1885+
18481886
func TestPipe(t *testing.T) {
18491887
t.Parallel()
18501888
r, w, err := os.Pipe()

0 commit comments

Comments
 (0)