Skip to content

Commit

Permalink
fix: actually use the size hint in util_windows.go
Browse files Browse the repository at this point in the history
This code was broken, it computed the size hint to do nothing with it.
  • Loading branch information
Jorropo committed Aug 2, 2022
1 parent d2bea22 commit 669f34a
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package flatfs

import (
"bytes"
"io/ioutil"
"io"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -93,10 +93,30 @@ func readFileOnce(filename string) ([]byte, error) {
// 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
n += fi.Size()
}

data := make([]byte, n)
remaining := data
var total int64
for {
n, err := f.Read(remaining)
remaining = remaining[n:]
total += int64(n)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}

// Size hint undershoot, let's grow the buffer
if len(remaining) == 0 {
dataSize := len(data)
data = append(data, 0) // use append's growth pattern
remaining = data[dataSize:]
}
}

return ioutil.ReadAll(f)
return data[:total], nil
}

0 comments on commit 669f34a

Please sign in to comment.