You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by di...@apache.org on 2019/10/14 01:48:58 UTC

[rocketmq-client-go] branch native updated: fix: avoid file not found err when remove old offset file

This is an automated email from the ASF dual-hosted git repository.

dinglei pushed a commit to branch native
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/native by this push:
     new 805bfbf  fix: avoid file not found err when remove old offset file
805bfbf is described below

commit 805bfbfb59836c0668a54dfe9103e4e257c60411
Author: xujianhai666 <ze...@bytedance.com>
AuthorDate: Sun Oct 13 18:37:26 2019 +0800

    fix: avoid file not found err when remove old offset file
    
    - add check before remove offset file
    
    Closes #221
---
 internal/utils/files.go | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/internal/utils/files.go b/internal/utils/files.go
index a8c7865..2583d11 100644
--- a/internal/utils/files.go
+++ b/internal/utils/files.go
@@ -41,7 +41,7 @@ func FileReadAll(path string) ([]byte, error) {
 	return data, nil
 }
 
-func MakeFileIfNotExist(path string) error {
+func ensureDir(path string) error {
 	info, err := os.Stat(path)
 	if err != nil {
 		if os.IsNotExist(err) {
@@ -56,7 +56,7 @@ func MakeFileIfNotExist(path string) error {
 }
 
 func WriteToFile(path string, data []byte) error {
-	if err := MakeFileIfNotExist(filepath.Dir(path)); err != nil {
+	if err := ensureDir(filepath.Dir(path)); err != nil {
 		return err
 	}
 	tmpFile, err := os.Create(path + ".tmp")
@@ -80,6 +80,10 @@ func WriteToFile(path string, data []byte) error {
 		}
 		CheckError(fmt.Sprintf("close %s", bakFile.Name()), bakFile.Close())
 	}
-	CheckError(fmt.Sprintf("remove %s", path), os.Remove(path))
+
+	_, err = os.Stat(path)
+	if err == nil {
+		CheckError(fmt.Sprintf("remove %s", path), os.Remove(path))
+	}
 	return os.Rename(path+".tmp", path)
 }