2024-06-13 20:21:00 +00:00
|
|
|
//go:build darwin || freebsd || linux || solaris || windows
|
|
|
|
// +build darwin freebsd linux solaris windows
|
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
package fs
|
2024-06-13 20:21:00 +00:00
|
|
|
|
|
|
|
import (
|
2024-12-03 00:38:43 +00:00
|
|
|
"bytes"
|
2024-06-13 20:21:00 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-06-13 20:52:31 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2024-06-13 20:21:00 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-12-03 00:38:43 +00:00
|
|
|
"github.com/restic/restic/internal/filter"
|
2024-08-26 21:03:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2024-06-13 20:21:00 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
func setAndVerifyXattr(t *testing.T, file string, attrs []restic.ExtendedAttribute) {
|
2024-06-13 20:52:31 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// windows seems to convert the xattr name to upper case
|
|
|
|
for i := range attrs {
|
|
|
|
attrs[i].Name = strings.ToUpper(attrs[i].Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
node := &restic.Node{
|
2024-07-09 17:51:44 +00:00
|
|
|
Type: restic.NodeTypeFile,
|
2024-06-13 20:21:00 +00:00
|
|
|
ExtendedAttributes: attrs,
|
|
|
|
}
|
2024-11-15 20:55:29 +00:00
|
|
|
/* restore all xattrs */
|
|
|
|
rtest.OK(t, nodeRestoreExtendedAttributes(node, file, func(_ string) bool { return true }))
|
2024-06-13 20:21:00 +00:00
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
nodeActual := &restic.Node{
|
2024-07-09 17:51:44 +00:00
|
|
|
Type: restic.NodeTypeFile,
|
2024-06-13 20:52:31 +00:00
|
|
|
}
|
2024-08-26 20:35:22 +00:00
|
|
|
rtest.OK(t, nodeFillExtendedAttributes(nodeActual, file, false))
|
2024-06-13 20:21:00 +00:00
|
|
|
|
2024-08-26 20:35:22 +00:00
|
|
|
rtest.Assert(t, nodeActual.Equals(*node), "xattr mismatch got %v expected %v", nodeActual.ExtendedAttributes, node.ExtendedAttributes)
|
2024-06-13 20:21:00 +00:00
|
|
|
}
|
|
|
|
|
2024-12-03 00:38:43 +00:00
|
|
|
func setAndVerifyXattrWithSelectFilter(t *testing.T, file string, testAttr []testXattrToRestore, xattrSelectFilter func(_ string) bool) {
|
|
|
|
attrs := make([]restic.ExtendedAttribute, len(testAttr))
|
|
|
|
for i := range testAttr {
|
|
|
|
attrs[i] = testAttr[i].xattr
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// windows seems to convert the xattr name to upper case
|
|
|
|
for i := range attrs {
|
|
|
|
attrs[i].Name = strings.ToUpper(attrs[i].Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node := &restic.Node{
|
|
|
|
Type: restic.NodeTypeFile,
|
|
|
|
ExtendedAttributes: attrs,
|
|
|
|
}
|
|
|
|
|
|
|
|
rtest.OK(t, nodeRestoreExtendedAttributes(node, file, xattrSelectFilter))
|
|
|
|
|
|
|
|
nodeActual := &restic.Node{
|
|
|
|
Type: restic.NodeTypeFile,
|
|
|
|
}
|
|
|
|
rtest.OK(t, nodeFillExtendedAttributes(nodeActual, file, false))
|
|
|
|
|
|
|
|
// Check nodeActual to make sure only xattrs we expect are there
|
|
|
|
for _, testAttr := range testAttr {
|
|
|
|
xattrFound := false
|
|
|
|
xattrRestored := false
|
|
|
|
for _, restoredAttr := range nodeActual.ExtendedAttributes {
|
|
|
|
if restoredAttr.Name == testAttr.xattr.Name {
|
|
|
|
xattrFound = true
|
|
|
|
xattrRestored = bytes.Equal(restoredAttr.Value, testAttr.xattr.Value)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if testAttr.shouldRestore {
|
|
|
|
rtest.Assert(t, xattrFound, "xattr %s not restored", testAttr.xattr.Name)
|
|
|
|
rtest.Assert(t, xattrRestored, "xattr %v value not restored", testAttr.xattr)
|
|
|
|
} else {
|
|
|
|
rtest.Assert(t, !xattrFound, "xattr %v should not have been restored", testAttr.xattr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testXattrToRestore struct {
|
|
|
|
xattr restic.ExtendedAttribute
|
|
|
|
shouldRestore bool
|
|
|
|
}
|
|
|
|
|
2024-06-13 20:21:00 +00:00
|
|
|
func TestOverwriteXattr(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
file := filepath.Join(dir, "file")
|
|
|
|
rtest.OK(t, os.WriteFile(file, []byte("hello world"), 0o600))
|
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
setAndVerifyXattr(t, file, []restic.ExtendedAttribute{
|
2024-06-13 20:21:00 +00:00
|
|
|
{
|
|
|
|
Name: "user.foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
2024-12-03 00:38:43 +00:00
|
|
|
{
|
|
|
|
Name: "abc.test",
|
|
|
|
Value: []byte("testxattr"),
|
|
|
|
},
|
2024-06-13 20:21:00 +00:00
|
|
|
})
|
|
|
|
|
2024-08-26 21:03:25 +00:00
|
|
|
setAndVerifyXattr(t, file, []restic.ExtendedAttribute{
|
2024-06-13 20:21:00 +00:00
|
|
|
{
|
|
|
|
Name: "user.other",
|
|
|
|
Value: []byte("some"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2024-12-03 00:38:43 +00:00
|
|
|
|
|
|
|
func TestOverwriteXattrWithSelectFilter(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
file := filepath.Join(dir, "file2")
|
|
|
|
rtest.OK(t, os.WriteFile(file, []byte("hello world"), 0o600))
|
|
|
|
|
|
|
|
noopWarnf := func(_ string, _ ...interface{}) {}
|
|
|
|
|
|
|
|
// Set a filter as if the user passed in --include-xattr user.*
|
|
|
|
xattrSelectFilter1 := func(xattrName string) bool {
|
|
|
|
shouldInclude, _ := filter.IncludeByPattern([]string{"user.*"}, noopWarnf)(xattrName)
|
|
|
|
return shouldInclude
|
|
|
|
}
|
|
|
|
|
|
|
|
setAndVerifyXattrWithSelectFilter(t, file, []testXattrToRestore{
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "user.foo",
|
|
|
|
Value: []byte("bar"),
|
|
|
|
},
|
|
|
|
shouldRestore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "user.test",
|
|
|
|
Value: []byte("testxattr"),
|
|
|
|
},
|
|
|
|
shouldRestore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "security.other",
|
|
|
|
Value: []byte("testing"),
|
|
|
|
},
|
|
|
|
shouldRestore: false,
|
|
|
|
},
|
|
|
|
}, xattrSelectFilter1)
|
|
|
|
|
|
|
|
// Set a filter as if the user passed in --include-xattr user.*
|
|
|
|
xattrSelectFilter2 := func(xattrName string) bool {
|
|
|
|
shouldInclude, _ := filter.IncludeByPattern([]string{"user.o*", "user.comm*"}, noopWarnf)(xattrName)
|
|
|
|
return shouldInclude
|
|
|
|
}
|
|
|
|
|
|
|
|
setAndVerifyXattrWithSelectFilter(t, file, []testXattrToRestore{
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "user.other",
|
|
|
|
Value: []byte("some"),
|
|
|
|
},
|
|
|
|
shouldRestore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "security.other",
|
|
|
|
Value: []byte("testing"),
|
|
|
|
},
|
|
|
|
shouldRestore: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "user.open",
|
|
|
|
Value: []byte("door"),
|
|
|
|
},
|
|
|
|
shouldRestore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "user.common",
|
|
|
|
Value: []byte("testing"),
|
|
|
|
},
|
|
|
|
shouldRestore: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
xattr: restic.ExtendedAttribute{
|
|
|
|
Name: "user.bad",
|
|
|
|
Value: []byte("dontincludeme"),
|
|
|
|
},
|
|
|
|
shouldRestore: false,
|
|
|
|
},
|
|
|
|
}, xattrSelectFilter2)
|
|
|
|
}
|