mirror of
https://github.com/dennwc/btrfs
synced 2025-02-10 00:27:00 +00:00
expose file clone ioctl; add test
This commit is contained in:
parent
4ac498bb4d
commit
08f5a6b26a
4
btrfs.go
4
btrfs.go
@ -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
|
||||
|
@ -2,6 +2,7 @@ package btrfs
|
||||
|
||||
import (
|
||||
"github.com/dennwc/btrfs/test"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user