rclone/lib/file/preallocate_unix.go
Maxwell Calman b015012d8b Add resume feature
Added an interface and machinery for resuming failed uploads.
Implemented this interface in the local backend.
Later on it can be implemented by any supporting backend.

Fixes #87
2021-11-17 20:33:22 +03:00

72 lines
1.4 KiB
Go

//go:build linux
// +build linux
package file
import (
"os"
"sync"
"sync/atomic"
"syscall"
"golang.org/x/sys/unix"
)
var (
fallocFlags = [...]uint32{
unix.FALLOC_FL_KEEP_SIZE, // Default
unix.FALLOC_FL_KEEP_SIZE | unix.FALLOC_FL_PUNCH_HOLE, // for ZFS #3066
}
fallocFlagsIndex int32
preAllocateMu sync.Mutex
)
// PreallocateImplemented is a constant indicating whether the
// implementation of Preallocate actually does anything.
const PreallocateImplemented = true
// PreAllocate the file for performance reasons
func PreAllocate(size int64, out *os.File) (err error) {
if size <= 0 {
return nil
}
preAllocateMu.Lock()
defer preAllocateMu.Unlock()
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)
goto again
}
// Wrap important errors
if err == unix.ENOSPC {
return ErrDiskFull
}
if err != syscall.EINTR {
break
}
}
return err
}
// SetSparseImplemented is a constant indicating whether the
// implementation of SetSparse actually does anything.
const SetSparseImplemented = false
// SetSparse makes the file be a sparse file
func SetSparse(out *os.File) error {
return nil
}