Skip to content

Commit

Permalink
expose file clone ioctl; add test
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jan 6, 2017
1 parent 4ac498b commit 08f5a6b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 4 additions & 0 deletions btrfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (

const SuperMagic = 0x9123683E

func CloneFile(dst, src *os.File) error {
return iocClone(dst, src)
}

func Open(path string, ro bool) (*FS, error) {
if ok, err := IsSubVolume(path); err != nil {
return nil, err
Expand Down
39 changes: 39 additions & 0 deletions btrfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package btrfs

import (
"github.com/dennwc/btrfs/test"
"io"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -95,3 +96,41 @@ func TestCompression(t *testing.T) {
t.Fatalf("unexpected compression returned: %q", string(c))
}
}

func TestCloneFile(t *testing.T) {
dir, closer := btrfstest.New(t, sizeDef)
defer closer()

f1, err := os.Create(filepath.Join(dir, "1.dat"))
if err != nil {
t.Fatal(err)
}
defer f1.Close()

const data = "btrfs_test"
_, err = f1.WriteString(data)
if err != nil {
t.Fatal(err)
}

f2, err := os.Create(filepath.Join(dir, "2.dat"))
if err != nil {
t.Fatal(err)
}
defer f2.Close()

err = CloneFile(f2, f1)
if err != nil {
t.Fatal(err)
}

buf := make([]byte, len(data))
n, err := f2.Read(buf)
if err != nil && err != io.EOF {
t.Fatal(err)
}
buf = buf[:n]
if string(buf) != data {
t.Fatalf("wrong data returned: %q", string(buf))
}
}
4 changes: 2 additions & 2 deletions ioctl_h.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ func iocSync(f *os.File) error {
return ioctl.Do(f, _BTRFS_IOC_SYNC, nil)
}

func iocClone(f *os.File, out *int32) error {
return ioctl.Do(f, _BTRFS_IOC_CLONE, out)
func iocClone(dst, src *os.File) error {
return ioctl.Ioctl(dst, _BTRFS_IOC_CLONE, src.Fd())
}

func iocAddDev(f *os.File, out *btrfs_ioctl_vol_args) error {
Expand Down

0 comments on commit 08f5a6b

Please # to comment.