Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
OTAdevice
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
957dd
OTAdevice
Commits
a779d0e4
Commit
a779d0e4
authored
Apr 11, 2025
by
957dd
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/update_bug' into 'master'
减少bug See merge request
!2
parents
c82c3112
c4a9b7c6
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
11 deletions
+46
-11
.gitignore
.gitignore
+5
-0
OTAdevice
goproject/OTAdevice
+0
-0
go.mod
goproject/go.mod
+3
-4
go.sum
goproject/go.sum
+2
-0
main.go
goproject/main.go
+36
-7
No files found.
.gitignore
0 → 100644
View file @
a779d0e4
backup
Deviceld.txt
log
main
\ No newline at end of file
goproject/OTAdevice
View file @
a779d0e4
No preview for this file type
goproject/go.mod
View file @
a779d0e4
...
@@ -2,7 +2,6 @@ module example.com/OTAdevice
...
@@ -2,7 +2,6 @@ module example.com/OTAdevice
go 1.21.5
go 1.21.5
require (
require github.com/go-resty/resty/v2 v2.16.5
github.com/go-resty/resty/v2 v2.16.5 // indirect
golang.org/x/net v0.33.0 // indirect
require golang.org/x/net v0.33.0 // indirect
)
goproject/go.sum
View file @
a779d0e4
...
@@ -2,3 +2,5 @@ github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptd
...
@@ -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=
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 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
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=
goproject/main.go
View file @
a779d0e4
...
@@ -93,7 +93,7 @@ func main() {
...
@@ -93,7 +93,7 @@ func main() {
for
{
for
{
time
.
Sleep
(
1
*
time
.
Minute
)
time
.
Sleep
(
1
0
*
time
.
Second
)
// 从 conf 文件中读取上次的版本号
// 从 conf 文件中读取上次的版本号
lastVersion
,
err
:=
readVersionFromConf
(
confPath
)
lastVersion
,
err
:=
readVersionFromConf
(
confPath
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -228,13 +228,42 @@ func startCarStartService() error {
...
@@ -228,13 +228,42 @@ func startCarStartService() error {
return
nil
return
nil
}
}
// 备份当前版本文件
func
backupCurrentVersion
(
downloadPath
,
backupDir
,
version
string
)
error
{
func
backupCurrentVersion
(
downloadPath
,
backupDir
,
version
string
)
error
{
if
_
,
err
:=
os
.
Stat
(
backupDir
);
os
.
IsNotExist
(
err
)
{
// 1. 确保备份目录存在且可写
os
.
MkdirAll
(
backupDir
,
0755
)
if
err
:=
os
.
MkdirAll
(
backupDir
,
0755
);
err
!=
nil
{
}
return
fmt
.
Errorf
(
"failed to create backup directory: %v"
,
err
)
backupPath
:=
filepath
.
Join
(
backupDir
,
"main_"
+
version
)
}
return
os
.
Rename
(
downloadPath
,
backupPath
)
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
)
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
)
}
}
// 恢复备份文件
// 恢复备份文件
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment