WriteFile*() takes file mode
This commit is contained in:
parent
4b6741ca3b
commit
3b50efbe02
14
os.go
14
os.go
|
@ -64,8 +64,8 @@ func MustReadFile(filePath string) []byte {
|
||||||
return fileBytes
|
return fileBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteFile(filePath string, contents []byte) error {
|
func WriteFile(filePath string, contents []byte, mode os.FileMode) error {
|
||||||
err := ioutil.WriteFile(filePath, contents, 0600)
|
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) {
|
func MustWriteFile(filePath string, contents []byte, mode os.FileMode) {
|
||||||
err := WriteFile(filePath, contents)
|
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…
Reference in New Issue