fuse: deduplicate xattr code

This commit is contained in:
Michael Eischer 2023-07-08 17:41:45 +02:00
parent 8c02ebb029
commit f3c3b0f377
4 changed files with 30 additions and 35 deletions

View file

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

View file

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

View file

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

24
internal/fuse/xattr.go Normal file
View file

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