mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-25 18:51:39 +00:00
Config is passed into each module. Remove tendermint/confer
This commit is contained in:
84
common/os.go
84
common/os.go
@ -28,31 +28,6 @@ func Exit(s string) {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Writes to newBytes to filePath.
|
||||
// Guaranteed not to lose *both* oldBytes and newBytes,
|
||||
// (assuming that the OS is perfect)
|
||||
func AtomicWriteFile(filePath string, newBytes []byte) 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("Failed to read file %v. %v", filePath, err)
|
||||
}
|
||||
err = ioutil.WriteFile(filePath+".bak", fileBytes, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to write file %v. %v", filePath+".bak", err)
|
||||
}
|
||||
}
|
||||
// Write newBytes to filePath.new
|
||||
err := ioutil.WriteFile(filePath+".new", newBytes, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to write file %v. %v", filePath+".new", err)
|
||||
}
|
||||
// Move filePath.new to filePath
|
||||
err = os.Rename(filePath+".new", filePath)
|
||||
return err
|
||||
}
|
||||
|
||||
func EnsureDir(dir string) error {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(dir, 0700)
|
||||
@ -62,3 +37,62 @@ func EnsureDir(dir string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func FileExists(filePath string) bool {
|
||||
_, err := os.Stat(filePath)
|
||||
return !os.IsNotExist(err)
|
||||
}
|
||||
|
||||
func ReadFile(filePath string) ([]byte, error) {
|
||||
return ioutil.ReadFile(filePath)
|
||||
}
|
||||
|
||||
func MustReadFile(filePath string) []byte {
|
||||
fileBytes, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
Exit(Fmt("MustReadFile failed: %v", err))
|
||||
return nil
|
||||
}
|
||||
return fileBytes
|
||||
}
|
||||
|
||||
func WriteFile(filePath string, contents []byte) error {
|
||||
err := ioutil.WriteFile(filePath, contents, 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("File written to %v.\n", filePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
func MustWriteFile(filePath string, contents []byte) {
|
||||
err := WriteFile(filePath, contents)
|
||||
if err != nil {
|
||||
Exit(Fmt("MustWriteFile failed: %v", err))
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// 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)
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not write file %v. %v", filePath+".new", err)
|
||||
}
|
||||
// Move filePath.new to filePath
|
||||
err = os.Rename(filePath+".new", filePath)
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user