1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2025-03-09 13:53:03 +00:00

fs: add file handle helpers for reading

This commit is contained in:
Michael Eischer 2024-11-17 13:50:55 +01:00
parent f1ae5a21e2
commit 1b87468531
3 changed files with 28 additions and 3 deletions

View file

@ -24,6 +24,15 @@ func openMetadataHandle(path string, flag int) (*os.File, error) {
return f, nil
}
func openReadHandle(path string, flag int) (*os.File, error) {
f, err := os.OpenFile(path, flag, 0)
if err != nil {
return nil, err
}
_ = setFlags(f)
return f, nil
}
// reopenMetadataHandle reopens a handle created by openMetadataHandle for reading.
// The caller must no longer use the original file.
func reopenMetadataHandle(f *os.File) (*os.File, error) {

View file

@ -25,6 +25,15 @@ func openMetadataHandle(path string, flag int) (*os.File, error) {
return f, nil
}
func openReadHandle(path string, flag int) (*os.File, error) {
f, err := os.OpenFile(path, flag, 0)
if err != nil {
return nil, err
}
_ = setFlags(f)
return f, nil
}
// reopenMetadataHandle reopens a handle created by openMetadataHandle for reading.
// The caller must no longer use the original file.
func reopenMetadataHandle(f *os.File) (*os.File, error) {

View file

@ -77,11 +77,18 @@ func (p *fdMetadataHandle) SecurityDescriptor() (*[]byte, error) {
}
func openMetadataHandle(path string, flag int) (*os.File, error) {
path = fixpath(path)
// OpenFile from go does not request FILE_READ_EA so we need our own low-level implementation
// according to the windows docs, STANDARD_RIGHTS_READ + FILE_FLAG_BACKUP_SEMANTICS disable security checks on access
return openCustomHandle(path, flag, windows.FILE_READ_EA|windows.FILE_READ_ATTRIBUTES|windows.STANDARD_RIGHTS_READ)
}
func openReadHandle(path string, flag int) (*os.File, error) {
return openCustomHandle(path, flag, windows.FILE_GENERIC_READ)
}
func openCustomHandle(path string, flag int, fileAccess int) (*os.File, error) {
path = fixpath(path)
// according to the windows docs, STANDARD_RIGHTS_READ (fileAccess) + FILE_FLAG_BACKUP_SEMANTICS disables security checks on access
// if the process holds the SeBackupPrivilege
fileAccess := windows.FILE_READ_EA | windows.FILE_READ_ATTRIBUTES | windows.STANDARD_RIGHTS_READ
shareMode := windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE | windows.FILE_SHARE_DELETE
attrs := windows.FILE_ATTRIBUTE_NORMAL | windows.FILE_FLAG_BACKUP_SEMANTICS
if flag&O_NOFOLLOW != 0 {