1
0
mirror of https://github.com/golang/go synced 2024-11-24 21:10:04 -07:00

os: remove read-only directories in Remove on Windows

Allows Remove to remove directory with read-only attribute in Windows

Fixes #26295

(Read-only attribute in Windows doesn't prevent users to delete a directory, it is just used to know if the folder has custom icon or other custom properties)
This commit is contained in:
Alexandre Alves 2022-11-06 18:20:20 +01:00
parent 1c05968c9a
commit 8f045d8146

View File

@ -245,9 +245,20 @@ func Remove(name string) error {
if e == nil {
return nil
}
e1 := syscall.RemoveDirectory(p)
if e1 == nil {
return nil
} else {
//Empty directories with "read-only" attribute on Windows can cause issues so we have to try with chmod to remove it
a, e2 := syscall.GetFileAttributes(p)
if e2 == nil && a&syscall.FILE_ATTRIBUTE_DIRECTORY != 0 && IsPermission(e1) {
if fs, err := Stat(fixLongPath(name)); err == nil {
if err = Chmod(fixLongPath(name), FileMode(0200|int(fs.Mode()))); err == nil {
e1 = syscall.RemoveDirectory(p)
}
}
}
}
// Both failed: figure out which error to return.