mirror of
https://github.com/golang/go
synced 2024-11-18 12:54:44 -07:00
refactor/rename: preserve file modes.
Fixes golang/go#9013 LGTM=sameer R=sameer, minux CC=golang-codereviews https://golang.org/cl/185000043
This commit is contained in:
parent
25f58f97ce
commit
a28cb655e6
@ -319,16 +319,23 @@ func plural(n int) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeFile(name string, fset *token.FileSet, f *ast.File) error {
|
func writeFile(name string, fset *token.FileSet, f *ast.File, mode os.FileMode) error {
|
||||||
out, err := os.Create(name)
|
out, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// assume error includes the filename
|
// assume error includes the filename
|
||||||
return fmt.Errorf("failed to open file: %s", err)
|
return fmt.Errorf("failed to open file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Oddly, os.OpenFile doesn't preserve all the mode bits, hence
|
||||||
|
// this chmod. (We use 0600 above to avoid a brief
|
||||||
|
// vulnerability if the user has an insecure umask.)
|
||||||
|
os.Chmod(name, mode) // ignore error
|
||||||
|
|
||||||
if err := format.Node(out, fset, f); err != nil {
|
if err := format.Node(out, fset, f); err != nil {
|
||||||
out.Close() // ignore error
|
out.Close() // ignore error
|
||||||
return fmt.Errorf("failed to write file: %s", err)
|
return fmt.Errorf("failed to write file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.Close()
|
return out.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -339,11 +346,16 @@ var rewriteFile = func(fset *token.FileSet, f *ast.File, orig string) (err error
|
|||||||
if Verbose {
|
if Verbose {
|
||||||
fmt.Fprintf(os.Stderr, "\t%s\n", orig)
|
fmt.Fprintf(os.Stderr, "\t%s\n", orig)
|
||||||
}
|
}
|
||||||
|
// save file mode
|
||||||
|
var mode os.FileMode = 0666
|
||||||
|
if fi, err := os.Stat(orig); err == nil {
|
||||||
|
mode = fi.Mode()
|
||||||
|
}
|
||||||
if err := os.Rename(orig, backup); err != nil {
|
if err := os.Rename(orig, backup); err != nil {
|
||||||
return fmt.Errorf("failed to make backup %s -> %s: %s",
|
return fmt.Errorf("failed to make backup %s -> %s: %s",
|
||||||
orig, filepath.Base(backup), err)
|
orig, filepath.Base(backup), err)
|
||||||
}
|
}
|
||||||
if err := writeFile(orig, fset, f); err != nil {
|
if err := writeFile(orig, fset, f, mode); err != nil {
|
||||||
// Restore the file from the backup.
|
// Restore the file from the backup.
|
||||||
os.Remove(orig) // ignore error
|
os.Remove(orig) // ignore error
|
||||||
os.Rename(backup, orig) // ignore error
|
os.Rename(backup, orig) // ignore error
|
||||||
|
Loading…
Reference in New Issue
Block a user