WriteFile*() takes file mode

This commit is contained in:
Jae Kwon 2015-12-03 23:56:50 -08:00
parent 4b6741ca3b
commit 3b50efbe02
1 changed files with 7 additions and 7 deletions

14
os.go
View File

@ -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)
}