forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_linux.go
More file actions
61 lines (52 loc) · 1.32 KB
/
util_linux.go
File metadata and controls
61 lines (52 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//go:build linux
// +build linux
package tools
import (
"io"
"io/ioutil"
"os"
"golang.org/x/sys/unix"
)
// 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 := unix.IoctlFileClone(int(fdst.Fd()), int(fsrc.Fd())); err != nil {
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)
}