Commit 75c13811 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

cmd/gofmt: don't call Chmod on windows

Fixes #18026

Change-Id: Id510f427ceffb2441c3d6f5bb5c93244e46c6497
Reviewed-on: https://go-review.googlesource.com/33477
TryBot-Result: Gobot Gobot <gobot@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarAlex Brainman <alex.brainman@gmail.com>
parent 8ace3461
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"runtime/pprof" "runtime/pprof"
"strings" "strings"
) )
...@@ -252,6 +253,8 @@ func diff(b1, b2 []byte) (data []byte, err error) { ...@@ -252,6 +253,8 @@ func diff(b1, b2 []byte) (data []byte, err error) {
} }
const chmodSupported = runtime.GOOS != "windows"
// backupFile writes data to a new file named filename<number> with permissions perm, // backupFile writes data to a new file named filename<number> with permissions perm,
// with <number randomly chosen such that the file name is unique. backupFile returns // with <number randomly chosen such that the file name is unique. backupFile returns
// the chosen file name. // the chosen file name.
...@@ -262,11 +265,13 @@ func backupFile(filename string, data []byte, perm os.FileMode) (string, error) ...@@ -262,11 +265,13 @@ func backupFile(filename string, data []byte, perm os.FileMode) (string, error)
return "", err return "", err
} }
bakname := f.Name() bakname := f.Name()
err = f.Chmod(perm) if chmodSupported {
if err != nil { err = f.Chmod(perm)
f.Close() if err != nil {
os.Remove(bakname) f.Close()
return bakname, err os.Remove(bakname)
return bakname, err
}
} }
// write data to backup file // write data to backup file
......
...@@ -171,3 +171,16 @@ func TestCRLF(t *testing.T) { ...@@ -171,3 +171,16 @@ func TestCRLF(t *testing.T) {
t.Errorf("%s contains CR's", golden) t.Errorf("%s contains CR's", golden)
} }
} }
func TestBackupFile(t *testing.T) {
dir, err := ioutil.TempDir("", "gofmt_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
name, err := backupFile(filepath.Join(dir, "foo.go"), []byte(" package main"), 0644)
if err != nil {
t.Fatal(err)
}
t.Logf("Created: %s", name)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment