]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/renameio: use ERROR_ACCESS_DENIED to check for errors
authorAlex Brainman <alex.brainman@gmail.com>
Sat, 27 Apr 2019 01:02:31 +0000 (11:02 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Sun, 28 Apr 2019 03:20:38 +0000 (03:20 +0000)
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>
src/cmd/go/internal/renameio/error.go [new file with mode: 0644]
src/cmd/go/internal/renameio/error_windows.go [new file with mode: 0644]
src/cmd/go/internal/renameio/renameio.go

diff --git a/src/cmd/go/internal/renameio/error.go b/src/cmd/go/internal/renameio/error.go
new file mode 100644 (file)
index 0000000..14943e3
--- /dev/null
@@ -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
+}
diff --git a/src/cmd/go/internal/renameio/error_windows.go b/src/cmd/go/internal/renameio/error_windows.go
new file mode 100644 (file)
index 0000000..30d0879
--- /dev/null
@@ -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
+}
index 3f3f1708fa48b27d7e1b1ed867d7ce2ae7c4d220..0bd40a544ad64a6f465a4da75b7ad5cee4297f6e 100644 (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
                }