diff --git a/lib/file/preallocate_unix.go b/lib/file/preallocate_unix.go index d610b46b1..6f666410f 100644 --- a/lib/file/preallocate_unix.go +++ b/lib/file/preallocate_unix.go @@ -6,6 +6,7 @@ import ( "os" "sync" "sync/atomic" + "syscall" "github.com/rclone/rclone/fs" "golang.org/x/sys/unix" @@ -25,30 +26,38 @@ var ( const PreallocateImplemented = true // PreAllocate the file for performance reasons -func PreAllocate(size int64, out *os.File) error { +func PreAllocate(size int64, out *os.File) (err error) { if size <= 0 { return nil } + preAllocateMu.Lock() defer preAllocateMu.Unlock() - index := atomic.LoadInt32(&fallocFlagsIndex) -again: - if index >= int32(len(fallocFlags)) { - return nil // Fallocate is disabled - } - flags := fallocFlags[index] - err := unix.Fallocate(int(out.Fd()), flags, 0, size) - if err == unix.ENOTSUP { - // Try the next flags combination - index++ - atomic.StoreInt32(&fallocFlagsIndex, index) - fs.Debugf(nil, "preAllocate: got error on fallocate, trying combination %d/%d: %v", index, len(fallocFlags), err) - goto again - } - // Wrap important errors - if err == unix.ENOSPC { - return ErrDiskFull + for { + + index := atomic.LoadInt32(&fallocFlagsIndex) + again: + if index >= int32(len(fallocFlags)) { + return nil // Fallocate is disabled + } + flags := fallocFlags[index] + err = unix.Fallocate(int(out.Fd()), flags, 0, size) + if err == unix.ENOTSUP { + // Try the next flags combination + index++ + atomic.StoreInt32(&fallocFlagsIndex, index) + fs.Debugf(nil, "preAllocate: got error on fallocate, trying combination %d/%d: %v", index, len(fallocFlags), err) + goto again + + } + // Wrap important errors + if err == unix.ENOSPC { + return ErrDiskFull + } + if err != syscall.EINTR { + break + } } return err }