1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 func TestRemoveAll(t *testing.T) {
23 if err := RemoveAll(""); err != nil {
24 t.Errorf("RemoveAll(\"\"): %v; want nil", err)
27 file := filepath.Join(tmpDir, "file")
28 path := filepath.Join(tmpDir, "_TestRemoveAll_")
29 fpath := filepath.Join(path, "file")
30 dpath := filepath.Join(path, "dir")
32 // Make a regular file and remove
33 fd, err := Create(file)
35 t.Fatalf("create %q: %s", file, err)
38 if err = RemoveAll(file); err != nil {
39 t.Fatalf("RemoveAll %q (first): %s", file, err)
41 if _, err = Lstat(file); err == nil {
42 t.Fatalf("Lstat %q succeeded after RemoveAll (first)", file)
45 // Make directory with 1 file and remove.
46 if err := MkdirAll(path, 0777); err != nil {
47 t.Fatalf("MkdirAll %q: %s", path, err)
49 fd, err = Create(fpath)
51 t.Fatalf("create %q: %s", fpath, err)
54 if err = RemoveAll(path); err != nil {
55 t.Fatalf("RemoveAll %q (second): %s", path, err)
57 if _, err = Lstat(path); err == nil {
58 t.Fatalf("Lstat %q succeeded after RemoveAll (second)", path)
61 // Make directory with file and subdirectory and remove.
62 if err = MkdirAll(dpath, 0777); err != nil {
63 t.Fatalf("MkdirAll %q: %s", dpath, err)
65 fd, err = Create(fpath)
67 t.Fatalf("create %q: %s", fpath, err)
70 fd, err = Create(dpath + "/file")
72 t.Fatalf("create %q: %s", fpath, err)
75 if err = RemoveAll(path); err != nil {
76 t.Fatalf("RemoveAll %q (third): %s", path, err)
78 if _, err := Lstat(path); err == nil {
79 t.Fatalf("Lstat %q succeeded after RemoveAll (third)", path)
82 // Chmod is not supported under Windows or wasip1 and test fails as root.
83 if runtime.GOOS != "windows" && runtime.GOOS != "wasip1" && Getuid() != 0 {
84 // Make directory with file and subdirectory and trigger error.
85 if err = MkdirAll(dpath, 0777); err != nil {
86 t.Fatalf("MkdirAll %q: %s", dpath, err)
89 for _, s := range []string{fpath, dpath + "/file1", path + "/zzz"} {
92 t.Fatalf("create %q: %s", s, err)
96 if err = Chmod(dpath, 0); err != nil {
97 t.Fatalf("Chmod %q 0: %s", dpath, err)
100 // No error checking here: either RemoveAll
101 // will or won't be able to remove dpath;
102 // either way we want to see if it removes fpath
103 // and path/zzz. Reasons why RemoveAll might
104 // succeed in removing dpath as well include:
106 // * running on a file system without permissions (FAT)
110 for _, s := range []string{fpath, path + "/zzz"} {
111 if _, err = Lstat(s); err == nil {
112 t.Fatalf("Lstat %q succeeded after partial RemoveAll", s)
116 if err = RemoveAll(path); err != nil {
117 t.Fatalf("RemoveAll %q after partial RemoveAll: %s", path, err)
119 if _, err = Lstat(path); err == nil {
120 t.Fatalf("Lstat %q succeeded after RemoveAll (final)", path)
124 // Test RemoveAll on a large directory.
125 func TestRemoveAllLarge(t *testing.T) {
127 t.Skip("skipping in short mode")
131 tmpDir := t.TempDir()
132 path := filepath.Join(tmpDir, "_TestRemoveAllLarge_")
134 // Make directory with 1000 files and remove.
135 if err := MkdirAll(path, 0777); err != nil {
136 t.Fatalf("MkdirAll %q: %s", path, err)
138 for i := 0; i < 1000; i++ {
139 fpath := fmt.Sprintf("%s/file%d", path, i)
140 fd, err := Create(fpath)
142 t.Fatalf("create %q: %s", fpath, err)
146 if err := RemoveAll(path); err != nil {
147 t.Fatalf("RemoveAll %q: %s", path, err)
149 if _, err := Lstat(path); err == nil {
150 t.Fatalf("Lstat %q succeeded after RemoveAll", path)
154 func TestRemoveAllLongPath(t *testing.T) {
155 switch runtime.GOOS {
156 case "aix", "darwin", "ios", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "illumos", "solaris":
159 t.Skip("skipping for not implemented platforms")
162 prevDir, err := Getwd()
164 t.Fatalf("Could not get wd: %s", err)
167 startPath, err := MkdirTemp("", "TestRemoveAllLongPath-")
169 t.Fatalf("Could not create TempDir: %s", err)
171 defer RemoveAll(startPath)
173 err = Chdir(startPath)
175 t.Fatalf("Could not chdir %s: %s", startPath, err)
178 // Removing paths with over 4096 chars commonly fails
179 for i := 0; i < 41; i++ {
180 name := strings.Repeat("a", 100)
182 err = Mkdir(name, 0755)
184 t.Fatalf("Could not mkdir %s: %s", name, err)
189 t.Fatalf("Could not chdir %s: %s", name, err)
195 t.Fatalf("Could not chdir %s: %s", prevDir, err)
198 err = RemoveAll(startPath)
200 t.Errorf("RemoveAll could not remove long file path %s: %s", startPath, err)
204 func TestRemoveAllDot(t *testing.T) {
205 prevDir, err := Getwd()
207 t.Fatalf("Could not get wd: %s", err)
209 tempDir, err := MkdirTemp("", "TestRemoveAllDot-")
211 t.Fatalf("Could not create TempDir: %s", err)
213 defer RemoveAll(tempDir)
217 t.Fatalf("Could not chdir to tempdir: %s", err)
222 t.Errorf("RemoveAll succeed to remove .")
227 t.Fatalf("Could not chdir %s: %s", prevDir, err)
231 func TestRemoveAllDotDot(t *testing.T) {
234 tempDir := t.TempDir()
235 subdir := filepath.Join(tempDir, "x")
236 subsubdir := filepath.Join(subdir, "y")
237 if err := MkdirAll(subsubdir, 0777); err != nil {
240 if err := RemoveAll(filepath.Join(subsubdir, "..")); err != nil {
243 for _, dir := range []string{subsubdir, subdir} {
244 if _, err := Stat(dir); err == nil {
245 t.Errorf("%s: exists after RemoveAll", dir)
251 func TestRemoveReadOnlyDir(t *testing.T) {
254 tempDir := t.TempDir()
255 subdir := filepath.Join(tempDir, "x")
256 if err := Mkdir(subdir, 0); err != nil {
260 // If an error occurs make it more likely that removing the
261 // temporary directory will succeed.
262 defer Chmod(subdir, 0777)
264 if err := RemoveAll(subdir); err != nil {
268 if _, err := Stat(subdir); err == nil {
269 t.Error("subdirectory was not removed")
274 func TestRemoveAllButReadOnlyAndPathError(t *testing.T) {
275 switch runtime.GOOS {
276 case "js", "wasip1", "windows":
277 t.Skipf("skipping test on %s", runtime.GOOS)
281 t.Skip("skipping test when running as root")
286 tempDir := t.TempDir()
298 readonly := []string{
301 inReadonly := func(d string) bool {
302 for _, ro := range readonly {
306 dd, _ := filepath.Split(d)
307 if filepath.Clean(dd) == ro {
314 for _, dir := range dirs {
315 if err := Mkdir(filepath.Join(tempDir, dir), 0777); err != nil {
319 for _, dir := range readonly {
320 d := filepath.Join(tempDir, dir)
321 if err := Chmod(d, 0555); err != nil {
325 // Defer changing the mode back so that the deferred
326 // RemoveAll(tempDir) can succeed.
330 err := RemoveAll(tempDir)
332 t.Fatal("RemoveAll succeeded unexpectedly")
335 // The error should be of type *PathError.
336 // see issue 30491 for details.
337 if pathErr, ok := err.(*PathError); ok {
338 want := filepath.Join(tempDir, "b", "y")
339 if pathErr.Path != want {
340 t.Errorf("RemoveAll(%q): err.Path=%q, want %q", tempDir, pathErr.Path, want)
343 t.Errorf("RemoveAll(%q): error has type %T, want *fs.PathError", tempDir, err)
346 for _, dir := range dirs {
347 _, err := Stat(filepath.Join(tempDir, dir))
350 t.Errorf("file %q was deleted but should still exist", dir)
354 t.Errorf("file %q still exists but should have been deleted", dir)
360 func TestRemoveUnreadableDir(t *testing.T) {
361 switch runtime.GOOS {
363 t.Skipf("skipping test on %s", runtime.GOOS)
367 t.Skip("skipping test when running as root")
372 tempDir := t.TempDir()
373 target := filepath.Join(tempDir, "d0", "d1", "d2")
374 if err := MkdirAll(target, 0755); err != nil {
377 if err := Chmod(target, 0300); err != nil {
380 if err := RemoveAll(filepath.Join(tempDir, "d0")); err != nil {
386 func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) {
388 t.Skip("skipping in short mode")
392 tmpDir := t.TempDir()
393 path := filepath.Join(tmpDir, "_TestRemoveAllWithMoreErrorThanReqSize_")
395 // Make directory with 1025 read-only files.
396 if err := MkdirAll(path, 0777); err != nil {
397 t.Fatalf("MkdirAll %q: %s", path, err)
399 for i := 0; i < 1025; i++ {
400 fpath := filepath.Join(path, fmt.Sprintf("file%d", i))
401 fd, err := Create(fpath)
403 t.Fatalf("create %q: %s", fpath, err)
408 // Make the parent directory read-only. On some platforms, this is what
409 // prevents Remove from removing the files within that directory.
410 if err := Chmod(path, 0555); err != nil {
413 defer Chmod(path, 0755)
415 // This call should not hang, even on a platform that disallows file deletion
416 // from read-only directories.
417 err := RemoveAll(path)
420 // On many platforms, root can remove files from read-only directories.
424 if runtime.GOOS == "windows" || runtime.GOOS == "wasip1" {
425 // Marking a directory as read-only in Windows does not prevent the RemoveAll
426 // from creating or removing files within it.
428 // For wasip1, there is no support for file permissions so we cannot prevent
429 // RemoveAll from removing the files.
432 t.Fatal("RemoveAll(<read-only directory>) = nil; want error")
435 dir, err := Open(path)
441 names, _ := dir.Readdirnames(1025)
442 if len(names) < 1025 {
443 t.Fatalf("RemoveAll(<read-only directory>) unexpectedly removed %d read-only files from that directory", 1025-len(names))
447 func TestRemoveAllNoFcntl(t *testing.T) {
449 t.Skip("skipping in short mode")
452 const env = "GO_TEST_REMOVE_ALL_NO_FCNTL"
453 if dir := Getenv(env); dir != "" {
454 if err := RemoveAll(dir); err != nil {
460 // Only test on Linux so that we can assume we have strace.
461 // The code is OS-independent so if it passes on Linux
462 // it should pass on other Unix systems.
463 if runtime.GOOS != "linux" {
464 t.Skipf("skipping test on %s", runtime.GOOS)
466 if _, err := Stat("/bin/strace"); err != nil {
467 t.Skipf("skipping test because /bin/strace not found: %v", err)
469 me, err := Executable()
471 t.Skipf("skipping because Executable failed: %v", err)
474 // Create 100 directories.
475 // The test is that we can remove them without calling fcntl
477 tmpdir := t.TempDir()
478 subdir := filepath.Join(tmpdir, "subdir")
479 if err := Mkdir(subdir, 0o755); err != nil {
482 for i := 0; i < 100; i++ {
483 subsubdir := filepath.Join(subdir, strconv.Itoa(i))
484 if err := Mkdir(filepath.Join(subdir, strconv.Itoa(i)), 0o755); err != nil {
487 if err := WriteFile(filepath.Join(subsubdir, "file"), nil, 0o644); err != nil {
492 cmd := testenv.Command(t, "/bin/strace", "-f", "-e", "fcntl", me, "-test.run=^TestRemoveAllNoFcntl$")
493 cmd = testenv.CleanCmdEnv(cmd)
494 cmd.Env = append(cmd.Env, env+"="+subdir)
495 out, err := cmd.CombinedOutput()
503 if got := bytes.Count(out, []byte("fcntl")); got >= 100 {
504 t.Errorf("found %d fcntl calls, want < 100", got)