From d045236a8d4b76a8a8d1fee1c3db5e78a2871381 Mon Sep 17 00:00:00 2001 From: qmuntal Date: Wed, 23 Oct 2024 10:24:06 +0200 Subject: [PATCH] windows: implement Ftruncate using a single syscall on Windows Ftruncate can be implemented on Windows using a single syscall. This makes the implementation more efficient and less prone to races when used in combination with other Seek calls. Note that this is the x/sys counterpart for CL 618835. Change-Id: Ie9be356bd953ccce85c0dd87a5dcc6ccf4fec464 Reviewed-on: https://go-review.googlesource.com/c/sys/+/621935 Reviewed-by: Alex Brainman Auto-Submit: Quim Muntal LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Pratt Reviewed-by: Damien Neil --- windows/syscall_windows.go | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/windows/syscall_windows.go b/windows/syscall_windows.go index 63471b0e4..a07bf34af 100644 --- a/windows/syscall_windows.go +++ b/windows/syscall_windows.go @@ -725,20 +725,12 @@ func DurationSinceBoot() time.Duration { } func Ftruncate(fd Handle, length int64) (err error) { - curoffset, e := Seek(fd, 0, 1) - if e != nil { - return e - } - defer Seek(fd, curoffset, 0) - _, e = Seek(fd, length, 0) - if e != nil { - return e + type _FILE_END_OF_FILE_INFO struct { + EndOfFile int64 } - e = SetEndOfFile(fd) - if e != nil { - return e - } - return nil + var info _FILE_END_OF_FILE_INFO + info.EndOfFile = length + return SetFileInformationByHandle(fd, FileEndOfFileInfo, (*byte)(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info))) } func Gettimeofday(tv *Timeval) (err error) {