This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
forked from goraft/raft
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request goraft#150 from xiangli-cmu/sync_fix
Fix file issue
- Loading branch information
Showing
3 changed files
with
52 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package raft | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
// WriteFile writes data to a file named by filename. | ||
// If the file does not exist, WriteFile creates it with permissions perm; | ||
// otherwise WriteFile truncates it before writing. | ||
// This is copied from ioutil.WriteFile with the addition of a Sync call to | ||
// ensure the data reaches the disk. | ||
func writeFileSynced(filename string, data []byte, perm os.FileMode) error { | ||
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
n, err := f.Write(data) | ||
if n < len(data) { | ||
f.Close() | ||
return io.ErrShortWrite | ||
} | ||
|
||
err = f.Sync() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return f.Close() | ||
} |