| 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. |
| 4 |
|
| 5 |
package os_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
. "os" |
| 11 |
"path/filepath" |
| 12 |
"runtime" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
) |
| 16 |
|
| 17 |
func TestRemoveAll(t *testing.T) { |
| 18 |
tmpDir := t.TempDir() |
| 19 |
if err := RemoveAll(""); err != nil { |
| 20 |
t.Errorf("RemoveAll(\"\"): %v; want nil", err) |
| 21 |
} |
| 22 |
|
| 23 |
file := filepath.Join(tmpDir, "file") |
| 24 |
path := filepath.Join(tmpDir, "_TestRemoveAll_") |
| 25 |
fpath := filepath.Join(path, "file") |
| 26 |
dpath := filepath.Join(path, "dir") |
| 27 |
|
| 28 |
// Make a regular file and remove |
| 29 |
fd, err := Create(file) |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("create %q: %s", file, err) |
| 32 |
} |
| 33 |
fd.Close() |
| 34 |
if err = RemoveAll(file); err != nil { |
| 35 |
t.Fatalf("RemoveAll %q (first): %s", file, err) |
| 36 |
} |
| 37 |
if _, err = Lstat(file); err == nil { |
| 38 |
t.Fatalf("Lstat %q succeeded after RemoveAll (first)", file) |
| 39 |
} |
| 40 |
|
| 41 |
// Make directory with 1 file and remove. |
| 42 |
if err := MkdirAll(path, 0777); err != nil { |
| 43 |
t.Fatalf("MkdirAll %q: %s", path, err) |
| 44 |
} |
| 45 |
fd, err = Create(fpath) |
| 46 |
if err != nil { |
| 47 |
t.Fatalf("create %q: %s", fpath, err) |
| 48 |
} |
| 49 |
fd.Close() |
| 50 |
if err = RemoveAll(path); err != nil { |
| 51 |
t.Fatalf("RemoveAll %q (second): %s", path, err) |
| 52 |
} |
| 53 |
if _, err = Lstat(path); err == nil { |
| 54 |
t.Fatalf("Lstat %q succeeded after RemoveAll (second)", path) |
| 55 |
} |
| 56 |
|
| 57 |
// Make directory with file and subdirectory and remove. |
| 58 |
if err = MkdirAll(dpath, 0777); err != nil { |
| 59 |
t.Fatalf("MkdirAll %q: %s", dpath, err) |
| 60 |
} |
| 61 |
fd, err = Create(fpath) |
| 62 |
if err != nil { |
| 63 |
t.Fatalf("create %q: %s", fpath, err) |
| 64 |
} |
| 65 |
fd.Close() |
| 66 |
fd, err = Create(dpath + "/file") |
| 67 |
if err != nil { |
| 68 |
t.Fatalf("create %q: %s", fpath, err) |
| 69 |
} |
| 70 |
fd.Close() |
| 71 |
if err = RemoveAll(path); err != nil { |
| 72 |
t.Fatalf("RemoveAll %q (third): %s", path, err) |
| 73 |
} |
| 74 |
if _, err := Lstat(path); err == nil { |
| 75 |
t.Fatalf("Lstat %q succeeded after RemoveAll (third)", path) |
| 76 |
} |
| 77 |
|
| 78 |
// Chmod is not supported under Windows and test fails as root. |
| 79 |
if runtime.GOOS != "windows" && Getuid() != 0 { |
| 80 |
// Make directory with file and subdirectory and trigger error. |
| 81 |
if err = MkdirAll(dpath, 0777); err != nil { |
| 82 |
t.Fatalf("MkdirAll %q: %s", dpath, err) |
| 83 |
} |
| 84 |
|
| 85 |
for _, s := range []string{fpath, dpath + "/file1", path + "/zzz"} { |
| 86 |
fd, err = Create(s) |
| 87 |
if err != nil { |
| 88 |
t.Fatalf("create %q: %s", s, err) |
| 89 |
} |
| 90 |
fd.Close() |
| 91 |
} |
| 92 |
if err = Chmod(dpath, 0); err != nil { |
| 93 |
t.Fatalf("Chmod %q 0: %s", dpath, err) |
| 94 |
} |
| 95 |
|
| 96 |
// No error checking here: either RemoveAll |
| 97 |
// will or won't be able to remove dpath; |
| 98 |
// either way we want to see if it removes fpath |
| 99 |
// and path/zzz. Reasons why RemoveAll might |
| 100 |
// succeed in removing dpath as well include: |
| 101 |
// * running as root |
| 102 |
// * running on a file system without permissions (FAT) |
| 103 |
RemoveAll(path) |
| 104 |
Chmod(dpath, 0777) |
| 105 |
|
| 106 |
for _, s := range []string{fpath, path + "/zzz"} { |
| 107 |
if _, err = Lstat(s); err == nil { |
| 108 |
t.Fatalf("Lstat %q succeeded after partial RemoveAll", s) |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
if err = RemoveAll(path); err != nil { |
| 113 |
t.Fatalf("RemoveAll %q after partial RemoveAll: %s", path, err) |
| 114 |
} |
| 115 |
if _, err = Lstat(path); err == nil { |
| 116 |
t.Fatalf("Lstat %q succeeded after RemoveAll (final)", path) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Test RemoveAll on a large directory. |
| 121 |
func TestRemoveAllLarge(t *testing.T) { |
| 122 |
if testing.Short() { |
| 123 |
t.Skip("skipping in short mode") |
| 124 |
} |
| 125 |
|
| 126 |
tmpDir := t.TempDir() |
| 127 |
path := filepath.Join(tmpDir, "_TestRemoveAllLarge_") |
| 128 |
|
| 129 |
// Make directory with 1000 files and remove. |
| 130 |
if err := MkdirAll(path, 0777); err != nil { |
| 131 |
t.Fatalf("MkdirAll %q: %s", path, err) |
| 132 |
} |
| 133 |
for i := 0; i < 1000; i++ { |
| 134 |
fpath := fmt.Sprintf("%s/file%d", path, i) |
| 135 |
fd, err := Create(fpath) |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("create %q: %s", fpath, err) |
| 138 |
} |
| 139 |
fd.Close() |
| 140 |
} |
| 141 |
if err := RemoveAll(path); err != nil { |
| 142 |
t.Fatalf("RemoveAll %q: %s", path, err) |
| 143 |
} |
| 144 |
if _, err := Lstat(path); err == nil { |
| 145 |
t.Fatalf("Lstat %q succeeded after RemoveAll", path) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestRemoveAllLongPath(t *testing.T) { |
| 150 |
switch runtime.GOOS { |
| 151 |
case "aix", "darwin", "ios", "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "illumos", "solaris": |
| 152 |
break |
| 153 |
default: |
| 154 |
t.Skip("skipping for not implemented platforms") |
| 155 |
} |
| 156 |
|
| 157 |
prevDir, err := Getwd() |
| 158 |
if err != nil { |
| 159 |
t.Fatalf("Could not get wd: %s", err) |
| 160 |
} |
| 161 |
|
| 162 |
startPath, err := os.MkdirTemp("", "TestRemoveAllLongPath-") |
| 163 |
if err != nil { |
| 164 |
t.Fatalf("Could not create TempDir: %s", err) |
| 165 |
} |
| 166 |
defer RemoveAll(startPath) |
| 167 |
|
| 168 |
err = Chdir(startPath) |
| 169 |
if err != nil { |
| 170 |
t.Fatalf("Could not chdir %s: %s", startPath, err) |
| 171 |
} |
| 172 |
|
| 173 |
// Removing paths with over 4096 chars commonly fails |
| 174 |
for i := 0; i < 41; i++ { |
| 175 |
name := strings.Repeat("a", 100) |
| 176 |
|
| 177 |
err = Mkdir(name, 0755) |
| 178 |
if err != nil { |
| 179 |
t.Fatalf("Could not mkdir %s: %s", name, err) |
| 180 |
} |
| 181 |
|
| 182 |
err = Chdir(name) |
| 183 |
if err != nil { |
| 184 |
t.Fatalf("Could not chdir %s: %s", name, err) |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
err = Chdir(prevDir) |
| 189 |
if err != nil { |
| 190 |
t.Fatalf("Could not chdir %s: %s", prevDir, err) |
| 191 |
} |
| 192 |
|
| 193 |
err = RemoveAll(startPath) |
| 194 |
if err != nil { |
| 195 |
t.Errorf("RemoveAll could not remove long file path %s: %s", startPath, err) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
func TestRemoveAllDot(t *testing.T) { |
| 200 |
prevDir, err := Getwd() |
| 201 |
if err != nil { |
| 202 |
t.Fatalf("Could not get wd: %s", err) |
| 203 |
} |
| 204 |
tempDir, err := os.MkdirTemp("", "TestRemoveAllDot-") |
| 205 |
if err != nil { |
| 206 |
t.Fatalf("Could not create TempDir: %s", err) |
| 207 |
} |
| 208 |
defer RemoveAll(tempDir) |
| 209 |
|
| 210 |
err = Chdir(tempDir) |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("Could not chdir to tempdir: %s", err) |
| 213 |
} |
| 214 |
|
| 215 |
err = RemoveAll(".") |
| 216 |
if err == nil { |
| 217 |
t.Errorf("RemoveAll succeed to remove .") |
| 218 |
} |
| 219 |
|
| 220 |
err = Chdir(prevDir) |
| 221 |
if err != nil { |
| 222 |
t.Fatalf("Could not chdir %s: %s", prevDir, err) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
func TestRemoveAllDotDot(t *testing.T) { |
| 227 |
t.Parallel() |
| 228 |
|
| 229 |
tempDir := t.TempDir() |
| 230 |
subdir := filepath.Join(tempDir, "x") |
| 231 |
subsubdir := filepath.Join(subdir, "y") |
| 232 |
if err := MkdirAll(subsubdir, 0777); err != nil { |
| 233 |
t.Fatal(err) |
| 234 |
} |
| 235 |
if err := RemoveAll(filepath.Join(subsubdir, "..")); err != nil { |
| 236 |
t.Error(err) |
| 237 |
} |
| 238 |
for _, dir := range []string{subsubdir, subdir} { |
| 239 |
if _, err := Stat(dir); err == nil { |
| 240 |
t.Errorf("%s: exists after RemoveAll", dir) |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
// Issue #29178. |
| 246 |
func TestRemoveReadOnlyDir(t *testing.T) { |
| 247 |
t.Parallel() |
| 248 |
|
| 249 |
tempDir := t.TempDir() |
| 250 |
subdir := filepath.Join(tempDir, "x") |
| 251 |
if err := Mkdir(subdir, 0); err != nil { |
| 252 |
t.Fatal(err) |
| 253 |
} |
| 254 |
|
| 255 |
// If an error occurs make it more likely that removing the |
| 256 |
// temporary directory will succeed. |
| 257 |
defer Chmod(subdir, 0777) |
| 258 |
|
| 259 |
if err := RemoveAll(subdir); err != nil { |
| 260 |
t.Fatal(err) |
| 261 |
} |
| 262 |
|
| 263 |
if _, err := Stat(subdir); err == nil { |
| 264 |
t.Error("subdirectory was not removed") |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
// Issue #29983. |
| 269 |
func TestRemoveAllButReadOnlyAndPathError(t *testing.T) { |
| 270 |
switch runtime.GOOS { |
| 271 |
case "js", "windows": |
| 272 |
t.Skipf("skipping test on %s", runtime.GOOS) |
| 273 |
} |
| 274 |
|
| 275 |
if Getuid() == 0 { |
| 276 |
t.Skip("skipping test when running as root") |
| 277 |
} |
| 278 |
|
| 279 |
t.Parallel() |
| 280 |
|
| 281 |
tempDir := t.TempDir() |
| 282 |
dirs := []string{ |
| 283 |
"a", |
| 284 |
"a/x", |
| 285 |
"a/x/1", |
| 286 |
"b", |
| 287 |
"b/y", |
| 288 |
"b/y/2", |
| 289 |
"c", |
| 290 |
"c/z", |
| 291 |
"c/z/3", |
| 292 |
} |
| 293 |
readonly := []string{ |
| 294 |
"b", |
| 295 |
} |
| 296 |
inReadonly := func(d string) bool { |
| 297 |
for _, ro := range readonly { |
| 298 |
if d == ro { |
| 299 |
return true |
| 300 |
} |
| 301 |
dd, _ := filepath.Split(d) |
| 302 |
if filepath.Clean(dd) == ro { |
| 303 |
return true |
| 304 |
} |
| 305 |
} |
| 306 |
return false |
| 307 |
} |
| 308 |
|
| 309 |
for _, dir := range dirs { |
| 310 |
if err := Mkdir(filepath.Join(tempDir, dir), 0777); err != nil { |
| 311 |
t.Fatal(err) |
| 312 |
} |
| 313 |
} |
| 314 |
for _, dir := range readonly { |
| 315 |
d := filepath.Join(tempDir, dir) |
| 316 |
if err := Chmod(d, 0555); err != nil { |
| 317 |
t.Fatal(err) |
| 318 |
} |
| 319 |
|
| 320 |
// Defer changing the mode back so that the deferred |
| 321 |
// RemoveAll(tempDir) can succeed. |
| 322 |
defer Chmod(d, 0777) |
| 323 |
} |
| 324 |
|
| 325 |
err := RemoveAll(tempDir) |
| 326 |
if err == nil { |
| 327 |
t.Fatal("RemoveAll succeeded unexpectedly") |
| 328 |
} |
| 329 |
|
| 330 |
// The error should be of type *PathError. |
| 331 |
// see issue 30491 for details. |
| 332 |
if pathErr, ok := err.(*PathError); ok { |
| 333 |
want := filepath.Join(tempDir, "b", "y") |
| 334 |
if pathErr.Path != want { |
| 335 |
t.Errorf("RemoveAll(%q): err.Path=%q, want %q", tempDir, pathErr.Path, want) |
| 336 |
} |
| 337 |
} else { |
| 338 |
t.Errorf("RemoveAll(%q): error has type %T, want *fs.PathError", tempDir, err) |
| 339 |
} |
| 340 |
|
| 341 |
for _, dir := range dirs { |
| 342 |
_, err := Stat(filepath.Join(tempDir, dir)) |
| 343 |
if inReadonly(dir) { |
| 344 |
if err != nil { |
| 345 |
t.Errorf("file %q was deleted but should still exist", dir) |
| 346 |
} |
| 347 |
} else { |
| 348 |
if err == nil { |
| 349 |
t.Errorf("file %q still exists but should have been deleted", dir) |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
func TestRemoveUnreadableDir(t *testing.T) { |
| 356 |
switch runtime.GOOS { |
| 357 |
case "js": |
| 358 |
t.Skipf("skipping test on %s", runtime.GOOS) |
| 359 |
} |
| 360 |
|
| 361 |
if Getuid() == 0 { |
| 362 |
t.Skip("skipping test when running as root") |
| 363 |
} |
| 364 |
|
| 365 |
t.Parallel() |
| 366 |
|
| 367 |
tempDir := t.TempDir() |
| 368 |
target := filepath.Join(tempDir, "d0", "d1", "d2") |
| 369 |
if err := MkdirAll(target, 0755); err != nil { |
| 370 |
t.Fatal(err) |
| 371 |
} |
| 372 |
if err := Chmod(target, 0300); err != nil { |
| 373 |
t.Fatal(err) |
| 374 |
} |
| 375 |
if err := RemoveAll(filepath.Join(tempDir, "d0")); err != nil { |
| 376 |
t.Fatal(err) |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
// Issue 29921 |
| 381 |
func TestRemoveAllWithMoreErrorThanReqSize(t *testing.T) { |
| 382 |
if testing.Short() { |
| 383 |
t.Skip("skipping in short mode") |
| 384 |
} |
| 385 |
|
| 386 |
tmpDir := t.TempDir() |
| 387 |
path := filepath.Join(tmpDir, "_TestRemoveAllWithMoreErrorThanReqSize_") |
| 388 |
|
| 389 |
// Make directory with 1025 read-only files. |
| 390 |
if err := MkdirAll(path, 0777); err != nil { |
| 391 |
t.Fatalf("MkdirAll %q: %s", path, err) |
| 392 |
} |
| 393 |
for i := 0; i < 1025; i++ { |
| 394 |
fpath := filepath.Join(path, fmt.Sprintf("file%d", i)) |
| 395 |
fd, err := Create(fpath) |
| 396 |
if err != nil { |
| 397 |
t.Fatalf("create %q: %s", fpath, err) |
| 398 |
} |
| 399 |
fd.Close() |
| 400 |
} |
| 401 |
|
| 402 |
// Make the parent directory read-only. On some platforms, this is what |
| 403 |
// prevents os.Remove from removing the files within that directory. |
| 404 |
if err := Chmod(path, 0555); err != nil { |
| 405 |
t.Fatal(err) |
| 406 |
} |
| 407 |
defer Chmod(path, 0755) |
| 408 |
|
| 409 |
// This call should not hang, even on a platform that disallows file deletion |
| 410 |
// from read-only directories. |
| 411 |
err := RemoveAll(path) |
| 412 |
|
| 413 |
if Getuid() == 0 { |
| 414 |
// On many platforms, root can remove files from read-only directories. |
| 415 |
return |
| 416 |
} |
| 417 |
if err == nil { |
| 418 |
if runtime.GOOS == "windows" { |
| 419 |
// Marking a directory as read-only in Windows does not prevent the RemoveAll |
| 420 |
// from creating or removing files within it. |
| 421 |
return |
| 422 |
} |
| 423 |
t.Fatal("RemoveAll(<read-only directory>) = nil; want error") |
| 424 |
} |
| 425 |
|
| 426 |
dir, err := Open(path) |
| 427 |
if err != nil { |
| 428 |
t.Fatal(err) |
| 429 |
} |
| 430 |
defer dir.Close() |
| 431 |
|
| 432 |
names, _ := dir.Readdirnames(1025) |
| 433 |
if len(names) < 1025 { |
| 434 |
t.Fatalf("RemoveAll(<read-only directory>) unexpectedly removed %d read-only files from that directory", 1025-len(names)) |
| 435 |
} |
| 436 |
} |