Skip to content

Commit

Permalink
Merge pull request #7282 from balajijinnah/balaji/create_file_atomically
Browse files Browse the repository at this point in the history
write api file automically
  • Loading branch information
Stebalien committed May 10, 2020
2 parents 19064b2 + 0a17fc8 commit fa8c88b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,30 @@ func (r *FSRepo) Path() string {

// SetAPIAddr writes the API Addr to the /api file.
func (r *FSRepo) SetAPIAddr(addr ma.Multiaddr) error {
f, err := os.Create(filepath.Join(r.path, apiFile))
// Create a temp file to write the address, so that we don't leave empty file when the
// program crashes after creating the file.
f, err := os.Create(filepath.Join(r.path, "."+apiFile+".tmp"))
if err != nil {
return err
}
defer f.Close()

_, err = f.WriteString(addr.String())
if _, err = f.WriteString(addr.String()); err != nil {
return err
}
if err = f.Close(); err != nil {
return err
}

// Atomically rename the temp file to the correct file name.
if err = os.Rename(filepath.Join(r.path, "."+apiFile+".tmp"), filepath.Join(r.path,
apiFile)); err == nil {
return nil
}
// Remove the temp file when rename return error
if err1 := os.Remove(filepath.Join(r.path, "."+apiFile+".tmp")); err1 != nil {
return fmt.Errorf("File Rename error: %s, File remove error: %s", err.Error(),
err1.Error())
}
return err
}

Expand Down

0 comments on commit fa8c88b

Please sign in to comment.