mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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>
This commit is contained in:
parent
bd885401d5
commit
853fc12739
2 changed files with 44 additions and 0 deletions
|
|
@ -1107,6 +1107,12 @@ func (fd *FD) Seek(offset int64, whence int) (int64, error) {
|
|||
fd.l.Lock()
|
||||
defer fd.l.Unlock()
|
||||
|
||||
if !fd.isBlocking && whence == io.SeekCurrent {
|
||||
// Windows doesn't keep the file pointer for overlapped file handles.
|
||||
// We do it ourselves in case to account for any read or write
|
||||
// operations that may have occurred.
|
||||
offset += fd.offset
|
||||
}
|
||||
n, err := syscall.Seek(fd.Sysfd, offset, whence)
|
||||
fd.setOffset(n)
|
||||
return n, err
|
||||
|
|
|
|||
|
|
@ -1845,6 +1845,44 @@ func TestFile(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFileOverlappedSeek(t *testing.T) {
|
||||
t.Parallel()
|
||||
name := filepath.Join(t.TempDir(), "foo")
|
||||
f := newFileOverlapped(t, name, true)
|
||||
content := []byte("foo")
|
||||
if _, err := f.Write(content); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Check that the file pointer is at the expected offset.
|
||||
n, err := f.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != int64(len(content)) {
|
||||
t.Errorf("expected file pointer to be at offset %d, got %d", len(content), n)
|
||||
}
|
||||
// Set the file pointer to the start of the file.
|
||||
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Read the first byte.
|
||||
var buf [1]byte
|
||||
if _, err := f.Read(buf[:]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(buf[:], content[:len(buf)]) {
|
||||
t.Errorf("expected %q, got %q", content[:len(buf)], buf[:])
|
||||
}
|
||||
// Check that the file pointer is at the expected offset.
|
||||
n, err = f.Seek(0, io.SeekCurrent)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != int64(len(buf)) {
|
||||
t.Errorf("expected file pointer to be at offset %d, got %d", len(buf), n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPipe(t *testing.T) {
|
||||
t.Parallel()
|
||||
r, w, err := os.Pipe()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue