From 3ca5482251defc7c2a5b2f0b43c6d8581ba37941 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 20 Aug 2015 11:02:56 -0700 Subject: [PATCH] pkg/fileutil: treat not support error as nil error in preallocate --- pkg/fileutil/perallocate_unsupported.go | 5 +++++ pkg/fileutil/preallocate.go | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/fileutil/perallocate_unsupported.go b/pkg/fileutil/perallocate_unsupported.go index 15e9fc811..c1a952bb7 100644 --- a/pkg/fileutil/perallocate_unsupported.go +++ b/pkg/fileutil/perallocate_unsupported.go @@ -18,6 +18,11 @@ package fileutil import "os" +// Preallocate tries to allocate the space for given +// file. This operation is only supported on linux by a +// few filesystems (btrfs, ext4, etc.). +// If the operation is unsupported, no error will be returned. +// Otherwise, the error encountered will be returned. func Preallocate(f *os.File, sizeInBytes int) error { return nil } diff --git a/pkg/fileutil/preallocate.go b/pkg/fileutil/preallocate.go index f579bdf48..c4bd4f4c8 100644 --- a/pkg/fileutil/preallocate.go +++ b/pkg/fileutil/preallocate.go @@ -21,8 +21,22 @@ import ( "syscall" ) +// Preallocate tries to allocate the space for given +// file. This operation is only supported on linux by a +// few filesystems (btrfs, ext4, etc.). +// If the operation is unsupported, no error will be returned. +// Otherwise, the error encountered will be returned. func Preallocate(f *os.File, sizeInBytes int) error { // use mode = 1 to keep size // see FALLOC_FL_KEEP_SIZE - return syscall.Fallocate(int(f.Fd()), 1, 0, int64(sizeInBytes)) + err := syscall.Fallocate(int(f.Fd()), 1, 0, int64(sizeInBytes)) + if err != nil { + errno, ok := err.(syscall.Errno) + // treat not support as nil error + if ok && errno == syscall.ENOTSUP { + return nil + } + return err + } + return nil }