From: Ian Lance Taylor Date: Wed, 12 Dec 2018 00:55:16 +0000 (-0800) Subject: os: in RemoveAll, try Remove first X-Git-Tag: go1.12beta1~99 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=ba1de79a3a542b5bf25c4cc3be1c91d1ede47c55;p=gostls13.git os: in RemoveAll, try Remove first Otherwise we can fail to remove a unreadable empty directory. Fixes #29178 Change-Id: I43d5c89fce57a86626abe2a1c2bbf145716e087b Reviewed-on: https://go-review.googlesource.com/c/153720 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/os/removeall_at.go b/src/os/removeall_at.go index c42319a831..f0fed6dc33 100644 --- a/src/os/removeall_at.go +++ b/src/os/removeall_at.go @@ -25,6 +25,12 @@ func RemoveAll(path string) error { return &PathError{"RemoveAll", path, syscall.EINVAL} } + // Simple case: if Remove works, we're done. + err := Remove(path) + if err == nil || IsNotExist(err) { + return nil + } + // RemoveAll recurses by deleting the path base from // its parent directory parentDir, base := splitPath(path) diff --git a/src/os/removeall_test.go b/src/os/removeall_test.go index 1c9f163225..0f7dce078a 100644 --- a/src/os/removeall_test.go +++ b/src/os/removeall_test.go @@ -264,3 +264,31 @@ func TestRemoveAllDotDot(t *testing.T) { } } } + +// Issue #29178. +func TestRemoveReadOnlyDir(t *testing.T) { + t.Parallel() + + tempDir, err := ioutil.TempDir("", "TestRemoveReadOnlyDir-") + if err != nil { + t.Fatal(err) + } + defer RemoveAll(tempDir) + + subdir := filepath.Join(tempDir, "x") + if err := Mkdir(subdir, 0); err != nil { + t.Fatal(err) + } + + // If an error occurs make it more likely that removing the + // temporary directory will succeed. + defer Chmod(subdir, 0777) + + if err := RemoveAll(subdir); err != nil { + t.Fatal(err) + } + + if _, err := Stat(subdir); err == nil { + t.Error("subdirectory was not removed") + } +}