From ac687d53593d8215b881db92fe2d1bbbd2d2d774 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Wed, 9 Sep 2020 11:17:19 +0200 Subject: [PATCH] v2api: add Darwin xattr support --- internal/fusefrontend/node.go | 8 +- internal/fusefrontend/node_xattr_darwin.go | 106 +++++++++++++++++++++ 2 files changed, 110 insertions(+), 4 deletions(-) diff --git a/internal/fusefrontend/node.go b/internal/fusefrontend/node.go index 00d06f5..bc4eb3b 100644 --- a/internal/fusefrontend/node.go +++ b/internal/fusefrontend/node.go @@ -480,7 +480,7 @@ func (n *Node) Rename(ctx context.Context, name string, newParent fs.InodeEmbedd // Easy case. rn := n.rootNode() if rn.args.PlaintextNames { - return fs.ToErrno(unix.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags))) + return fs.ToErrno(syscallcompat.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags))) } // Long destination file name: create .name file nameFileAlreadyThere := false @@ -498,8 +498,8 @@ func (n *Node) Rename(ctx context.Context, name string, newParent fs.InodeEmbedd } // Actual rename tlog.Debug.Printf("Renameat %d/%s -> %d/%s\n", dirfd, cName, dirfd2, cName2) - err = unix.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags)) - if (flags&unix.RENAME_NOREPLACE == 0) && (err == syscall.ENOTEMPTY || err == syscall.EEXIST) { + err = syscallcompat.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags)) + if (flags&syscallcompat.RENAME_NOREPLACE == 0) && (err == syscall.ENOTEMPTY || err == syscall.EEXIST) { // If an empty directory is overwritten we will always get an error as // the "empty" directory will still contain gocryptfs.diriv. // Interestingly, ext4 returns ENOTEMPTY while xfs returns EEXIST. @@ -507,7 +507,7 @@ func (n *Node) Rename(ctx context.Context, name string, newParent fs.InodeEmbedd // again. tlog.Debug.Printf("Rename: Handling ENOTEMPTY") if n2.Rmdir(ctx, newName) == 0 { - err = unix.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags)) + err = syscallcompat.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags)) } } if err != nil { diff --git a/internal/fusefrontend/node_xattr_darwin.go b/internal/fusefrontend/node_xattr_darwin.go index e69de29..82c0dff 100644 --- a/internal/fusefrontend/node_xattr_darwin.go +++ b/internal/fusefrontend/node_xattr_darwin.go @@ -0,0 +1,106 @@ +package fusefrontend + +import ( + "syscall" + + "golang.org/x/sys/unix" + + "github.com/hanwen/go-fuse/v2/fs" + + "github.com/rfjakob/gocryptfs/internal/syscallcompat" +) + +// On Darwin we have to unset XATTR_NOSECURITY 0x0008 +func filterXattrSetFlags(flags int) int { + // See https://opensource.apple.com/source/xnu/xnu-1504.15.3/bsd/sys/xattr.h.auto.html + const XATTR_NOSECURITY = 0x0008 + + return flags &^ XATTR_NOSECURITY +} + +func (n *Node) getXAttr(cAttr string) (out []byte, errno syscall.Errno) { + dirfd, cName, errno := n.prepareAtSyscall("") + if errno != 0 { + return + } + defer syscall.Close(dirfd) + + // O_NONBLOCK to not block on FIFOs. + fd, err := syscallcompat.Openat(dirfd, cName, syscall.O_RDONLY|syscall.O_NONBLOCK, 0) + if err != nil { + return nil, fs.ToErrno(err) + } + defer syscall.Close(fd) + + cData, err := syscallcompat.Fgetxattr(fd, cAttr) + if err != nil { + return nil, fs.ToErrno(err) + } + + return cData, 0 +} + +func (n *Node) setXAttr(cAttr string, cData []byte, flags uint32) (errno syscall.Errno) { + dirfd, cName, errno := n.prepareAtSyscall("") + if errno != 0 { + return + } + defer syscall.Close(dirfd) + + // O_NONBLOCK to not block on FIFOs. + fd, err := syscallcompat.Openat(dirfd, cName, syscall.O_WRONLY|syscall.O_NONBLOCK, 0) + // Directories cannot be opened read-write. Retry. + if err == syscall.EISDIR { + fd, err = syscallcompat.Openat(dirfd, cName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NONBLOCK, 0) + } + if err != nil { + fs.ToErrno(err) + } + defer syscall.Close(fd) + + err = unix.Fsetxattr(fd, cAttr, cData, int(flags)) + return fs.ToErrno(err) +} + +func (n *Node) removeXAttr(cAttr string) (errno syscall.Errno) { + dirfd, cName, errno := n.prepareAtSyscall("") + if errno != 0 { + return + } + defer syscall.Close(dirfd) + + // O_NONBLOCK to not block on FIFOs. + fd, err := syscallcompat.Openat(dirfd, cName, syscall.O_WRONLY|syscall.O_NONBLOCK, 0) + // Directories cannot be opened read-write. Retry. + if err == syscall.EISDIR { + fd, err = syscallcompat.Openat(dirfd, cName, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NONBLOCK, 0) + } + if err != nil { + return fs.ToErrno(err) + } + defer syscall.Close(fd) + + err = unix.Fremovexattr(fd, cAttr) + return fs.ToErrno(err) +} + +func (n *Node) listXAttr() (out []string, errno syscall.Errno) { + dirfd, cName, errno := n.prepareAtSyscall("") + if errno != 0 { + return + } + defer syscall.Close(dirfd) + + // O_NONBLOCK to not block on FIFOs. + fd, err := syscallcompat.Openat(dirfd, cName, syscall.O_RDONLY|syscall.O_NONBLOCK, 0) + if err != nil { + return nil, fs.ToErrno(err) + } + defer syscall.Close(fd) + + cNames, err := syscallcompat.Flistxattr(fd) + if err != nil { + return nil, fs.ToErrno(err) + } + return cNames, 0 +}