Browse Source

WriteFile*() takes file mode

pull/1842/head
Jae Kwon 9 years ago
parent
commit
3b50efbe02
1 changed files with 7 additions and 7 deletions
  1. +7
    -7
      os.go

+ 7
- 7
os.go View File

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


Loading…
Cancel
Save