From 760ac1dd33f460a1e73c912913eb0c3da7afb286 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sat, 17 Nov 2018 14:09:24 +0100 Subject: [PATCH] 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 Reviewed-by: Brad Fitzpatrick Reviewed-by: roger peppe --- src/os/removeall_at.go | 8 +++++++- src/os/removeall_noat.go | 6 ++++++ src/os/removeall_test.go | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/os/removeall_at.go b/src/os/removeall_at.go index eb220bd103..5aa1b46117 100644 --- a/src/os/removeall_at.go +++ b/src/os/removeall_at.go @@ -13,8 +13,14 @@ import ( ) func RemoveAll(path string) error { + if path == "" { + // fail silently to retain compatibility with previous behavior + // of RemoveAll. See issue 28830. + return nil + } + // Not allowed in unix - if path == "" || endsWithDot(path) { + if endsWithDot(path) { return syscall.EINVAL } diff --git a/src/os/removeall_noat.go b/src/os/removeall_noat.go index d1dd43ff6a..d382b42af3 100644 --- a/src/os/removeall_noat.go +++ b/src/os/removeall_noat.go @@ -16,6 +16,12 @@ import ( // it encounters. If the path does not exist, RemoveAll // returns nil (no error). func RemoveAll(path string) error { + if path == "" { + // fail silently to retain compatibility with previous behavior + // of RemoveAll. See issue 28830. + return nil + } + // Simple case: if Remove works, we're done. err := Remove(path) if err == nil || IsNotExist(err) { diff --git a/src/os/removeall_test.go b/src/os/removeall_test.go index 5eec8cd154..fd3b8d22f9 100644 --- a/src/os/removeall_test.go +++ b/src/os/removeall_test.go @@ -21,6 +21,10 @@ func TestRemoveAll(t *testing.T) { } defer RemoveAll(tmpDir) + if err := RemoveAll(""); err != nil { + t.Errorf("RemoveAll(\"\"): %v; want nil", err) + } + file := filepath.Join(tmpDir, "file") path := filepath.Join(tmpDir, "_TestRemoveAll_") fpath := filepath.Join(path, "file") -- 2.50.0