From f3c3b0f377ecfed00af10957e145d77ff275ce7d Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 8 Jul 2023 17:41:45 +0200 Subject: [PATCH] fuse: deduplicate xattr code --- internal/fuse/dir.go | 13 ++----------- internal/fuse/file.go | 13 ++----------- internal/fuse/link.go | 15 ++------------- internal/fuse/xattr.go | 24 ++++++++++++++++++++++++ 4 files changed, 30 insertions(+), 35 deletions(-) create mode 100644 internal/fuse/xattr.go diff --git a/internal/fuse/dir.go b/internal/fuse/dir.go index 7dc157b7e..242b4b03e 100644 --- a/internal/fuse/dir.go +++ b/internal/fuse/dir.go @@ -222,19 +222,10 @@ func (d *dir) Lookup(ctx context.Context, name string) (fs.Node, error) { } func (d *dir) Listxattr(_ context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error { - debug.Log("Listxattr(%v, %v)", d.node.Name, req.Size) - for _, attr := range d.node.ExtendedAttributes { - resp.Append(attr.Name) - } + nodeToXattrList(d.node, req, resp) return nil } func (d *dir) Getxattr(_ context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error { - debug.Log("Getxattr(%v, %v, %v)", d.node.Name, req.Name, req.Size) - attrval := d.node.GetExtendedAttribute(req.Name) - if attrval != nil { - resp.Xattr = attrval - return nil - } - return fuse.ErrNoXattr + return nodeGetXattr(d.node, req, resp) } diff --git a/internal/fuse/file.go b/internal/fuse/file.go index fd9d8ccc2..aec39273a 100644 --- a/internal/fuse/file.go +++ b/internal/fuse/file.go @@ -167,19 +167,10 @@ func (f *openFile) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.R } func (f *file) Listxattr(_ context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error { - debug.Log("Listxattr(%v, %v)", f.node.Name, req.Size) - for _, attr := range f.node.ExtendedAttributes { - resp.Append(attr.Name) - } + nodeToXattrList(f.node, req, resp) return nil } func (f *file) Getxattr(_ context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error { - debug.Log("Getxattr(%v, %v, %v)", f.node.Name, req.Name, req.Size) - attrval := f.node.GetExtendedAttribute(req.Name) - if attrval != nil { - resp.Xattr = attrval - return nil - } - return fuse.ErrNoXattr + return nodeGetXattr(f.node, req, resp) } diff --git a/internal/fuse/link.go b/internal/fuse/link.go index 43b00a855..3aea8b06e 100644 --- a/internal/fuse/link.go +++ b/internal/fuse/link.go @@ -6,8 +6,6 @@ package fuse import ( "context" - "github.com/restic/restic/internal/debug" - "github.com/anacrolix/fuse" "github.com/anacrolix/fuse/fs" "github.com/restic/restic/internal/restic" @@ -50,19 +48,10 @@ func (l *link) Attr(_ context.Context, a *fuse.Attr) error { } func (l *link) Listxattr(_ context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error { - debug.Log("Listxattr(%v, %v)", l.node.Name, req.Size) - for _, attr := range l.node.ExtendedAttributes { - resp.Append(attr.Name) - } + nodeToXattrList(l.node, req, resp) return nil } func (l *link) Getxattr(_ context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error { - debug.Log("Getxattr(%v, %v, %v)", l.node.Name, req.Name, req.Size) - attrval := l.node.GetExtendedAttribute(req.Name) - if attrval != nil { - resp.Xattr = attrval - return nil - } - return fuse.ErrNoXattr + return nodeGetXattr(l.node, req, resp) } diff --git a/internal/fuse/xattr.go b/internal/fuse/xattr.go new file mode 100644 index 000000000..f208938a6 --- /dev/null +++ b/internal/fuse/xattr.go @@ -0,0 +1,24 @@ +package fuse + +import ( + "github.com/anacrolix/fuse" + "github.com/restic/restic/internal/debug" + "github.com/restic/restic/internal/restic" +) + +func nodeToXattrList(node *restic.Node, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) { + debug.Log("Listxattr(%v, %v)", node.Name, req.Size) + for _, attr := range node.ExtendedAttributes { + resp.Append(attr.Name) + } +} + +func nodeGetXattr(node *restic.Node, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error { + debug.Log("Getxattr(%v, %v, %v)", node.Name, req.Name, req.Size) + attrval := node.GetExtendedAttribute(req.Name) + if attrval != nil { + resp.Xattr = attrval + return nil + } + return fuse.ErrNoXattr +}