2018-08-20 19:14:19 +00:00
|
|
|
//+build linux
|
|
|
|
|
2020-03-24 16:26:45 +00:00
|
|
|
package file
|
2018-08-20 19:14:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-03-25 15:22:40 +00:00
|
|
|
"sync/atomic"
|
2018-08-20 19:14:19 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-08-20 19:14:19 +00:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2019-03-25 15:22:40 +00: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-24 16:26:45 +00:00
|
|
|
// PreAllocate the file for performance reasons
|
|
|
|
func PreAllocate(size int64, out *os.File) error {
|
2018-08-20 19:14:19 +00:00
|
|
|
if size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-03-25 15:22:40 +00: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-20 19:14:19 +00:00
|
|
|
// FIXME could be doing something here
|
|
|
|
// if err == unix.ENOSPC {
|
|
|
|
// log.Printf("No space")
|
|
|
|
// }
|
|
|
|
return err
|
|
|
|
}
|
2020-03-06 12:41:48 +00:00
|
|
|
|
2020-03-24 16:26:45 +00:00
|
|
|
// SetSparse makes the file be a sparse file
|
|
|
|
func SetSparse(out *os.File) error {
|
2020-03-06 12:41:48 +00:00
|
|
|
return nil
|
|
|
|
}
|