Handle readonly empty files in windows

This commit is contained in:
Aneesh Nireshwalia 2024-02-22 17:54:43 -07:00
parent eeb1aa5388
commit d4be734c73
No known key found for this signature in database
GPG key ID: 6F5A52831C046F44

View file

@ -205,11 +205,19 @@ func (res *Restorer) restoreHardlinkAt(node *restic.Node, target, path, location
func (res *Restorer) restoreEmptyFileAt(node *restic.Node, target, location string) error { func (res *Restorer) restoreEmptyFileAt(node *restic.Node, target, location string) error {
wr, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600) wr, err := os.OpenFile(target, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil { if fs.IsAccessDenied(err) {
return err // If file is readonly, clear the readonly flag by resetting the
// permissions of the file and try again
// as the metadata will be set again in the second pass and the
// readonly flag will be applied again if needed.
if err = fs.ResetPermissions(target); err != nil {
return err
}
if wr, err = os.OpenFile(target, os.O_TRUNC|os.O_WRONLY, 0600); err != nil {
return err
}
} }
err = wr.Close() if err = wr.Close(); err != nil {
if err != nil {
return err return err
} }