Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Sanders committed Oct 12, 2018
1 parent e750c76 commit ad89c97
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 0 deletions.
3 changes: 3 additions & 0 deletions keybase/platform_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,6 @@ func (c context) Apply(update updater.Update, options updater.UpdateOptions, tmp

// DeepClean is called when a faulty upgrade has been detected
func (c context) DeepClean() {}

// Launch is called on windows to see if executables should be switched
func (c context) Launch() {}
3 changes: 3 additions & 0 deletions keybase/platform_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ func (c context) IsCheckCommand() bool {

// DeepClean is called when a faulty upgrade has been detected
func (c context) DeepClean() {}

// Launch is called on windows to see if executables should be switched
func (c context) Launch() {}
106 changes: 106 additions & 0 deletions keybase/platform_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,109 @@ func (c context) GetAppStatePath() string {
func (c context) IsCheckCommand() bool {
return c.isCheckCommand
}

func (c context) stopKeybaseProcesses(path string) error {

c.stopKeybase()
time.Sleep(time.Second)

// Terminate any executing processes
ospid := os.Getpid()

exes, err := filepath.Glob(filepath.Join(path, "*.exe"))
if err != nil {
c.log.Errorf("Unable to glob exe files: %s", err)
}
guiExes, err := filepath.Glob(filepath.Join(path, "Gui", "*.exe"))
if err != nil {
c.log.Errorf("Unable to glob exe files: %s", err)
} else {
exes = Append(exes, guiExes)
}

c.log.Infof("Terminating any existing programs we will be updating")
for _, program := range exes {
matcher := process.NewMatcher(program, process.PathEqual, log)
matcher.ExceptPID(ospid)
log.Infof("Terminating %s", program)
process.TerminateAll(matcher, time.Second, log)
}
}

// Launch is called on windows to see if executables should be switched.
// Sequence:
// Invoke with -launch to check for updates and launch the app
// Is there an update directory containing an upd.exe? If so, invoke
// update/upd.exe -launchupdate and exit.
// If not, perform the launch command to start the app, then exit
//
// Invoke with -launchupdate from the update directory to update
// Check again for running processes, then copy executables and the Gui directory
// from the update directory to the parent directory, then start ..\upd.exe with --launchupdated
//
// Invoke with --launchupdated from the target directory to finish the update
// and launch the app
// Delete or rename the update directory
// Launch the app and exit
func (c context) Launch() error {

path, err := osext.ExecutableFolder()
if err != nil {
return err
}

if util.FileExists(filepath.Join(path, "update")) {
pathExe, err := osext.Executable()
if err != nil {
return err
}
_, exeName := filepath.Split(pathExe)

newerUpdaterPath = filepath.Join(path, "update", exeName)
if util.FileExists(newerUpdaterPath) {
c.stopKeybaseProcesses(path)
args := []string{newerUpdaterPath, "-launchupdate"}
_, err = command.Exec(filepath.Join(path, "update", "keybaserq.exe"), args, time.Minute, c.log)
if err != nil {
c.log.Infof("Error invoking new updater with -launchupdate", err.Error())
}

return nil
}
}

return LaunchApp()
}


// We are running from the update directory and we should copy stuff up
func (c context) LaunchUpdate() error {
path, err := osext.ExecutableFolder()
if err != nil {
return err
}
if ! strings.HasSuffix(path, "update") {
return errors.New("launchupdate from non-update directory")
}
targetDir := filepath.Join(path, "..")

args := []string{filepath.Join(os.Getenv("windir"), "system32", "xcopy.exe"), "/E", "/I", "/Y", path, targetDir}
_, err = command.Exec(filepath.Join(path, "keybaserq.exe"), args, time.Minute, c.log)
if err != nil {
c.log.Infof("LaunchUpdate error", err.Error())
}

updaterName := "upd.exe"
pathExe, err := osext.Executable()
if err != nil {
c.log.Infof("osext.Executable error", err.Error())
} else {
_, updaterName = filepath.Split(pathExe)
}

_, err = command.Exec(filepath.Join(targetDir, "keybaserq.exe"), args, time.Minute, c.log)
if err != nil {
c.log.Infof("Error invoking new updater with -launchupdate", err.Error())
}

}
8 changes: 8 additions & 0 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ func run(f flags) {
} else {
ulog.Errorf("Unknown command: %s", f.command)
}
case "launch":
if runtime.GOOS == "windows" {
ctx, _ := keybase.NewUpdaterContext(f.appName, f.pathToKeybase, ulog, true)
fmt.Printf("Doing launch\n")
ctx.Launch()
} else {
ulog.Errorf("Unknown command: %s", f.command)
}
default:
ulog.Errorf("Unknown command: %s", f.command)
}
Expand Down
2 changes: 2 additions & 0 deletions update_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ func (c testUpdateCheckUI) IsCheckCommand() bool {

func (c testUpdateCheckUI) DeepClean() {}

func (c testUpdateCheckUI) Launch() {}

func TestUpdateCheckerError(t *testing.T) {
testServer := testServerForUpdateFile(t, testZipPath)
defer testServer.Close()
Expand Down
1 change: 1 addition & 0 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Context interface {
GetAppStatePath() string
IsCheckCommand() bool
DeepClean()
Launch()
}

// Config defines configuration for the Updater
Expand Down
2 changes: 2 additions & 0 deletions updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ func (c testUpdateUI) IsCheckCommand() bool {

func (c testUpdateUI) DeepClean() {}

func (c testUpdateUI) Launch() {}

type testUpdateSource struct {
testServer *httptest.Server
config Config
Expand Down

0 comments on commit ad89c97

Please sign in to comment.