removeall_noat.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. //go:build !aix && !darwin && !dragonfly && !freebsd && !hurd && !linux && !netbsd && !openbsd && !solaris
  5. package os
  6. import (
  7. "io"
  8. "runtime"
  9. "syscall"
  10. )
  11. func removeAll(path string) error {
  12. if path == "" {
  13. // fail silently to retain compatibility with previous behavior
  14. // of RemoveAll. See issue 28830.
  15. return nil
  16. }
  17. // The rmdir system call permits removing "." on Plan 9,
  18. // so we don't permit it to remain consistent with the
  19. // "at" implementation of RemoveAll.
  20. if endsWithDot(path) {
  21. return &PathError{Op: "RemoveAll", Path: path, Err: syscall.EINVAL}
  22. }
  23. // Simple case: if Remove works, we're done.
  24. err := Remove(path)
  25. if err == nil || IsNotExist(err) {
  26. return nil
  27. }
  28. // Otherwise, is this a directory we need to recurse into?
  29. dir, serr := Lstat(path)
  30. if serr != nil {
  31. if serr, ok := serr.(*PathError); ok && (IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
  32. return nil
  33. }
  34. return serr
  35. }
  36. if !dir.IsDir() {
  37. // Not a directory; return the error from Remove.
  38. return err
  39. }
  40. // Remove contents & return first error.
  41. err = nil
  42. for {
  43. fd, err := Open(path)
  44. if err != nil {
  45. if IsNotExist(err) {
  46. // Already deleted by someone else.
  47. return nil
  48. }
  49. return err
  50. }
  51. const reqSize = 1024
  52. var names []string
  53. var readErr error
  54. for {
  55. numErr := 0
  56. names, readErr = fd.Readdirnames(reqSize)
  57. for _, name := range names {
  58. err1 := RemoveAll(path + string(PathSeparator) + name)
  59. if err == nil {
  60. err = err1
  61. }
  62. if err1 != nil {
  63. numErr++
  64. }
  65. }
  66. // If we can delete any entry, break to start new iteration.
  67. // Otherwise, we discard current names, get next entries and try deleting them.
  68. if numErr != reqSize {
  69. break
  70. }
  71. }
  72. // Removing files from the directory may have caused
  73. // the OS to reshuffle it. Simply calling Readdirnames
  74. // again may skip some entries. The only reliable way
  75. // to avoid this is to close and re-open the
  76. // directory. See issue 20841.
  77. fd.Close()
  78. if readErr == io.EOF {
  79. break
  80. }
  81. // If Readdirnames returned an error, use it.
  82. if err == nil {
  83. err = readErr
  84. }
  85. if len(names) == 0 {
  86. break
  87. }
  88. // We don't want to re-open unnecessarily, so if we
  89. // got fewer than request names from Readdirnames, try
  90. // simply removing the directory now. If that
  91. // succeeds, we are done.
  92. if len(names) < reqSize {
  93. err1 := Remove(path)
  94. if err1 == nil || IsNotExist(err1) {
  95. return nil
  96. }
  97. if err != nil {
  98. // We got some error removing the
  99. // directory contents, and since we
  100. // read fewer names than we requested
  101. // there probably aren't more files to
  102. // remove. Don't loop around to read
  103. // the directory again. We'll probably
  104. // just get the same error.
  105. return err
  106. }
  107. }
  108. }
  109. // Remove directory.
  110. err1 := Remove(path)
  111. if err1 == nil || IsNotExist(err1) {
  112. return nil
  113. }
  114. if runtime.GOOS == "windows" && IsPermission(err1) {
  115. if fs, err := Stat(path); err == nil {
  116. if err = Chmod(path, FileMode(0200|int(fs.Mode()))); err == nil {
  117. err1 = Remove(path)
  118. }
  119. }
  120. }
  121. if err == nil {
  122. err = err1
  123. }
  124. return err
  125. }