Skip to content

Commit e85d619

Browse files
committed
cmd/go/internal/renameio: use ERROR_ACCESS_DENIED to check for errors
CL 172418 added code to check for "Access is denied" error. But "Access is denied" error will be spelled differently on non-English version of Windows. Check if error is ERROR_ACCESS_DENIED instead. Updates #31247 Change-Id: I7b1633013d563f7c06c1f12a9be75122106834f9 Reviewed-on: https://go-review.googlesource.com/c/go/+/174123 Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
1 parent 4fdeb73 commit e85d619

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/cmd/go/internal/renameio/error.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2019 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+
5+
// +build !windows
6+
7+
package renameio
8+
9+
// isAccessDeniedError always returns false on non-windows.
10+
func isAccessDeniedError(err error) bool {
11+
return false
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2019 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+
5+
package renameio
6+
7+
import (
8+
"os"
9+
"syscall"
10+
)
11+
12+
// isAccessDeniedError returns true if err was caused by ERROR_ACCESS_DENIED.
13+
func isAccessDeniedError(err error) bool {
14+
linkerr, ok := err.(*os.LinkError)
15+
if !ok {
16+
return false
17+
}
18+
errno, ok := linkerr.Err.(syscall.Errno)
19+
if !ok {
20+
return false
21+
}
22+
return errno == syscall.ERROR_ACCESS_DENIED
23+
}

src/cmd/go/internal/renameio/renameio.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import (
1111
"io/ioutil"
1212
"os"
1313
"path/filepath"
14-
"runtime"
15-
"strings"
1614
"time"
1715
)
1816

@@ -66,7 +64,7 @@ func WriteToFile(filename string, data io.Reader) (err error) {
6664
var start time.Time
6765
for {
6866
err := os.Rename(f.Name(), filename)
69-
if err == nil || runtime.GOOS != "windows" || !strings.HasSuffix(err.Error(), "Access is denied.") {
67+
if err == nil || !isAccessDeniedError(err) {
7068
return err
7169
}
7270

0 commit comments

Comments
 (0)