diff --git a/core/install.go b/core/install.go index 3c0dfb3..21823d4 100644 --- a/core/install.go +++ b/core/install.go @@ -32,34 +32,19 @@ func install() { gLog.Println(LvERROR, "cd error:", err) return } - uninstall() // save config file 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 - } - - 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 - } - - _, errFiles = io.Copy(dst, src) - if errFiles != nil { - gLog.Printf(LvERROR, "io.Copy error:%s", errFiles) - return + 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) + if errFiles != nil { + gLog.Println(LvERROR, errFiles) + } } - src.Close() - dst.Close() // install system service gLog.Println(LvINFO, "targetPath:", targetPath) @@ -77,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) + 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 {