Skip to content

Instantly share code, notes, and snippets.

@tyru
Created December 11, 2017 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyru/b6c8cfa8b4072ca0ee791065c669d79a to your computer and use it in GitHub Desktop.
Save tyru/b6c8cfa8b4072ca0ee791065c669d79a to your computer and use it in GitHub Desktop.
The modified patch for go1.10beta1 of WSL (Windows Subsystem Linux) specific problem of os.RemoveAll(): https://github.com/vim-volt/volt/issues/1
--- src/os/path.go.old 2017-12-11 19:45:29.722217500 +0900
+++ src/os/path.go 2017-12-11 19:51:19.668455900 +0900
@@ -84,26 +84,35 @@
return err
}
- // Directory.
- fd, err := Open(path)
- if err != nil {
- if IsNotExist(err) {
- // Race. It was deleted between the Lstat and Open.
- // Return nil per RemoveAll's docs.
- return nil
+ // nextNames enumerates the entries of path, which is a directory.
+ // In POSIX the behavior of Readdir after asynchronous removal of files is
+ // undefined. To remove all files, on all filesystems across all platforms, one
+ // must re-open the directory fd between enumerations.
+ nextNames := func(prevErr error) ([]string, error) {
+ fd, err := Open(path)
+ if err != nil {
+ if IsNotExist(err) {
+ // Race. It was deleted between the Lstat and Open.
+ // Return nil per RemoveAll's docs.
+ return nil, nil
+ }
+ return nil, err
}
- return err
- }
+ defer fd.Close()
- // Remove contents & return first error.
- err = nil
- for {
- if err == nil && (runtime.GOOS == "plan9" || runtime.GOOS == "nacl") {
+ if prevErr == nil && (runtime.GOOS == "plan9" || runtime.GOOS == "nacl") {
// Reset read offset after removing directory entries.
// See golang.org/issue/22572.
fd.Seek(0, 0)
}
- names, err1 := fd.Readdirnames(100)
+
+ return fd.Readdirnames(1024)
+ }
+
+ // Remove contents & return first error.
+ err = nil
+ for {
+ names, err1 := nextNames(err)
for _, name := range names {
err1 := RemoveAll(path + string(PathSeparator) + name)
if err == nil {
@@ -122,9 +131,6 @@
}
}
- // Close directory, because windows won't remove opened directory.
- fd.Close()
-
// Remove directory.
err1 := Remove(path)
if err1 == nil || IsNotExist(err1) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment