From 0a17fc87642a8677fa4844904043cf7e318145f8 Mon Sep 17 00:00:00 2001 From: Tiger Date: Sun, 10 May 2020 18:04:43 +0530 Subject: [PATCH] clean up tmp file Signed-off-by: Tiger --- repo/fsrepo/fsrepo.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index 41a94a08c..103597fea 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -374,7 +374,16 @@ func (r *FSRepo) SetAPIAddr(addr ma.Multiaddr) error { } // Atomically rename the temp file to the correct file name. - return os.Rename(filepath.Join(r.path, "."+apiFile+".tmp"), filepath.Join(r.path, apiFile)) + 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 } // openConfig returns an error if the config file is not present.