From 20a6c526e518b1d73a3b2b1797bccbff218c6fbc Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 14 Jun 2024 21:15:46 +0200 Subject: [PATCH 1/2] restic: also fix file path when reading windows xattrs --- internal/fs/file_windows.go | 18 ++++++++++++++++++ internal/restic/node_windows.go | 21 ++------------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/internal/fs/file_windows.go b/internal/fs/file_windows.go index 2f0969804..cadd69284 100644 --- a/internal/fs/file_windows.go +++ b/internal/fs/file_windows.go @@ -103,3 +103,21 @@ func ClearAttribute(path string, attribute uint32) error { } return nil } + +// OpenHandleForEA return a file handle for file or dir for setting/getting EAs +func OpenHandleForEA(nodeType, path string) (handle windows.Handle, err error) { + path = fixpath(path) + switch nodeType { + case "file": + utf16Path := windows.StringToUTF16Ptr(path) + fileAccessRightReadWriteEA := (0x8 | 0x10) + handle, err = windows.CreateFile(utf16Path, uint32(fileAccessRightReadWriteEA), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0) + case "dir": + utf16Path := windows.StringToUTF16Ptr(path) + fileAccessRightReadWriteEA := (0x8 | 0x10) + handle, err = windows.CreateFile(utf16Path, uint32(fileAccessRightReadWriteEA), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS, 0) + default: + return 0, nil + } + return handle, err +} diff --git a/internal/restic/node_windows.go b/internal/restic/node_windows.go index 0d96bdb98..0825992b1 100644 --- a/internal/restic/node_windows.go +++ b/internal/restic/node_windows.go @@ -88,7 +88,7 @@ func (node Node) restoreExtendedAttributes(path string) (err error) { // fill extended attributes in the node. This also includes the Generic attributes for windows. func (node *Node) fillExtendedAttributes(path string, _ bool) (err error) { var fileHandle windows.Handle - if fileHandle, err = getFileHandleForEA(node.Type, path); fileHandle == 0 { + if fileHandle, err = fs.OpenHandleForEA(node.Type, path); fileHandle == 0 { return nil } if err != nil { @@ -118,23 +118,6 @@ func (node *Node) fillExtendedAttributes(path string, _ bool) (err error) { return nil } -// Get file handle for file or dir for setting/getting EAs -func getFileHandleForEA(nodeType, path string) (handle windows.Handle, err error) { - switch nodeType { - case "file": - utf16Path := windows.StringToUTF16Ptr(path) - fileAccessRightReadWriteEA := (0x8 | 0x10) - handle, err = windows.CreateFile(utf16Path, uint32(fileAccessRightReadWriteEA), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0) - case "dir": - utf16Path := windows.StringToUTF16Ptr(path) - fileAccessRightReadWriteEA := (0x8 | 0x10) - handle, err = windows.CreateFile(utf16Path, uint32(fileAccessRightReadWriteEA), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS, 0) - default: - return 0, nil - } - return handle, err -} - // closeFileHandle safely closes a file handle and logs any errors. func closeFileHandle(fileHandle windows.Handle, path string) { err := windows.CloseHandle(fileHandle) @@ -147,7 +130,7 @@ func closeFileHandle(fileHandle windows.Handle, path string) { // The Windows API requires setting of all the Extended Attributes in one call. func restoreExtendedAttributes(nodeType, path string, eas []fs.ExtendedAttribute) (err error) { var fileHandle windows.Handle - if fileHandle, err = getFileHandleForEA(nodeType, path); fileHandle == 0 { + if fileHandle, err = fs.OpenHandleForEA(nodeType, path); fileHandle == 0 { return nil } if err != nil { From c01b65522917cff4ebd3d51528732d8f3815a853 Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 14 Jun 2024 22:36:07 +0200 Subject: [PATCH 2/2] backup: fix reading xattrs on Windows for root disks as regular user Unprivileged users cannot open the root disks with write permissions for xattrs. Thus, only request read permissions for reading the xattrs. --- internal/fs/file_windows.go | 13 ++++++++----- internal/restic/node_windows.go | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/fs/file_windows.go b/internal/fs/file_windows.go index cadd69284..b05068c42 100644 --- a/internal/fs/file_windows.go +++ b/internal/fs/file_windows.go @@ -105,17 +105,20 @@ 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) (handle windows.Handle, err error) { +func OpenHandleForEA(nodeType, path string, writeAccess bool) (handle windows.Handle, err error) { path = fixpath(path) + fileAccess := windows.FILE_READ_EA + if writeAccess { + fileAccess = fileAccess | windows.FILE_WRITE_EA + } + switch nodeType { case "file": utf16Path := windows.StringToUTF16Ptr(path) - fileAccessRightReadWriteEA := (0x8 | 0x10) - handle, err = windows.CreateFile(utf16Path, uint32(fileAccessRightReadWriteEA), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0) + handle, err = windows.CreateFile(utf16Path, uint32(fileAccess), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0) case "dir": utf16Path := windows.StringToUTF16Ptr(path) - fileAccessRightReadWriteEA := (0x8 | 0x10) - handle, err = windows.CreateFile(utf16Path, uint32(fileAccessRightReadWriteEA), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS, 0) + handle, err = windows.CreateFile(utf16Path, uint32(fileAccess), 0, nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_BACKUP_SEMANTICS, 0) default: return 0, nil } diff --git a/internal/restic/node_windows.go b/internal/restic/node_windows.go index 0825992b1..8942db901 100644 --- a/internal/restic/node_windows.go +++ b/internal/restic/node_windows.go @@ -88,7 +88,7 @@ func (node Node) restoreExtendedAttributes(path string) (err error) { // fill extended attributes in the node. This also includes the Generic attributes for windows. func (node *Node) fillExtendedAttributes(path string, _ bool) (err error) { var fileHandle windows.Handle - if fileHandle, err = fs.OpenHandleForEA(node.Type, path); fileHandle == 0 { + if fileHandle, err = fs.OpenHandleForEA(node.Type, path, false); fileHandle == 0 { return nil } if err != nil { @@ -130,7 +130,7 @@ func closeFileHandle(fileHandle windows.Handle, path string) { // The Windows API requires setting of all the Extended Attributes in one call. func restoreExtendedAttributes(nodeType, path string, eas []fs.ExtendedAttribute) (err error) { var fileHandle windows.Handle - if fileHandle, err = fs.OpenHandleForEA(nodeType, path); fileHandle == 0 { + if fileHandle, err = fs.OpenHandleForEA(nodeType, path, true); fileHandle == 0 { return nil } if err != nil {