Skip to content

Commit 0e0f798

Browse files
committed
Revert "os: add support for long path names on unix RemoveAll"
This reverts commit 85143d3. Reason for revert: Breaking all Darwin and FreeBSD builds. Trybots did not pass for this. Change-Id: I5494e14ad5ab9cf6e1e225a25b2e8b38f3359d13 Reviewed-on: https://go-review.googlesource.com/c/145897 Reviewed-by: Katie Hockman <[email protected]> Run-TryBot: Katie Hockman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 08816cb commit 0e0f798

16 files changed

+225
-685
lines changed

src/internal/syscall/unix/at.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_darwin.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_dragonfly.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_freebsd.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_fstatat64_linux.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_fstatat_linux.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_linux.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_netbsd.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_newfstatat_linux.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/internal/syscall/unix/at_sysnum_openbsd.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/os/path.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package os
66

77
import (
8+
"io"
89
"syscall"
910
)
1011

@@ -57,3 +58,101 @@ func MkdirAll(path string, perm FileMode) error {
5758
}
5859
return nil
5960
}
61+
62+
// RemoveAll removes path and any children it contains.
63+
// It removes everything it can but returns the first error
64+
// it encounters. If the path does not exist, RemoveAll
65+
// returns nil (no error).
66+
func RemoveAll(path string) error {
67+
// Simple case: if Remove works, we're done.
68+
err := Remove(path)
69+
if err == nil || IsNotExist(err) {
70+
return nil
71+
}
72+
73+
// Otherwise, is this a directory we need to recurse into?
74+
dir, serr := Lstat(path)
75+
if serr != nil {
76+
if serr, ok := serr.(*PathError); ok && (IsNotExist(serr.Err) || serr.Err == syscall.ENOTDIR) {
77+
return nil
78+
}
79+
return serr
80+
}
81+
if !dir.IsDir() {
82+
// Not a directory; return the error from Remove.
83+
return err
84+
}
85+
86+
// Remove contents & return first error.
87+
err = nil
88+
for {
89+
fd, err := Open(path)
90+
if err != nil {
91+
if IsNotExist(err) {
92+
// Already deleted by someone else.
93+
return nil
94+
}
95+
return err
96+
}
97+
98+
const request = 1024
99+
names, err1 := fd.Readdirnames(request)
100+
101+
// Removing files from the directory may have caused
102+
// the OS to reshuffle it. Simply calling Readdirnames
103+
// again may skip some entries. The only reliable way
104+
// to avoid this is to close and re-open the
105+
// directory. See issue 20841.
106+
fd.Close()
107+
108+
for _, name := range names {
109+
err1 := RemoveAll(path + string(PathSeparator) + name)
110+
if err == nil {
111+
err = err1
112+
}
113+
}
114+
115+
if err1 == io.EOF {
116+
break
117+
}
118+
// If Readdirnames returned an error, use it.
119+
if err == nil {
120+
err = err1
121+
}
122+
if len(names) == 0 {
123+
break
124+
}
125+
126+
// We don't want to re-open unnecessarily, so if we
127+
// got fewer than request names from Readdirnames, try
128+
// simply removing the directory now. If that
129+
// succeeds, we are done.
130+
if len(names) < request {
131+
err1 := Remove(path)
132+
if err1 == nil || IsNotExist(err1) {
133+
return nil
134+
}
135+
136+
if err != nil {
137+
// We got some error removing the
138+
// directory contents, and since we
139+
// read fewer names than we requested
140+
// there probably aren't more files to
141+
// remove. Don't loop around to read
142+
// the directory again. We'll probably
143+
// just get the same error.
144+
return err
145+
}
146+
}
147+
}
148+
149+
// Remove directory.
150+
err1 := Remove(path)
151+
if err1 == nil || IsNotExist(err1) {
152+
return nil
153+
}
154+
if err == nil {
155+
err = err1
156+
}
157+
return err
158+
}

0 commit comments

Comments
 (0)