| 1 |
//go:build linux |
| 2 |
|
| 3 |
package desync |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"encoding/binary" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"syscall" |
| 11 |
"unsafe" |
| 12 |
|
| 13 |
"github.com/pkg/errors" |
| 14 |
) |
| 15 |
|
| 16 |
// BLKGETSIZE64 ioctl |
| 17 |
const blkGetSize64 = 0x80081272 |
| 18 |
|
| 19 |
// FICLONERANGE ioctl |
| 20 |
const fiCloneRange = 0x4020940d |
| 21 |
|
| 22 |
// CanClone tries to determine if the filesystem allows cloning of blocks between |
| 23 |
// two files. It'll create two tempfiles in the same dirs and attempt to perform |
| 24 |
// a 0-byte long block clone. If that's successful it'll return true. |
| 25 |
func CanClone(dstFile, srcFile string) bool { |
| 26 |
dst, err := os.CreateTemp(filepath.Dir(dstFile), ".tmp") |
| 27 |
if err != nil { |
| 28 |
return false |
| 29 |
} |
| 30 |
defer os.Remove(dst.Name()) |
| 31 |
defer dst.Close() |
| 32 |
src, err := os.CreateTemp(filepath.Dir(srcFile), ".tmp") |
| 33 |
if err != nil { |
| 34 |
return false |
| 35 |
} |
| 36 |
defer os.Remove(src.Name()) |
| 37 |
defer src.Close() |
| 38 |
err = CloneRange(dst, src, 0, 0, 0) |
| 39 |
return err == nil |
| 40 |
} |
| 41 |
|
| 42 |
// CloneRange uses the FICLONERANGE ioctl to de-dupe blocks between two files |
| 43 |
// when using XFS or btrfs. Only works at block-boundaries. |
| 44 |
func CloneRange(dst, src *os.File, srcOffset, srcLength, dstOffset uint64) error { |
| 45 |
// Build a structure to hold the argument for this IOCTL |
| 46 |
// struct file_clone_range { |
| 47 |
// __s64 src_fd; |
| 48 |
// __u64 src_offset; |
| 49 |
// __u64 src_length; |
| 50 |
// __u64 dest_offset; |
| 51 |
// }; |
| 52 |
arg := new(bytes.Buffer) |
| 53 |
binary.Write(arg, binary.LittleEndian, uint64(src.Fd())) |
| 54 |
binary.Write(arg, binary.LittleEndian, srcOffset) |
| 55 |
binary.Write(arg, binary.LittleEndian, srcLength) |
| 56 |
binary.Write(arg, binary.LittleEndian, dstOffset) |
| 57 |
err := ioctl(dst.Fd(), fiCloneRange, uintptr(unsafe.Pointer(&arg.Bytes()[0]))) |
| 58 |
return errors.Wrapf(err, "failure cloning blocks from %s to %s", src.Name(), dst.Name()) |
| 59 |
} |
| 60 |
|
| 61 |
// GetFileSize determines the size, in Bytes, of the file located at the given |
| 62 |
// fileName. |
| 63 |
func GetFileSize(fileName string) (size uint64, err error) { |
| 64 |
info, err := os.Stat(fileName) |
| 65 |
if err != nil { |
| 66 |
return 0, err |
| 67 |
} |
| 68 |
fm := info.Mode() |
| 69 |
if isDevice(fm) { |
| 70 |
// When we are working with block devices, we can't simply use `Size()`, because it |
| 71 |
// will return zero instead of the expected device size. |
| 72 |
f, err := os.Open(fileName) |
| 73 |
if err != nil { |
| 74 |
return 0, err |
| 75 |
} |
| 76 |
err = ioctl(f.Fd(), blkGetSize64, uintptr(unsafe.Pointer(&size))) |
| 77 |
if err != nil { |
| 78 |
return 0, err |
| 79 |
} |
| 80 |
return size, nil |
| 81 |
} else { |
| 82 |
return uint64(info.Size()), nil |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func ioctl(fd, operation, argp uintptr) error { |
| 87 |
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, operation, argp) |
| 88 |
if e != 0 { |
| 89 |
return syscall.Errno(e) |
| 90 |
} |
| 91 |
return nil |
| 92 |
} |