Prechádzať zdrojové kódy

Merge pull request #3340 from xiang90/fix_perallocate

pkg/fileutil: treat not support error as nil error in preallocate
Xiang Li 10 rokov pred
rodič
commit
7cf9770e12

+ 5 - 0
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
 }

+ 15 - 1
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
 }