-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcow_linux.go
42 lines (37 loc) · 826 Bytes
/
cow_linux.go
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
//+build cgo
package copyfile
import (
"os"
"syscall"
)
// #include <errno.h>
// #include <sys/ioctl.h>
// #include <linux/fs.h>
import "C"
func (c *Copier) copySpecialised(srcFile *os.File, dest string, mode os.FileMode) error {
destFile, err := os.Create(dest)
if err != nil {
return err
}
err = c.ficlone(srcFile, destFile, mode)
destFile.Close()
if err != nil {
return c.WriteFile(srcFile, dest, mode)
}
return nil
}
func (c *Copier) ficlone(srcFile, destFile *os.File, mode os.FileMode) error {
_, _, err := syscall.Syscall(C.FICLONE, destFile.Fd(), srcFile.Fd(), 0)
switch err {
case 0:
return nil
case C.EBADF, C.EOPNOTSUPP, C.ENOSYS:
// These error codes indicate the filesystem doesn't support reflink.
if !c.AlwaysCOW {
c.cowFailed = true
}
return err
default:
return err
}
}