removeall_test.go 10 KB

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