Skip to content

Commit d045236

Browse files
qmuntalgopherbot
authored andcommitted
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 <[email protected]> Auto-Submit: Quim Muntal <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Pratt <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent cff53d5 commit d045236

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

windows/syscall_windows.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -725,20 +725,12 @@ func DurationSinceBoot() time.Duration {
725725
}
726726

727727
func Ftruncate(fd Handle, length int64) (err error) {
728-
curoffset, e := Seek(fd, 0, 1)
729-
if e != nil {
730-
return e
731-
}
732-
defer Seek(fd, curoffset, 0)
733-
_, e = Seek(fd, length, 0)
734-
if e != nil {
735-
return e
728+
type _FILE_END_OF_FILE_INFO struct {
729+
EndOfFile int64
736730
}
737-
e = SetEndOfFile(fd)
738-
if e != nil {
739-
return e
740-
}
741-
return nil
731+
var info _FILE_END_OF_FILE_INFO
732+
info.EndOfFile = length
733+
return SetFileInformationByHandle(fd, FileEndOfFileInfo, (*byte)(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info)))
742734
}
743735

744736
func Gettimeofday(tv *Timeval) (err error) {

0 commit comments

Comments
 (0)