From fcf1a593519896984a8ab384e9d04d7c6cef27cd Mon Sep 17 00:00:00 2001 From: Jorropo Date: Tue, 2 Aug 2022 15:47:38 +0200 Subject: [PATCH] fix: actually use the size hint in util_windows.go This code was broken, it computed the size hint to do nothing with it. --- util_windows.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/util_windows.go b/util_windows.go index 7a0c3af..ba5c00d 100644 --- a/util_windows.go +++ b/util_windows.go @@ -12,7 +12,6 @@ package flatfs import ( "bytes" - "io" "os" "path/filepath" "strconv" @@ -83,19 +82,20 @@ func readFileOnce(filename string) ([]byte, error) { defer f.Close() // It's a good but not certain bet that FileInfo will tell us exactly how much to // read, so let's try it but be prepared for the answer to be wrong. - var n int64 = bytes.MinRead + var n int = bytes.MinRead if fi, err := f.Stat(); err == nil { - // As initial capacity for readAll, use Size + a little extra in case Size - // is zero, and to avoid another allocation after Read has filled the - // buffer. The readAll call will read into its allocated internal buffer - // cheaply. If the size was wrong, we'll either waste some space off the end - // or reallocate as needed, but in the overwhelmingly common case we'll get - // it just right. - if size := fi.Size() + bytes.MinRead; size > n { - n = size + if sz := int(fi.Size()); sz > n { + n = sz } } - return io.ReadAll(f) + var buf bytes.Buffer + buf.Grow(n) + _, err = buf.ReadFrom(f) + if err != nil { + return nil, err + } + + return buf.Bytes(), nil }