2018-08-21 03:14:19 +08:00
|
|
|
//+build linux
|
|
|
|
|
2020-03-25 00:26:45 +08:00
|
|
|
package file
|
2018-08-21 03:14:19 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-03-25 23:22:40 +08:00
|
|
|
"sync/atomic"
|
2018-08-21 03:14:19 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-08-21 03:14:19 +08:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2019-03-25 23:22:40 +08:00
|
|
|
var (
|
|
|
|
fallocFlags = [...]uint32{
|
|
|
|
unix.FALLOC_FL_KEEP_SIZE, // Default
|
|
|
|
unix.FALLOC_FL_KEEP_SIZE | unix.FALLOC_FL_PUNCH_HOLE, // for ZFS #3066
|
|
|
|
}
|
|
|
|
fallocFlagsIndex int32
|
|
|
|
)
|
|
|
|
|
2020-03-25 00:26:45 +08:00
|
|
|
// PreAllocate the file for performance reasons
|
|
|
|
func PreAllocate(size int64, out *os.File) error {
|
2018-08-21 03:14:19 +08:00
|
|
|
if size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-25 23:22:40 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2018-08-21 03:14:19 +08:00
|
|
|
// FIXME could be doing something here
|
|
|
|
// if err == unix.ENOSPC {
|
|
|
|
// log.Printf("No space")
|
|
|
|
// }
|
|
|
|
return err
|
|
|
|
}
|
2020-03-06 20:41:48 +08:00
|
|
|
|
2020-03-25 00:26:45 +08:00
|
|
|
// SetSparse makes the file be a sparse file
|
|
|
|
func SetSparse(out *os.File) error {
|
2020-03-06 20:41:48 +08:00
|
|
|
return nil
|
|
|
|
}
|