Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install Fixbug #60

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 24 additions & 22 deletions core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down