Skip to content

Commit

Permalink
fix: avoid file not found err when remove old offset file
Browse files Browse the repository at this point in the history
- add check before remove offset file

Closes #221
  • Loading branch information
xujianhai666 authored and ShannonDing committed Oct 14, 2019
1 parent 2eed2fa commit 805bfbf
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions internal/utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
Expand All @@ -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)
}

0 comments on commit 805bfbf

Please sign in to comment.