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>
This commit is contained in:
Alex Brainman 2019-04-27 11:02:31 +10:00
parent 4fdeb73fed
commit e85d619530
3 changed files with 36 additions and 3 deletions

View File

@ -0,0 +1,12 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !windows
package renameio
// isAccessDeniedError always returns false on non-windows.
func isAccessDeniedError(err error) bool {
return false
}

View File

@ -0,0 +1,23 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package renameio
import (
"os"
"syscall"
)
// isAccessDeniedError returns true if err was caused by ERROR_ACCESS_DENIED.
func isAccessDeniedError(err error) bool {
linkerr, ok := err.(*os.LinkError)
if !ok {
return false
}
errno, ok := linkerr.Err.(syscall.Errno)
if !ok {
return false
}
return errno == syscall.ERROR_ACCESS_DENIED
}

View File

@ -11,8 +11,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
@ -66,7 +64,7 @@ func WriteToFile(filename string, data io.Reader) (err error) {
var start time.Time
for {
err := os.Rename(f.Name(), filename)
if err == nil || runtime.GOOS != "windows" || !strings.HasSuffix(err.Error(), "Access is denied.") {
if err == nil || !isAccessDeniedError(err) {
return err
}