Skip to content

Commit fe562ce

Browse files
committed
os: make RemoveAll("") fail silently on unix
CL 146020 changed the behavior of RemoveAll("") on unix systems using the *at functions to return syscall.EINVAL instead of nil. Adjust the *at implementation to retain this behavior as is the case on the *noat systems. Additionally, also make sure RemoveAll("") on systems not using the "at functions (e.g. nacl and js/wasm) follow the same behavior (which wasn't the case previously). Fixes #28830 Change-Id: I8383c1423fefe871d18ff49134a1d23077ec6867 Reviewed-on: https://go-review.googlesource.com/c/150158 Run-TryBot: Tobias Klauser <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: roger peppe <[email protected]>
1 parent 054640b commit fe562ce

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/os/removeall_at.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ import (
1313
)
1414

1515
func RemoveAll(path string) error {
16+
if path == "" {
17+
// fail silently to retain compatibility with previous behavior
18+
// of RemoveAll. See issue 28830.
19+
return nil
20+
}
21+
1622
// Not allowed in unix
17-
if path == "" || endsWithDot(path) {
23+
if endsWithDot(path) {
1824
return syscall.EINVAL
1925
}
2026

src/os/removeall_noat.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import (
1616
// it encounters. If the path does not exist, RemoveAll
1717
// returns nil (no error).
1818
func RemoveAll(path string) error {
19+
if path == "" {
20+
// fail silently to retain compatibility with previous behavior
21+
// of RemoveAll. See issue 28830.
22+
return nil
23+
}
24+
1925
// Simple case: if Remove works, we're done.
2026
err := Remove(path)
2127
if err == nil || IsNotExist(err) {

src/os/removeall_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func TestRemoveAll(t *testing.T) {
2121
}
2222
defer RemoveAll(tmpDir)
2323

24+
if err := RemoveAll(""); err != nil {
25+
t.Errorf("RemoveAll(\"\"): %v; want nil", err)
26+
}
27+
2428
file := filepath.Join(tmpDir, "file")
2529
path := filepath.Join(tmpDir, "_TestRemoveAll_")
2630
fpath := filepath.Join(path, "file")

0 commit comments

Comments
 (0)