Skip to content

Commit

Permalink
Use writeAndSyncFile instead of just write
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Buil <[email protected]>
  • Loading branch information
manuelbuil committed Jul 19, 2023
1 parent 04854df commit 088da1a
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion flannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -207,7 +208,28 @@ func saveScratchNetConf(containerID, dataDir string, netconf []byte) error {
return err
}
path := filepath.Join(dataDir, containerID)
return os.WriteFile(path, netconf, 0600)
return writeAndSyncFile(path, netconf, 0600)
}

// WriteAndSyncFile behaves just like ioutil.WriteFile in the standard library,
// but calls Sync before closing the file. WriteAndSyncFile guarantees the data
// is synced if there is no error returned.
func writeAndSyncFile(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 err == nil && n < len(data) {
err = io.ErrShortWrite
}
if err == nil {
err = f.Sync()
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}

func consumeScratchNetConf(containerID, dataDir string) (func(error), []byte, error) {
Expand Down

0 comments on commit 088da1a

Please sign in to comment.