From 8f045d8146cdbc9ee6a47f2e54f472023311e554 Mon Sep 17 00:00:00 2001 From: Alexandre Alves Date: Sun, 6 Nov 2022 18:20:20 +0100 Subject: [PATCH] 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) --- src/os/file_windows.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/os/file_windows.go b/src/os/file_windows.go index db5c27dd30f41..4e8b351f217d2 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -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.