From 9fa8c959ee331938a8a05fbe318ce61af1296972 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 20 Aug 2018 20:14:19 +0100 Subject: [PATCH] local: preallocate files on linux with fallocate(2) --- backend/local/preallocate_other.go | 2 +- backend/local/preallocate_unix.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 backend/local/preallocate_unix.go diff --git a/backend/local/preallocate_other.go b/backend/local/preallocate_other.go index 7afff5e29..fb71cad59 100644 --- a/backend/local/preallocate_other.go +++ b/backend/local/preallocate_other.go @@ -1,4 +1,4 @@ -//+build !windows +//+build !windows,!linux package local diff --git a/backend/local/preallocate_unix.go b/backend/local/preallocate_unix.go new file mode 100644 index 000000000..f7208ab0b --- /dev/null +++ b/backend/local/preallocate_unix.go @@ -0,0 +1,22 @@ +//+build linux + +package local + +import ( + "os" + + "golang.org/x/sys/unix" +) + +// preAllocate the file for performance reasons +func preAllocate(size int64, out *os.File) error { + if size <= 0 { + return nil + } + err := unix.Fallocate(int(out.Fd()), unix.FALLOC_FL_KEEP_SIZE, 0, size) + // FIXME could be doing something here + // if err == unix.ENOSPC { + // log.Printf("No space") + // } + return err +}