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 <alex.brainman@gmail.com>
Auto-Submit: Quim Muntal <quimmuntal@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
qmuntal
2024-10-28 14:49:53 +00:00
committed by Gopher Robot
parent cff53d5a33
commit d045236a8d
+5 -13
View File
@@ -725,20 +725,12 @@ func DurationSinceBoot() time.Duration {
} }
func Ftruncate(fd Handle, length int64) (err error) { func Ftruncate(fd Handle, length int64) (err error) {
curoffset, e := Seek(fd, 0, 1) type _FILE_END_OF_FILE_INFO struct {
if e != nil { EndOfFile int64
return e
} }
defer Seek(fd, curoffset, 0) var info _FILE_END_OF_FILE_INFO
_, e = Seek(fd, length, 0) info.EndOfFile = length
if e != nil { return SetFileInformationByHandle(fd, FileEndOfFileInfo, (*byte)(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info)))
return e
}
e = SetEndOfFile(fd)
if e != nil {
return e
}
return nil
} }
func Gettimeofday(tv *Timeval) (err error) { func Gettimeofday(tv *Timeval) (err error) {