Skip to content

Commit

Permalink
Update perms.WriteFile to write atomically (ava-labs#2063)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
marun and StephenButtolph authored Sep 22, 2023
1 parent 3c9a2e6 commit d42c75f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/ethereum/go-ethereum v1.12.0
github.com/golang-jwt/jwt/v4 v4.3.0
github.com/google/btree v1.1.2
github.com/google/renameio/v2 v2.0.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/rpc v1.2.0
github.com/gorilla/websocket v1.4.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 h1:4/hN5RUoecvl+RmJRE2YxKWtnnQls6rQjjW5oV7qg2U=
github.com/google/pprof v0.0.0-20230207041349-798e818bf904/go.mod h1:uglQLonpP8qtYCYyzA+8c/9qtqgA3qsXGYqCPKARAFg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/renameio/v2 v2.0.0 h1:UifI23ZTGY8Tt29JbYFiuyIU3eX+RNFtUwefq9qAhxg=
github.com/google/renameio/v2 v2.0.0/go.mod h1:BtmJXm5YlszgC+TD4HOEEUFgkJP3nLxehU6hfe7jRt4=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (n *Node) writeProcessContext() error {
if err != nil {
return fmt.Errorf("failed to marshal process context: %w", err)
}
if err := os.WriteFile(n.Config.ProcessContextFilePath, bytes, perms.ReadWrite); err != nil {
if err := perms.WriteFile(n.Config.ProcessContextFilePath, bytes, perms.ReadWrite); err != nil {
return fmt.Errorf("failed to write process context: %w", err)
}
return nil
Expand Down
9 changes: 6 additions & 3 deletions utils/perms/write_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ package perms
import (
"errors"
"os"

"github.com/google/renameio/v2/maybe"
)

// WriteFile writes [data] to [filename] and ensures that [filename] has [perm]
// permissions.
// permissions. Will write atomically on linux/macos and fall back to non-atomic
// ioutil.WriteFile on windows.
func WriteFile(filename string, data []byte, perm os.FileMode) error {
info, err := os.Stat(filename)
if errors.Is(err, os.ErrNotExist) {
// The file doesn't exist, so try to write it.
return os.WriteFile(filename, data, perm)
return maybe.WriteFile(filename, data, perm)
}
if err != nil {
return err
Expand All @@ -27,5 +30,5 @@ func WriteFile(filename string, data []byte, perm os.FileMode) error {
}
// The file has the right permissions, so truncate any data and write the
// file.
return os.WriteFile(filename, data, perm)
return maybe.WriteFile(filename, data, perm)
}

0 comments on commit d42c75f

Please sign in to comment.