Skip to content

Commit ba1de79

Browse files
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent da6294a commit ba1de79

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/os/removeall_at.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func RemoveAll(path string) error {
2525
return &PathError{"RemoveAll", path, syscall.EINVAL}
2626
}
2727

28+
// Simple case: if Remove works, we're done.
29+
err := Remove(path)
30+
if err == nil || IsNotExist(err) {
31+
return nil
32+
}
33+
2834
// RemoveAll recurses by deleting the path base from
2935
// its parent directory
3036
parentDir, base := splitPath(path)

src/os/removeall_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,31 @@ func TestRemoveAllDotDot(t *testing.T) {
264264
}
265265
}
266266
}
267+
268+
// Issue #29178.
269+
func TestRemoveReadOnlyDir(t *testing.T) {
270+
t.Parallel()
271+
272+
tempDir, err := ioutil.TempDir("", "TestRemoveReadOnlyDir-")
273+
if err != nil {
274+
t.Fatal(err)
275+
}
276+
defer RemoveAll(tempDir)
277+
278+
subdir := filepath.Join(tempDir, "x")
279+
if err := Mkdir(subdir, 0); err != nil {
280+
t.Fatal(err)
281+
}
282+
283+
// If an error occurs make it more likely that removing the
284+
// temporary directory will succeed.
285+
defer Chmod(subdir, 0777)
286+
287+
if err := RemoveAll(subdir); err != nil {
288+
t.Fatal(err)
289+
}
290+
291+
if _, err := Stat(subdir); err == nil {
292+
t.Error("subdirectory was not removed")
293+
}
294+
}

0 commit comments

Comments
 (0)