Skip to content
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

}