From 41809caf7d430591a170ea34eb5ec05576929a2d Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Wed, 21 Sep 2016 18:34:04 +0300 Subject: [PATCH] SetFlags and SetCompression --- btrfs.go | 30 ++++++++++++++++++++++++++++++ ioctl_h.go | 5 +++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/btrfs.go b/btrfs.go index 0100677..3fe2100 100644 --- a/btrfs.go +++ b/btrfs.go @@ -11,6 +11,8 @@ import ( const SuperMagic = 0x9123683E +const xattrPrefix = "btrfs." + func Open(path string, ro bool) (*FS, error) { if ok, err := IsSubVolume(path); err != nil { return nil, err @@ -152,6 +154,10 @@ func (f *FS) GetFlags() (SubvolFlags, error) { return iocSubvolGetflags(f.f) } +func (f *FS) SetFlags(flags SubvolFlags) error { + return iocSubvolSetflags(f.f, flags) +} + func (f *FS) Sync() (err error) { if err = ioctl.Do(f.f, _BTRFS_IOC_START_SYNC, nil); err != nil { return @@ -194,3 +200,27 @@ func (f *FS) Receive(r io.Reader) error { func (f *FS) ReceiveTo(r io.Reader, mount string) error { return Receive(r, filepath.Join(f.f.Name(), mount)) } + +type Compression string + +const ( + CompressionNone = Compression("") + LZO = Compression("lzo") + ZLIB = Compression("zlib") +) + +func SetCompression(path string, v Compression) error { + var value []byte + if v != CompressionNone { + var err error + value, err = syscall.ByteSliceFromString(string(v)) + if err != nil { + return err + } + } + err := syscall.Setxattr(path, xattrPrefix+"compression", value, 0) + if err != nil { + return &os.PathError{Op: "setxattr", Path: path, Err: err} + } + return nil +} diff --git a/ioctl_h.go b/ioctl_h.go index a0fcb98..8a8ef98 100644 --- a/ioctl_h.go +++ b/ioctl_h.go @@ -695,8 +695,9 @@ func iocSubvolGetflags(f *os.File) (out SubvolFlags, err error) { return } -func iocSubvolSetflags(f *os.File, out *uint64) error { - return ioctl.Do(f, _BTRFS_IOC_SUBVOL_SETFLAGS, out) +func iocSubvolSetflags(f *os.File, flags SubvolFlags) error { + v := uint64(flags) + return ioctl.Do(f, _BTRFS_IOC_SUBVOL_SETFLAGS, &v) } func iocScrub(f *os.File, out *btrfs_ioctl_scrub_args) error {