From 7d33cbabf42b09732ac6bea08cad6261581144ec Mon Sep 17 00:00:00 2001 From: Mark Zhou Date: Fri, 23 Aug 2024 14:43:48 +0800 Subject: [PATCH] feat: windows agent update (#1036) --- agent/update/update_windows.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/agent/update/update_windows.go b/agent/update/update_windows.go index ad9e113c..5da3cc66 100644 --- a/agent/update/update_windows.go +++ b/agent/update/update_windows.go @@ -10,6 +10,8 @@ import ( "os" "path/filepath" "strings" + + "golang.org/x/sys/windows" ) func download(file string) (string, error) { @@ -40,7 +42,6 @@ func download(file string) (string, error) { } func Update(tar string) error { - return fmt.Errorf("linux support only") // download fname, err := download(tar) if err != nil { @@ -69,6 +70,16 @@ func Update(tar string) error { } log.Printf("I! replace old version:%s with new version:%s", ov, "./"+nv) + // rename current -> current.old + oldBackup := ov + ".old" + err = os.Rename(ov, oldBackup) + if err != nil { + return err + } + err = windows.MoveFileEx(windows.StringToUTF16Ptr(oldBackup), nil, windows.MOVEFILE_DELAY_UNTIL_REBOOT) // optional: delay delete old file + if err != nil { + log.Printf("I! cannot auto remove old file for current user. please manual remove %s. cause: %v", oldBackup, err) + } // replace err = os.Rename(nv, ov) if err != nil {