From 17ec10436f22a63e4c0e05375df329428f7fa0a8 Mon Sep 17 00:00:00 2001 From: Tiger Date: Wed, 6 May 2020 22:42:17 +0530 Subject: [PATCH 1/3] 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 a14e96ff25c..ba62e3059de 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. From 03b1a603c3c7a8b7de79dc9ea04012f14af761de Mon Sep 17 00:00:00 2001 From: Tiger Date: Thu, 7 May 2020 23:34:42 +0530 Subject: [PATCH 2/3] change tmp name Signed-off-by: Tiger --- repo/fsrepo/fsrepo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/repo/fsrepo/fsrepo.go b/repo/fsrepo/fsrepo.go index ba62e3059de..41a94a08ca7 100644 --- a/repo/fsrepo/fsrepo.go +++ b/repo/fsrepo/fsrepo.go @@ -361,7 +361,7 @@ func (r *FSRepo) Path() string { func (r *FSRepo) SetAPIAddr(addr ma.Multiaddr) error { // 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")) + f, err := os.Create(filepath.Join(r.path, "."+apiFile+".tmp")) if err != nil { return err } @@ -374,7 +374,7 @@ 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)) + 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. From 0a17fc87642a8677fa4844904043cf7e318145f8 Mon Sep 17 00:00:00 2001 From: Tiger Date: Sun, 10 May 2020 18:04:43 +0530 Subject: [PATCH 3/3] 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 41a94a08ca7..103597feac1 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.