mirror of https://github.com/ehang-io/nps
fix file not close before rename
parent
3ec790d98d
commit
f6063e0d6c
|
@ -3,6 +3,7 @@ package file
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/astaxie/beego/logs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -151,7 +152,6 @@ func storeSyncMapToFile(m sync.Map, filePath string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
m.Range(func(key, value interface{}) bool {
|
m.Range(func(key, value interface{}) bool {
|
||||||
var b []byte
|
var b []byte
|
||||||
var err error
|
var err error
|
||||||
|
@ -191,6 +191,11 @@ func storeSyncMapToFile(m sync.Map, filePath string) {
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
_ = file.Sync()
|
_ = file.Sync()
|
||||||
|
_ = file.Close()
|
||||||
|
// must close file first, then rename it
|
||||||
err = os.Rename(filePath+".tmp", filePath)
|
err = os.Rename(filePath+".tmp", filePath)
|
||||||
|
if err != nil {
|
||||||
|
logs.Error(err, "store to file err, data will lost")
|
||||||
|
}
|
||||||
// replace the file, maybe provides atomic operation
|
// replace the file, maybe provides atomic operation
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue