From 17ec10436f22a63e4c0e05375df329428f7fa0a8 Mon Sep 17 00:00:00 2001 From: Tiger Date: Wed, 6 May 2020 22:42:17 +0530 Subject: [PATCH] write api file automically Signed-off-by: Tiger --- repo/fsrepo/fsrepo.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index a14e96ff2..ba62e3059 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -359,14 +359,22 @@ 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()) - return err + 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. + return os.Rename(filepath.Join(r.path, apiFile+"-tmp"), filepath.Join(r.path, apiFile)) } // openConfig returns an error if the config file is not present.