From 87a3c87e450de9b3a5580e744d638dde125951c7 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Wed, 7 Jun 2017 09:33:27 -0700 Subject: [PATCH] fileutil: return immediately if preallocating 0 bytes fallocate will return EINVAL, causing zeroing to the end of a 0 byte file to fail. Fixes #8045 --- pkg/fileutil/fileutil_test.go | 5 +++++ pkg/fileutil/preallocate.go | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/pkg/fileutil/fileutil_test.go b/pkg/fileutil/fileutil_test.go index 35f9ac5ac..cde2f516a 100644 --- a/pkg/fileutil/fileutil_test.go +++ b/pkg/fileutil/fileutil_test.go @@ -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 diff --git a/pkg/fileutil/preallocate.go b/pkg/fileutil/preallocate.go index 3270a3298..c747b7cf8 100644 --- a/pkg/fileutil/preallocate.go +++ b/pkg/fileutil/preallocate.go @@ -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) }