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

fix(plugin install): copy executable by renaming instead of create file #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion commands/pluginhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func UpdateCLI(pluginPkg string, updateOption int) error {
cliExe = cliExe + ".exe"
}

err = util.Copy(filepath.Join(cliCmdPath, cliExe), exPath, false)
err = util.SwapFile(filepath.Join(cliCmdPath, cliExe), exPath)
if err != nil {
//fmt.Fprintf(os.Stderr, "Error: %v\n", osErr)
return err
Expand Down
32 changes: 31 additions & 1 deletion util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,34 @@ func DeleteFile(path string) error {
}

return nil
}
}

// SwapFile is like a copy but use a temporary file and rename it
// to allow running executable replacement.
// Thanks to https://gist.github.com/fenollp/7e31e6462b10c96aef443351bce6aea7
func SwapFile(src, dst string) error {
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
destDir := filepath.Dir(dst)
tmpFile := filepath.Join(destDir, "exe_swap")

defer DeleteFile(tmpFile)

data, err := ioutil.ReadFile(src)
if err != nil {
return err
}

if err := ioutil.WriteFile(tmpFile, data, srcInfo.Mode()); err != nil {
return err
}

if err := os.Rename(tmpFile, dst); err != nil {
return err
}

return nil

}