Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy-on-write support for BTRFS filesystem #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions shutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func CopyFile(src, dst string, followSymlinks bool) (error) {
}
defer fdst.Close()

if success, _ := clonefile(fdst, fsrc); success {
return nil
}

size, err := io.Copy(fdst, fsrc)
if err != nil {
return err
Expand Down
11 changes: 11 additions & 0 deletions shutil_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build !linux !cgo

package shutil

import (
"os"
)

func clonefile(fdst *os.File, fsrc *os.File) (bool, error) {
return false, nil
}
29 changes: 29 additions & 0 deletions shutil_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// +build linux,cgo

package shutil

/*
#include <sys/ioctl.h>

#undef BTRFS_IOCTL_MAGIC
#define BTRFS_IOCTL_MAGIC 0x94
#undef BTRFS_IOC_CLONE
#define BTRFS_IOC_CLONE _IOW (BTRFS_IOCTL_MAGIC, 9, int)
*/
import "C"

import (
"os"
"syscall"
)

const (
BtrfsIocClone = C.BTRFS_IOC_CLONE
)

func clonefile(fdst *os.File, fsrc *os.File) (bool, error) {
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fdst.Fd(), BtrfsIocClone, fsrc.Fd()); err != 0 {
return false, err
}
return true, nil
}