git-lfs/tools/util_linux.go
brian m. carlson a27bda0eb9
tools: remove the use of cgo on Linux
We currently import the C package solely for a single value: the FICLONE
ioctl, which is a constant.  Since this is a constant and also part of
the Linux kernel ABI, there's no reason we need to have the C compiler
generate it for us every time we build.  We can instead hard-code the
constant in place, since it will never change.

This avoids the need for cgo in order to have a functional invocation of
git lfs dedup on Linux.
2020-02-14 15:16:02 +00:00

67 lines
1.5 KiB
Go

// +build linux
package tools
import (
"io"
"io/ioutil"
"os"
"syscall"
)
const (
// This is the FICLONE ioctl value found in linux/fs.h on a typical
// system. It's equivalent to the older BTRFS_IOC_CLONE. We hard-code
// the value here to avoid a dependency on cgo.
ioctlFiClone = 0x40049409
)
// CheckCloneFileSupported runs explicit test of clone file on supplied directory.
// This function creates some (src and dst) file in the directory and remove after test finished.
//
// If check failed (e.g. directory is read-only), returns err.
func CheckCloneFileSupported(dir string) (supported bool, err error) {
src, err := ioutil.TempFile(dir, "src")
if err != nil {
return false, err
}
defer os.Remove(src.Name())
dst, err := ioutil.TempFile(dir, "dst")
if err != nil {
return false, err
}
defer os.Remove(dst.Name())
if ok, err := CloneFile(dst, src); err != nil {
return false, err
} else {
return ok, nil
}
}
func CloneFile(writer io.Writer, reader io.Reader) (bool, error) {
fdst, fdstFound := writer.(*os.File)
fsrc, fsrcFound := reader.(*os.File)
if fdstFound && fsrcFound {
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fdst.Fd(), ioctlFiClone, fsrc.Fd()); err != 0 {
return false, err
}
return true, nil
}
return false, nil
}
func CloneFileByPath(dst, src string) (bool, error) {
srcFile, err := os.Open(src)
if err != nil {
return false, err
}
dstFile, err := os.Create(dst) //truncating, it if it already exists.
if err != nil {
return false, err
}
return CloneFile(dstFile, srcFile)
}