From c74659290a473cf932ec6bc96bfa7e96a930676e Mon Sep 17 00:00:00 2001 From: Baokun Lee Date: Tue, 5 Mar 2019 03:10:38 +0800 Subject: [PATCH] os: remove unreadable directories in RemoveAll Fixes #30555 Change-Id: Ib894b4f3cdba23a18a69c9470cf69ceb83591a4d Reviewed-on: https://go-review.googlesource.com/c/go/+/165057 Run-TryBot: Baokun Lee TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/os/removeall_at.go | 3 ++- src/os/removeall_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/os/removeall_at.go b/src/os/removeall_at.go index 0b7d5efb7a..94232cf556 100644 --- a/src/os/removeall_at.go +++ b/src/os/removeall_at.go @@ -92,7 +92,8 @@ func removeAllFrom(parent *File, path string) error { if IsNotExist(err) { return nil } - return err + recurseErr = err + break } names, readErr := file.Readdirnames(request) diff --git a/src/os/removeall_test.go b/src/os/removeall_test.go index 9dab0d4bb1..21371d8776 100644 --- a/src/os/removeall_test.go +++ b/src/os/removeall_test.go @@ -372,3 +372,33 @@ func TestRemoveAllButReadOnly(t *testing.T) { } } } + +func TestRemoveUnreadableDir(t *testing.T) { + switch runtime.GOOS { + case "nacl", "js", "windows": + t.Skipf("skipping test on %s", runtime.GOOS) + } + + if Getuid() == 0 { + t.Skip("skipping test when running as root") + } + + t.Parallel() + + tempDir, err := ioutil.TempDir("", "TestRemoveAllButReadOnly-") + if err != nil { + t.Fatal(err) + } + defer RemoveAll(tempDir) + + target := filepath.Join(tempDir, "d0", "d1", "d2") + if err := MkdirAll(target, 0755); err != nil { + t.Fatal(err) + } + if err := Chmod(target, 0300); err != nil { + t.Fatal(err) + } + if err := RemoveAll(filepath.Join(tempDir, "d0")); err != nil { + t.Fatal(err) + } +}