tstor/pkg/cowutils/reflink_unix.go
royalcat e9df8925d1
Some checks failed
docker / build-docker (linux/amd64) (push) Failing after 18s
docker / build-docker (linux/386) (push) Successful in 1m57s
docker / build-docker (linux/arm64) (push) Successful in 7m22s
docker / build-docker (linux/arm/v7) (push) Successful in 7m53s
docker / build-docker (linux/arm64/v8) (push) Failing after 3h2m18s
storage rework
2024-06-15 01:14:44 +03:00

53 lines
1 KiB
Go

//!build +unix
package cowutils
import (
"errors"
"os"
"golang.org/x/sys/unix"
)
// reflink performs the actual reflink action without worrying about fallback
func reflink(dst, src *os.File) error {
srcFd := int(src.Fd())
dstFd := int(dst.Fd())
err := unix.IoctlFileClone(dstFd, srcFd)
if err != nil && errors.Is(err, unix.ENOTSUP) {
return ErrNotSupported
}
return err
}
func reflinkRange(dst, src *os.File, dstOffset, srcOffset, n int64) error {
srcFd := int(src.Fd())
dstFd := int(dst.Fd())
req := &unix.FileCloneRange{
Src_fd: int64(srcFd),
Src_offset: uint64(srcOffset),
Src_length: uint64(n),
Dest_offset: uint64(dstOffset),
}
err := unix.IoctlFileCloneRange(dstFd, req)
if err != nil && errors.Is(err, unix.ENOTSUP) {
return ErrNotSupported
}
return err
}
func copyFileRange(dst, src *os.File, dstOffset, srcOffset, n int64) (int64, error) {
srcFd := int(src.Fd())
dstFd := int(dst.Fd())
resN, err := unix.CopyFileRange(srcFd, &srcOffset, dstFd, &dstOffset, int(n), 0)
return int64(resN), err
}