Skip to content

Commit

Permalink
Merge pull request #8046 from heyitsanthony/fix-falloc-0
Browse files Browse the repository at this point in the history
fileutil: return immediately if preallocating 0 bytes
  • Loading branch information
Anthony Romano committed Jun 7, 2017
2 parents 09abea5 + 87a3c87 commit 2991119
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/fileutil/fileutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func TestZeroToEnd(t *testing.T) {
}
defer f.Close()

// Ensure 0 size is a nop so zero-to-end on an empty file won't give EINVAL.
if err = ZeroToEnd(f); err != nil {
t.Fatal(err)
}

b := make([]byte, 1024)
for i := range b {
b[i] = 12
Expand Down
4 changes: 4 additions & 0 deletions pkg/fileutil/preallocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
// If the operation is unsupported, no error will be returned.
// Otherwise, the error encountered will be returned.
func Preallocate(f *os.File, sizeInBytes int64, extendFile bool) error {
if sizeInBytes == 0 {
// fallocate will return EINVAL if length is 0; skip
return nil
}
if extendFile {
return preallocExtend(f, sizeInBytes)
}
Expand Down

0 comments on commit 2991119

Please sign in to comment.