add proper constants for node type

This commit is contained in:
Michael Eischer
2024-07-09 19:51:44 +02:00
parent 3b438e5c7c
commit ca1e5e10b6
42 changed files with 206 additions and 206 deletions

View File

@@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/restic/restic/internal/restic"
"golang.org/x/sys/windows"
)
@@ -105,7 +106,7 @@ func ClearAttribute(path string, attribute uint32) error {
}
// OpenHandleForEA return a file handle for file or dir for setting/getting EAs
func OpenHandleForEA(nodeType, path string, writeAccess bool) (handle windows.Handle, err error) {
func OpenHandleForEA(nodeType restic.NodeType, path string, writeAccess bool) (handle windows.Handle, err error) {
path = fixpath(path)
fileAccess := windows.FILE_READ_EA
if writeAccess {
@@ -113,10 +114,10 @@ func OpenHandleForEA(nodeType, path string, writeAccess bool) (handle windows.Ha
}
switch nodeType {
case "file":
case restic.NodeTypeFile:
utf16Path := windows.StringToUTF16Ptr(path)
handle, err = windows.CreateFile(utf16Path, uint32(fileAccess), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0)
case "dir":
case restic.NodeTypeDir:
utf16Path := windows.StringToUTF16Ptr(path)
handle, err = windows.CreateFile(utf16Path, uint32(fileAccess), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS, 0)
default:

View File

@@ -25,7 +25,7 @@ func NodeFromFileInfo(path string, fi os.FileInfo, ignoreXattrListError bool) (*
}
node.Type = nodeTypeFromFileInfo(fi)
if node.Type == "file" {
if node.Type == restic.NodeTypeFile {
node.Size = uint64(fi.Size())
}
@@ -33,27 +33,27 @@ func NodeFromFileInfo(path string, fi os.FileInfo, ignoreXattrListError bool) (*
return node, err
}
func nodeTypeFromFileInfo(fi os.FileInfo) string {
func nodeTypeFromFileInfo(fi os.FileInfo) restic.NodeType {
switch fi.Mode() & os.ModeType {
case 0:
return "file"
return restic.NodeTypeFile
case os.ModeDir:
return "dir"
return restic.NodeTypeDir
case os.ModeSymlink:
return "symlink"
return restic.NodeTypeSymlink
case os.ModeDevice | os.ModeCharDevice:
return "chardev"
return restic.NodeTypeCharDev
case os.ModeDevice:
return "dev"
return restic.NodeTypeDev
case os.ModeNamedPipe:
return "fifo"
return restic.NodeTypeFifo
case os.ModeSocket:
return "socket"
return restic.NodeTypeSocket
case os.ModeIrregular:
return "irregular"
return restic.NodeTypeIrregular
}
return ""
return restic.NodeTypeInvalid
}
func nodeFillExtra(node *restic.Node, path string, fi os.FileInfo, ignoreXattrListError bool) error {
@@ -74,25 +74,25 @@ func nodeFillExtra(node *restic.Node, path string, fi os.FileInfo, ignoreXattrLi
nodeFillUser(node, stat)
switch node.Type {
case "file":
case restic.NodeTypeFile:
node.Size = uint64(stat.size())
node.Links = uint64(stat.nlink())
case "dir":
case "symlink":
case restic.NodeTypeDir:
case restic.NodeTypeSymlink:
var err error
node.LinkTarget, err = Readlink(path)
node.Links = uint64(stat.nlink())
if err != nil {
return errors.WithStack(err)
}
case "dev":
case restic.NodeTypeDev:
node.Device = uint64(stat.rdev())
node.Links = uint64(stat.nlink())
case "chardev":
case restic.NodeTypeCharDev:
node.Device = uint64(stat.rdev())
node.Links = uint64(stat.nlink())
case "fifo":
case "socket":
case restic.NodeTypeFifo:
case restic.NodeTypeSocket:
default:
return errors.Errorf("unsupported file type %q", node.Type)
}
@@ -178,31 +178,31 @@ func NodeCreateAt(node *restic.Node, path string) error {
debug.Log("create node %v at %v", node.Name, path)
switch node.Type {
case "dir":
case restic.NodeTypeDir:
if err := nodeCreateDirAt(node, path); err != nil {
return err
}
case "file":
case restic.NodeTypeFile:
if err := nodeCreateFileAt(path); err != nil {
return err
}
case "symlink":
case restic.NodeTypeSymlink:
if err := nodeCreateSymlinkAt(node, path); err != nil {
return err
}
case "dev":
case restic.NodeTypeDev:
if err := nodeCreateDevAt(node, path); err != nil {
return err
}
case "chardev":
case restic.NodeTypeCharDev:
if err := nodeCreateCharDevAt(node, path); err != nil {
return err
}
case "fifo":
case restic.NodeTypeFifo:
if err := nodeCreateFifoAt(path); err != nil {
return err
}
case "socket":
case restic.NodeTypeSocket:
return nil
default:
return errors.Errorf("filetype %q not implemented", node.Type)
@@ -305,7 +305,7 @@ func nodeRestoreMetadata(node *restic.Node, path string, warn func(msg string))
// Moving RestoreTimestamps and restoreExtendedAttributes calls above as for readonly files in windows
// calling Chmod below will no longer allow any modifications to be made on the file and the
// calls above would fail.
if node.Type != "symlink" {
if node.Type != restic.NodeTypeSymlink {
if err := Chmod(path, node.Mode); err != nil {
if firsterr == nil {
firsterr = errors.WithStack(err)
@@ -322,7 +322,7 @@ func NodeRestoreTimestamps(node *restic.Node, path string) error {
syscall.NsecToTimespec(node.ModTime.UnixNano()),
}
if node.Type == "symlink" {
if node.Type == restic.NodeTypeSymlink {
return nodeRestoreSymlinkTimestamps(path, utimes)
}

View File

@@ -79,7 +79,7 @@ func parseTime(s string) time.Time {
var nodeTests = []restic.Node{
{
Name: "testFile",
Type: "file",
Type: restic.NodeTypeFile,
Content: restic.IDs{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -90,7 +90,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testSuidFile",
Type: "file",
Type: restic.NodeTypeFile,
Content: restic.IDs{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -101,7 +101,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testSuidFile2",
Type: "file",
Type: restic.NodeTypeFile,
Content: restic.IDs{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -112,7 +112,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testSticky",
Type: "file",
Type: restic.NodeTypeFile,
Content: restic.IDs{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -123,7 +123,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testDir",
Type: "dir",
Type: restic.NodeTypeDir,
Subtree: nil,
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -134,7 +134,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testSymlink",
Type: "symlink",
Type: restic.NodeTypeSymlink,
LinkTarget: "invalid",
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -148,7 +148,7 @@ var nodeTests = []restic.Node{
// metadata, so we can test if CreateAt works with pre-existing files.
{
Name: "testFile",
Type: "file",
Type: restic.NodeTypeFile,
Content: restic.IDs{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -159,7 +159,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testDir",
Type: "dir",
Type: restic.NodeTypeDir,
Subtree: nil,
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -170,7 +170,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testXattrFile",
Type: "file",
Type: restic.NodeTypeFile,
Content: restic.IDs{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -184,7 +184,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testXattrDir",
Type: "dir",
Type: restic.NodeTypeDir,
Subtree: nil,
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -198,7 +198,7 @@ var nodeTests = []restic.Node{
},
{
Name: "testXattrFileMacOSResourceFork",
Type: "file",
Type: restic.NodeTypeFile,
Content: restic.IDs{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
@@ -268,7 +268,7 @@ func TestNodeRestoreAt(t *testing.T) {
"%v: UID doesn't match (%v != %v)", test.Type, test.UID, n2.UID)
rtest.Assert(t, test.GID == n2.GID,
"%v: GID doesn't match (%v != %v)", test.Type, test.GID, n2.GID)
if test.Type != "symlink" {
if test.Type != restic.NodeTypeSymlink {
// On OpenBSD only root can set sticky bit (see sticky(8)).
if runtime.GOOS != "openbsd" && runtime.GOOS != "netbsd" && runtime.GOOS != "solaris" && test.Name == "testSticky" {
rtest.Assert(t, test.Mode == n2.Mode,
@@ -288,11 +288,11 @@ func TestNodeRestoreAt(t *testing.T) {
}
}
func AssertFsTimeEqual(t *testing.T, label string, nodeType string, t1 time.Time, t2 time.Time) {
func AssertFsTimeEqual(t *testing.T, label string, nodeType restic.NodeType, t1 time.Time, t2 time.Time) {
var equal bool
// Go currently doesn't support setting timestamps of symbolic links on darwin and bsd
if nodeType == "symlink" {
if nodeType == restic.NodeTypeSymlink {
switch runtime.GOOS {
case "darwin", "freebsd", "openbsd", "netbsd", "solaris":
return

View File

@@ -42,7 +42,7 @@ func checkFile(t testing.TB, stat *syscall.Stat_t, node *restic.Node) {
t.Errorf("Dev does not match, want %v, got %v", stat.Dev, node.DeviceID)
}
if node.Size != uint64(stat.Size) && node.Type != "symlink" {
if node.Size != uint64(stat.Size) && node.Type != restic.NodeTypeSymlink {
t.Errorf("Size does not match, want %v, got %v", stat.Size, node.Size)
}
@@ -135,9 +135,9 @@ func TestNodeFromFileInfo(t *testing.T) {
}
switch node.Type {
case "file", "symlink":
case restic.NodeTypeFile, restic.NodeTypeSymlink:
checkFile(t, s, node)
case "dev", "chardev":
case restic.NodeTypeDev, restic.NodeTypeCharDev:
checkFile(t, s, node)
checkDevice(t, s, node)
default:

View File

@@ -139,7 +139,7 @@ func closeFileHandle(fileHandle windows.Handle, path string) {
// restoreExtendedAttributes handles restore of the Windows Extended Attributes to the specified path.
// The Windows API requires setting of all the Extended Attributes in one call.
func restoreExtendedAttributes(nodeType, path string, eas []ExtendedAttribute) (err error) {
func restoreExtendedAttributes(nodeType restic.NodeType, path string, eas []ExtendedAttribute) (err error) {
var fileHandle windows.Handle
if fileHandle, err = OpenHandleForEA(nodeType, path, true); fileHandle == 0 {
return nil
@@ -386,7 +386,7 @@ func nodeFillGenericAttributes(node *restic.Node, path string, fi os.FileInfo, s
}
var sd *[]byte
if node.Type == "file" || node.Type == "dir" {
if node.Type == restic.NodeTypeFile || node.Type == restic.NodeTypeDir {
// Check EA support and get security descriptor for file/dir only
allowExtended, err = checkAndStoreEASupport(path)
if err != nil {

View File

@@ -24,14 +24,14 @@ func TestRestoreSecurityDescriptors(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
for i, sd := range TestFileSDs {
testRestoreSecurityDescriptor(t, sd, tempDir, "file", fmt.Sprintf("testfile%d", i))
testRestoreSecurityDescriptor(t, sd, tempDir, restic.NodeTypeFile, fmt.Sprintf("testfile%d", i))
}
for i, sd := range TestDirSDs {
testRestoreSecurityDescriptor(t, sd, tempDir, "dir", fmt.Sprintf("testdir%d", i))
testRestoreSecurityDescriptor(t, sd, tempDir, restic.NodeTypeDir, fmt.Sprintf("testdir%d", i))
}
}
func testRestoreSecurityDescriptor(t *testing.T, sd string, tempDir, fileType, fileName string) {
func testRestoreSecurityDescriptor(t *testing.T, sd string, tempDir string, fileType restic.NodeType, fileName string) {
// Decode the encoded string SD to get the security descriptor input in bytes.
sdInputBytes, err := base64.StdEncoding.DecodeString(sd)
test.OK(t, errors.Wrapf(err, "Error decoding SD for: %s", fileName))
@@ -56,7 +56,7 @@ func testRestoreSecurityDescriptor(t *testing.T, sd string, tempDir, fileType, f
CompareSecurityDescriptors(t, testPath, *sdByteFromRestoredNode, *sdBytesFromRestoredPath)
}
func getNode(name string, fileType string, genericAttributes map[restic.GenericAttributeType]json.RawMessage) restic.Node {
func getNode(name string, fileType restic.NodeType, genericAttributes map[restic.GenericAttributeType]json.RawMessage) restic.Node {
return restic.Node{
Name: name,
Type: fileType,
@@ -113,7 +113,7 @@ func TestRestoreFileAttributes(t *testing.T) {
expectedNodes := []restic.Node{
{
Name: fmt.Sprintf("testfile%d", i),
Type: "file",
Type: restic.NodeTypeFile,
Mode: 0655,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -146,7 +146,7 @@ func TestRestoreFileAttributes(t *testing.T) {
expectedNodes := []restic.Node{
{
Name: fmt.Sprintf("testdirectory%d", i),
Type: "dir",
Type: restic.NodeTypeDir,
Mode: 0755,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -164,7 +164,7 @@ func runGenericAttributesTest(t *testing.T, tempDir string, genericAttributeName
expectedNodes := []restic.Node{
{
Name: "testfile",
Type: "file",
Type: restic.NodeTypeFile,
Mode: 0644,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -173,7 +173,7 @@ func runGenericAttributesTest(t *testing.T, tempDir string, genericAttributeName
},
{
Name: "testdirectory",
Type: "dir",
Type: restic.NodeTypeDir,
Mode: 0755,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -200,12 +200,12 @@ func restoreAndGetNode(t *testing.T, tempDir string, testNode *restic.Node, warn
err := os.MkdirAll(filepath.Dir(testPath), testNode.Mode)
test.OK(t, errors.Wrapf(err, "Failed to create parent directories for: %s", testPath))
if testNode.Type == "file" {
if testNode.Type == restic.NodeTypeFile {
testFile, err := os.Create(testPath)
test.OK(t, errors.Wrapf(err, "Failed to create test file: %s", testPath))
testFile.Close()
} else if testNode.Type == "dir" {
} else if testNode.Type == restic.NodeTypeDir {
err := os.Mkdir(testPath, testNode.Mode)
test.OK(t, errors.Wrapf(err, "Failed to create test directory: %s", testPath))
@@ -242,7 +242,7 @@ func TestNewGenericAttributeType(t *testing.T) {
expectedNodes := []restic.Node{
{
Name: "testfile",
Type: "file",
Type: restic.NodeTypeFile,
Mode: 0644,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -251,7 +251,7 @@ func TestNewGenericAttributeType(t *testing.T) {
},
{
Name: "testdirectory",
Type: "dir",
Type: restic.NodeTypeDir,
Mode: 0755,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -274,7 +274,7 @@ func TestRestoreExtendedAttributes(t *testing.T) {
expectedNodes := []restic.Node{
{
Name: "testfile",
Type: "file",
Type: restic.NodeTypeFile,
Mode: 0644,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -285,7 +285,7 @@ func TestRestoreExtendedAttributes(t *testing.T) {
},
{
Name: "testdirectory",
Type: "dir",
Type: restic.NodeTypeDir,
Mode: 0755,
ModTime: parseTime("2005-05-14 21:07:03.111"),
AccessTime: parseTime("2005-05-14 21:07:04.222"),
@@ -301,9 +301,9 @@ func TestRestoreExtendedAttributes(t *testing.T) {
var handle windows.Handle
var err error
utf16Path := windows.StringToUTF16Ptr(testPath)
if node.Type == "file" {
if node.Type == restic.NodeTypeFile {
handle, err = windows.CreateFile(utf16Path, windows.FILE_READ_EA, 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0)
} else if node.Type == "dir" {
} else if node.Type == restic.NodeTypeDir {
handle, err = windows.CreateFile(utf16Path, windows.FILE_READ_EA, 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS, 0)
}
test.OK(t, errors.Wrapf(err, "Error opening file/directory for: %s", testPath))

View File

@@ -23,13 +23,13 @@ func setAndVerifyXattr(t *testing.T, file string, attrs []restic.ExtendedAttribu
}
node := &restic.Node{
Type: "file",
Type: restic.NodeTypeFile,
ExtendedAttributes: attrs,
}
rtest.OK(t, nodeRestoreExtendedAttributes(node, file))
nodeActual := &restic.Node{
Type: "file",
Type: restic.NodeTypeFile,
}
rtest.OK(t, nodeFillExtendedAttributes(nodeActual, file, false))