Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create driver-ready file atomically #789

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,16 +864,23 @@ func createStatusFile(statusFile string) error {
}

func createStatusFileWithContent(statusFile string, content string) error {
f, err := os.Create(statusFile)
dir := filepath.Dir(statusFile)
tmpFile, err := os.CreateTemp(dir, filepath.Base(statusFile)+".*.tmp")
if err != nil {
return fmt.Errorf("unable to create status file %s: %s", statusFile, err)
return fmt.Errorf("failed to create temporary status file: %w", err)
}

_, err = f.WriteString(content)
_, err = tmpFile.WriteString(content)
tmpFile.Close()
if err != nil {
return fmt.Errorf("unable to write contents of status file %s: %s", statusFile, err)
return fmt.Errorf("failed to write temporary status file: %w", err)
}
defer func() {
_ = os.Remove(tmpFile.Name())
}()

if err := os.Rename(tmpFile.Name(), statusFile); err != nil {
return fmt.Errorf("error moving temporary file to '%v': %w", statusFile, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return fmt.Errorf("error moving temporary file to '%v': %w", statusFile, err)
return fmt.Errorf("error moving temporary file to '%s': %w", statusFile, err)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have address this comment in #790

}
return nil
}

Expand Down
Loading