Source file
src/os/removeall_noat.go
1
2
3
4
5
6
7 package os
8
9 import (
10 "io"
11 "runtime"
12 "syscall"
13 )
14
15 func removeAll(path string) error {
16 if path == "" {
17
18
19 return nil
20 }
21
22
23
24
25 if endsWithDot(path) {
26 return &PathError{Op: "RemoveAll", Path: path, Err: syscall.EINVAL}
27 }
28
29
30 err := Remove(path)
31 if err == nil || IsNotExist(err) {
32 return nil
33 }
34
35
36 dir, serr := Lstat(path)
37 if serr != nil {
38 if serr, ok := serr.(*PathError); ok && (IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
39 return nil
40 }
41 return serr
42 }
43 if !dir.IsDir() {
44
45 return err
46 }
47
48
49 err = nil
50 for {
51 fd, err := Open(path)
52 if err != nil {
53 if IsNotExist(err) {
54
55 return nil
56 }
57 return err
58 }
59
60 const reqSize = 1024
61 var names []string
62 var readErr error
63
64 for {
65 numErr := 0
66 names, readErr = fd.Readdirnames(reqSize)
67
68 for _, name := range names {
69 err1 := RemoveAll(path + string(PathSeparator) + name)
70 if err == nil {
71 err = err1
72 }
73 if err1 != nil {
74 numErr++
75 }
76 }
77
78
79
80 if numErr != reqSize {
81 break
82 }
83 }
84
85
86
87
88
89
90 fd.Close()
91
92 if readErr == io.EOF {
93 break
94 }
95
96 if err == nil {
97 err = readErr
98 }
99 if len(names) == 0 {
100 break
101 }
102
103
104
105
106
107 if len(names) < reqSize {
108 err1 := Remove(path)
109 if err1 == nil || IsNotExist(err1) {
110 return nil
111 }
112
113 if err != nil {
114
115
116
117
118
119
120
121 return err
122 }
123 }
124 }
125
126
127 err1 := Remove(path)
128 if err1 == nil || IsNotExist(err1) {
129 return nil
130 }
131 if runtime.GOOS == "windows" && IsPermission(err1) {
132 if fs, err := Stat(path); err == nil {
133 if err = Chmod(path, FileMode(0200|int(fs.Mode()))); err == nil {
134 err1 = Remove(path)
135 }
136 }
137 }
138 if err == nil {
139 err = err1
140 }
141 return err
142 }
143
View as plain text