|
|
@ -64,8 +64,8 @@ func MustReadFile(filePath string) []byte { |
|
|
|
return fileBytes |
|
|
|
} |
|
|
|
|
|
|
|
func WriteFile(filePath string, contents []byte) error { |
|
|
|
err := ioutil.WriteFile(filePath, contents, 0600) |
|
|
|
func WriteFile(filePath string, contents []byte, mode os.FileMode) error { |
|
|
|
err := ioutil.WriteFile(filePath, contents, mode) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
@ -73,8 +73,8 @@ func WriteFile(filePath string, contents []byte) error { |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func MustWriteFile(filePath string, contents []byte) { |
|
|
|
err := WriteFile(filePath, contents) |
|
|
|
func MustWriteFile(filePath string, contents []byte, mode os.FileMode) { |
|
|
|
err := WriteFile(filePath, contents, mode) |
|
|
|
if err != nil { |
|
|
|
Exit(Fmt("MustWriteFile failed: %v", err)) |
|
|
|
} |
|
|
@ -83,20 +83,20 @@ func MustWriteFile(filePath string, contents []byte) { |
|
|
|
// Writes to newBytes to filePath.
|
|
|
|
// Guaranteed not to lose *both* oldBytes and newBytes,
|
|
|
|
// (assuming that the OS is perfect)
|
|
|
|
func WriteFileAtomic(filePath string, newBytes []byte) error { |
|
|
|
func WriteFileAtomic(filePath string, newBytes []byte, mode os.FileMode) error { |
|
|
|
// If a file already exists there, copy to filePath+".bak" (overwrite anything)
|
|
|
|
if _, err := os.Stat(filePath); !os.IsNotExist(err) { |
|
|
|
fileBytes, err := ioutil.ReadFile(filePath) |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("Could not read file %v. %v", filePath, err) |
|
|
|
} |
|
|
|
err = ioutil.WriteFile(filePath+".bak", fileBytes, 0600) |
|
|
|
err = ioutil.WriteFile(filePath+".bak", fileBytes, mode) |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("Could not write file %v. %v", filePath+".bak", err) |
|
|
|
} |
|
|
|
} |
|
|
|
// Write newBytes to filePath.new
|
|
|
|
err := ioutil.WriteFile(filePath+".new", newBytes, 0600) |
|
|
|
err := ioutil.WriteFile(filePath+".new", newBytes, mode) |
|
|
|
if err != nil { |
|
|
|
return fmt.Errorf("Could not write file %v. %v", filePath+".new", err) |
|
|
|
} |
|
|
|