2021-08-17 12:46:50 +00:00
|
|
|
//go:build linux
|
2020-02-12 20:00:17 +00:00
|
|
|
// +build linux
|
2016-01-26 09:26:27 +00:00
|
|
|
|
2016-05-19 14:12:46 +00:00
|
|
|
package tools
|
2016-01-26 09:26:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2019-08-04 04:42:25 +00:00
|
|
|
"io/ioutil"
|
2016-02-04 20:42:43 +00:00
|
|
|
"os"
|
2016-01-26 09:26:27 +00:00
|
|
|
|
2020-09-23 09:32:15 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-01-26 09:26:27 +00:00
|
|
|
)
|
|
|
|
|
2019-08-04 04:42:25 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 09:26:27 +00:00
|
|
|
func CloneFile(writer io.Writer, reader io.Reader) (bool, error) {
|
|
|
|
fdst, fdstFound := writer.(*os.File)
|
|
|
|
fsrc, fsrcFound := reader.(*os.File)
|
|
|
|
if fdstFound && fsrcFound {
|
2020-09-23 09:32:15 +00:00
|
|
|
if err := unix.IoctlFileClone(int(fdst.Fd()), int(fsrc.Fd())); err != nil {
|
2016-01-26 09:26:27 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-07-27 07:08:17 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|