Add explicit sync to disk for critical file writes (#1686)

For Orbit operations, make an explicit *os.File.Sync() call and check the 
error, to make sure the writes are fully flushed to disk.

Closes #1679 .
This commit is contained in:
Martin Angers 2021-08-17 08:41:56 -04:00 committed by GitHub
parent 1e11f0f669
commit 3c9d7fd4bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 4 deletions

View File

@ -0,0 +1 @@
* Orbit: sync critical file writes to disk.

View File

@ -60,7 +60,10 @@ func makeConfigIfNotExists(fp string) error {
}
}
_, err := secure.OpenFile(fp, os.O_RDONLY|os.O_CREATE, configFilePerms)
f, err := secure.OpenFile(fp, os.O_RDONLY|os.O_CREATE, configFilePerms)
if err == nil {
f.Close()
}
return err
}

View File

@ -137,6 +137,9 @@ func buildNFPM(opt Options, pkger nfpm.Packager) error {
if err := pkger.Package(info, out); err != nil {
return errors.Wrap(err, "write package")
}
if err := out.Sync(); err != nil {
return errors.Wrap(err, "sync output file")
}
log.Info().Str("path", filename).Msg("wrote package")
return nil

View File

@ -316,9 +316,9 @@ func cpio(srcPath, dstPath string) error {
if err != nil {
return errors.Wrap(err, "wait gzip")
}
err = dst.Close()
err = dst.Sync()
if err != nil {
return errors.Wrap(err, "close dst")
return errors.Wrap(err, "sync dst")
}
return nil

View File

@ -70,6 +70,9 @@ func copyFile(srcPath, dstPath string, perm os.FileMode) error {
if _, err := io.Copy(dst, src); err != nil {
return errors.Wrap(err, "copy src to dst")
}
if err := dst.Sync(); err != nil {
return errors.Wrap(err, "sync dst after copy")
}
return nil
}

View File

@ -88,7 +88,10 @@ func (s *fileStore) writeData() error {
defer f.Close()
if err := json.NewEncoder(f).Encode(s.metadata); err != nil {
return errors.Wrap(err, "read file store")
return errors.Wrap(err, "write file store")
}
if err := f.Sync(); err != nil {
return errors.Wrap(err, "sync file store")
}
return nil