1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-01-08 17:51:57 +00:00

safeio: Extend WriteFile to take operations before the final rename

This patch extends WriteFile to allow arbitrary operations to be applied
to the file before it is atomically renamed.

This will be used in upcoming patches to change the mtime of the file
before it is atomically renamed.
This commit is contained in:
Alberto Bertogli
2017-01-25 11:04:29 +00:00
parent fe00750e39
commit 79c0a17328
2 changed files with 79 additions and 3 deletions

View File

@@ -9,13 +9,21 @@ import (
"syscall"
)
// Type FileOp represents an operation on a file (passed by its name).
type FileOp func(fname string) error
// WriteFile writes data to a file named by filename, atomically.
//
// It's a wrapper to ioutil.WriteFile, but provides atomicity (and increased
// safety) by writing to a temporary file and renaming it at the end.
//
// Before the final rename, the given ops (if any) are called. They can be
// used to manipulate the file before it is atomically renamed.
// If any operation fails, the file is removed and the error is returned.
//
// Note this relies on same-directory Rename being atomic, which holds in most
// reasonably modern filesystems.
func WriteFile(filename string, data []byte, perm os.FileMode) error {
func WriteFile(filename string, data []byte, perm os.FileMode, ops ...FileOp) error {
// Note we create the temporary file in the same directory, otherwise we
// would have no expectation of Rename being atomic.
// We make the file names start with "." so there's no confusion with the
@@ -50,6 +58,13 @@ func WriteFile(filename string, data []byte, perm os.FileMode) error {
return err
}
for _, op := range ops {
if err = op(tmpf.Name()); err != nil {
os.Remove(tmpf.Name())
return err
}
}
return os.Rename(tmpf.Name(), filename)
}