Commit c4a9b7c6 authored by 957dd's avatar 957dd

减少bug

parent c82c3112
backup
Deviceld.txt
log
main
\ No newline at end of file
No preview for this file type
......@@ -2,7 +2,6 @@ module example.com/OTAdevice
go 1.21.5
require (
github.com/go-resty/resty/v2 v2.16.5 // indirect
golang.org/x/net v0.33.0 // indirect
)
require github.com/go-resty/resty/v2 v2.16.5
require golang.org/x/net v0.33.0 // indirect
......@@ -2,3 +2,5 @@ github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptd
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
......@@ -93,7 +93,7 @@ func main() {
for {
time.Sleep(1 * time.Minute)
time.Sleep(10 * time.Second)
// 从 conf 文件中读取上次的版本号
lastVersion, err := readVersionFromConf(confPath)
if err != nil {
......@@ -228,13 +228,42 @@ func startCarStartService() error {
return nil
}
// 备份当前版本文件
func backupCurrentVersion(downloadPath, backupDir, version string) error {
if _, err := os.Stat(backupDir); os.IsNotExist(err) {
os.MkdirAll(backupDir, 0755)
// 1. 确保备份目录存在且可写
if err := os.MkdirAll(backupDir, 0755); err != nil {
return fmt.Errorf("failed to create backup directory: %v", err)
}
if err := os.WriteFile(filepath.Join(backupDir, ".test"), []byte(""), 0644); err != nil {
return fmt.Errorf("backup directory is not writable: %v", err)
}
os.Remove(filepath.Join(backupDir, ".test"))
// 2. 移动文件到备份目录(临时名称,避免冲突)
tempPath := filepath.Join(backupDir, fmt.Sprintf("main_%d.tmp", time.Now().UnixNano()))
if err := os.Rename(downloadPath, tempPath); err != nil {
// 跨文件系统回退方案
if err := copyFile(downloadPath, tempPath); err != nil {
return fmt.Errorf("failed to move/copy file: %v", err)
}
os.Remove(downloadPath)
}
// 3. 重命名为最终版本名称
backupPath := filepath.Join(backupDir, "main_"+version)
return os.Rename(downloadPath, backupPath)
if err := os.Rename(tempPath, backupPath); err != nil {
return fmt.Errorf("failed to rename backup file: %v", err)
}
return nil
}
// 跨文件系统的复制函数
func copyFile(src, dst string) error {
input, err := os.ReadFile(src)
if err != nil {
return err
}
return os.WriteFile(dst, input, 0644)
}
// 恢复备份文件
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment