v2api: add Darwin xattr support

This commit is contained in:
Jakob Unterwurzacher 2020-09-09 11:17:19 +02:00
parent 8b1df08b8a
commit ac687d5359
2 changed files with 110 additions and 4 deletions

View File

@ -480,7 +480,7 @@ func (n *Node) Rename(ctx context.Context, name string, newParent fs.InodeEmbedd
// Easy case. // Easy case.
rn := n.rootNode() rn := n.rootNode()
if rn.args.PlaintextNames { 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 // Long destination file name: create .name file
nameFileAlreadyThere := false nameFileAlreadyThere := false
@ -498,8 +498,8 @@ func (n *Node) Rename(ctx context.Context, name string, newParent fs.InodeEmbedd
} }
// Actual rename // Actual rename
tlog.Debug.Printf("Renameat %d/%s -> %d/%s\n", dirfd, cName, dirfd2, cName2) tlog.Debug.Printf("Renameat %d/%s -> %d/%s\n", dirfd, cName, dirfd2, cName2)
err = unix.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags)) err = syscallcompat.Renameat2(dirfd, cName, dirfd2, cName2, uint(flags))
if (flags&unix.RENAME_NOREPLACE == 0) && (err == syscall.ENOTEMPTY || err == syscall.EEXIST) { 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 // If an empty directory is overwritten we will always get an error as
// the "empty" directory will still contain gocryptfs.diriv. // the "empty" directory will still contain gocryptfs.diriv.
// Interestingly, ext4 returns ENOTEMPTY while xfs returns EEXIST. // 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. // again.
tlog.Debug.Printf("Rename: Handling ENOTEMPTY") tlog.Debug.Printf("Rename: Handling ENOTEMPTY")
if n2.Rmdir(ctx, newName) == 0 { 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 { if err != nil {

View File

@ -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
}