Skip to content

Commit

Permalink
Revert "windows: support nil done parameter in ReadFile and WriteFile"
Browse files Browse the repository at this point in the history
This reverts CL 559375.

Reason for revert: introduced a different regression (golang/go#65378).

Fixes golang/go#65378.
Updates golang/go#65365.

Change-Id: Ie2a602415913b04b9d9b65fee5c6a54c0267b35e
Cq-Include-Trybots: luci.golang.try:x_sys-gotip-windows-amd64-longtest
Reviewed-on: https://go-review.googlesource.com/c/sys/+/559502
Auto-Submit: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
  • Loading branch information
Bryan Mills authored and gopherbot committed Jan 30, 2024
1 parent 628365d commit 511ec84
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 69 deletions.
20 changes: 6 additions & 14 deletions windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,31 +572,23 @@ func Write(fd Handle, p []byte) (n int, err error) {
}

func ReadFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error {
var n uint32
err := readFile(fd, p, &n, overlapped)
err := readFile(fd, p, done, overlapped)
if raceenabled {
if n > 0 {
raceWriteRange(unsafe.Pointer(&p[0]), int(n))
if *done > 0 {
raceWriteRange(unsafe.Pointer(&p[0]), int(*done))
}
raceAcquire(unsafe.Pointer(&ioSync))
}
if done != nil {
*done = n
}
return err
}

func WriteFile(fd Handle, p []byte, done *uint32, overlapped *Overlapped) error {
if raceenabled {
raceReleaseMerge(unsafe.Pointer(&ioSync))
}
var n uint32
err := writeFile(fd, p, &n, overlapped)
if raceenabled && n > 0 {
raceReadRange(unsafe.Pointer(&p[0]), int(n))
}
if done != nil {
*done = n
err := writeFile(fd, p, done, overlapped)
if raceenabled && *done > 0 {
raceReadRange(unsafe.Pointer(&p[0]), int(*done))
}
return err
}
Expand Down
55 changes: 0 additions & 55 deletions windows/syscall_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,58 +1275,3 @@ uintptr_t beep(void) {
t.Fatal("LoadLibraryEx unexpectedly found beep.dll")
}
}

func TestReadWriteFileOverlapped(t *testing.T) {
name := filepath.Join(t.TempDir(), "test.txt")
fd, err := windows.CreateFile(windows.StringToUTF16Ptr(name), windows.GENERIC_READ|windows.GENERIC_WRITE, 0, nil, windows.CREATE_NEW, windows.FILE_FLAG_OVERLAPPED, 0)
if err != nil {
t.Fatal(err)
}
defer windows.CloseHandle(fd)

content := []byte("hello")
// Test that we can write to a file using overlapped I/O.
var ow windows.Overlapped
ow.HEvent, err = windows.CreateEvent(nil, 0, 0, nil)
if err != nil {
t.Fatal(err)
}
defer windows.CloseHandle(ow.HEvent)
if err := windows.WriteFile(fd, content, nil, &ow); err != nil && err != windows.ERROR_IO_PENDING {
t.Fatal(err)
}
if _, err := windows.WaitForSingleObject(ow.HEvent, windows.INFINITE); err != nil {
t.Fatal(err)
}
var n uint32
if err := windows.GetOverlappedResult(fd, &ow, &n, true); err != nil {
t.Fatal(err)
}
if n != uint32(len(content)) {
t.Fatalf("got %d bytes written; want %d", n, len(content))
}

// Test that we can read from a file using overlapped I/O.
var or windows.Overlapped
or.HEvent, err = windows.CreateEvent(nil, 0, 0, nil)
if err != nil {
t.Fatal(err)
}
defer windows.CloseHandle(ow.HEvent)
buf := make([]byte, len(content))
if err := windows.ReadFile(fd, buf, nil, &or); err != nil && err != windows.ERROR_IO_PENDING {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
if _, err := windows.WaitForSingleObject(or.HEvent, windows.INFINITE); err != nil {
t.Fatal(err)
}
if err := windows.GetOverlappedResult(fd, &or, &n, true); err != nil {
t.Fatal(err)
}
if string(buf) != string(content) {
t.Fatalf("got %q; want %q", buf, content)
}
}

0 comments on commit 511ec84

Please sign in to comment.