From c63962f66dacbeff1eb3aee48564e6796c1fe2ba Mon Sep 17 00:00:00 2001 From: W192547975 Date: Sat, 25 Nov 2023 21:27:49 +0800 Subject: [PATCH 1/4] Install Fixbug --- core/install.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/core/install.go b/core/install.go index 3c0dfb3..01fc30f 100644 --- a/core/install.go +++ b/core/install.go @@ -38,28 +38,32 @@ func install() { parseParams("install") targetPath := filepath.Join(defaultInstallPath, defaultBinName) d := daemon{} - // copy files - binPath, _ := os.Executable() - src, errFiles := os.Open(binPath) // can not use args[0], on Windows call openp2p is ok(=openp2p.exe) - if errFiles != nil { - gLog.Printf(LvERROR, "os.OpenFile %s error:%s", os.Args[0], errFiles) - return - } + if targetPath != binPath { + // copy files + src, errFiles := os.Open(binPath) // can not use args[0], on Windows call openp2p is ok(=openp2p.exe) + if errFiles != nil { + gLog.Printf(LvERROR, "os.Open %s error:%s", os.Args[0], errFiles) + return + } - dst, errFiles := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775) - if errFiles != nil { - gLog.Printf(LvERROR, "os.OpenFile %s error:%s", targetPath, errFiles) - return - } + dst, errFiles := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775) + if errFiles != nil { + gLog.Printf(LvERROR, "os.OpenFile %s error:%s", targetPath, errFiles) + src.Close() + return + } - _, errFiles = io.Copy(dst, src) - if errFiles != nil { - gLog.Printf(LvERROR, "io.Copy error:%s", errFiles) - return + _, errFiles = io.Copy(dst, src) + if errFiles != nil { + gLog.Printf(LvERROR, "io.Copy error:%s", errFiles) + src.Close() + dst.Close() + return + } + src.Close() + dst.Close() } - src.Close() - dst.Close() // install system service gLog.Println(LvINFO, "targetPath:", targetPath) From 585fb4bdfc8fad395cf180c01a42ec473d04653b Mon Sep 17 00:00:00 2001 From: W192547975 <138874985+W192547975@users.noreply.github.com> Date: Fri, 1 Dec 2023 21:44:01 +0800 Subject: [PATCH 2/4] Update install.go Fix bug --- core/install.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/install.go b/core/install.go index 01fc30f..05407eb 100644 --- a/core/install.go +++ b/core/install.go @@ -32,7 +32,6 @@ func install() { gLog.Println(LvERROR, "cd error:", err) return } - uninstall() // save config file parseParams("install") @@ -43,7 +42,7 @@ func install() { // copy files src, errFiles := os.Open(binPath) // can not use args[0], on Windows call openp2p is ok(=openp2p.exe) if errFiles != nil { - gLog.Printf(LvERROR, "os.Open %s error:%s", os.Args[0], errFiles) + gLog.Printf(LvERROR, "os.Open %s error:%s", binPath, errFiles) return } From 3102089590c8f672fb687d529528aa91a5097edb Mon Sep 17 00:00:00 2001 From: W192547975 <138874985+W192547975@users.noreply.github.com> Date: Sat, 30 Dec 2023 16:09:01 +0800 Subject: [PATCH 3/4] Update install.go New function copyFile which use fmt.Errorf --- core/install.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/core/install.go b/core/install.go index 05407eb..5ea0409 100644 --- a/core/install.go +++ b/core/install.go @@ -40,28 +40,10 @@ func install() { binPath, _ := os.Executable() if targetPath != binPath { // copy files - src, errFiles := os.Open(binPath) // can not use args[0], on Windows call openp2p is ok(=openp2p.exe) + errFiles := copyFile(targetPath, binPath) if errFiles != nil { - gLog.Printf(LvERROR, "os.Open %s error:%s", binPath, errFiles) - return + gLog.Println(LvERROR, errFiles) } - - dst, errFiles := os.OpenFile(targetPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775) - if errFiles != nil { - gLog.Printf(LvERROR, "os.OpenFile %s error:%s", targetPath, errFiles) - src.Close() - return - } - - _, errFiles = io.Copy(dst, src) - if errFiles != nil { - gLog.Printf(LvERROR, "io.Copy error:%s", errFiles) - src.Close() - dst.Close() - return - } - src.Close() - dst.Close() } // install system service @@ -80,6 +62,23 @@ func install() { gLog.Println(LvINFO, "Visit WebUI on https://console.openp2p.cn") } +func copyFile(dst, src string) error { + srcF, err := os.Open(src) // can not use args[0], on Windows call openp2p is ok(=openp2p.exe) + if err != nil { + return fmt.Errorf("os.Open %s error: %w", src, err) + } + defer srcF.Close() + dstF, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0775) + if err != nil { + return fmt.Errorf("os.OpenFile %s error: %w", dst, err) + } + defer dstF.Close() + _, err = io.Copy(dstF, srcF) + if err != nil { + return fmt.Errorf("io.Copy error: %w", err) + } +} + func installByFilename() { params := strings.Split(filepath.Base(os.Args[0]), "-") if len(params) < 4 { From 7f737bd925d7ebe0135ad97ac87cb532684e3497 Mon Sep 17 00:00:00 2001 From: W192547975 <138874985+W192547975@users.noreply.github.com> Date: Sat, 30 Dec 2023 16:10:17 +0800 Subject: [PATCH 4/4] Update install.go --- core/install.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/install.go b/core/install.go index 5ea0409..21823d4 100644 --- a/core/install.go +++ b/core/install.go @@ -37,7 +37,7 @@ func install() { parseParams("install") targetPath := filepath.Join(defaultInstallPath, defaultBinName) d := daemon{} - binPath, _ := os.Executable() + binPath, _ := os.Executable() // can not use args[0], on Windows call openp2p is ok(=openp2p.exe) if targetPath != binPath { // copy files errFiles := copyFile(targetPath, binPath) @@ -63,7 +63,7 @@ func install() { } func copyFile(dst, src string) error { - srcF, err := os.Open(src) // can not use args[0], on Windows call openp2p is ok(=openp2p.exe) + srcF, err := os.Open(src) if err != nil { return fmt.Errorf("os.Open %s error: %w", src, err) }