fix(windows): make installer upgrades version aware

This commit is contained in:
cabbage committed 2026-10-27 15:17:52 +00:00
1 parent 6906ed52f2
commit ee7844cb40
14 files changed
+824 -51

No files matched your search

+5 -3
View File
@@ -70,9 +70,11 @@ const (
// Status is read-only SCM state. Query never requests service mutation rights.
type Status struct {
Installed bool
State State
Startup StartupMode
Installed bool
State State
Startup StartupMode
ExecutablePath string
ConfigPath string
}
type Command uint8
@@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"syscall"
"time"
@@ -62,7 +63,21 @@ func queryNative() (Status, error) {
} else if configuration.StartType == mgr.StartManual {
startup = StartupManual
}
return Status{Installed: true, State: state, Startup: startup}, nil
result := Status{Installed: true, State: state, Startup: startup}
if args, err := windows.DecomposeCommandLine(configuration.BinaryPathName); err == nil && len(args) > 0 {
result.ExecutablePath = args[0]
for i := 1; i < len(args); i++ {
if args[i] == "--config" && i+1 < len(args) {
result.ConfigPath = args[i+1]
break
}
if strings.HasPrefix(args[i], "--config=") {
result.ConfigPath = strings.TrimPrefix(args[i], "--config=")
break
}
}
}
return result, nil
}
func installNative(spec InstallSpec) error {
@@ -111,7 +126,7 @@ func installNative(spec InstallSpec) error {
}
}
defer service.Close()
if err := service.Start(); err != nil && !errors.Is(err, windows.ERROR_SERVICE_ALREADY_RUNNING) {
if err := ensureServiceRunning(service, 30*time.Second); err != nil {
return fmt.Errorf("start %s service: %w", Name, err)
}
if err := registerTray(spec); err != nil {
@@ -224,10 +239,44 @@ func startNative() error {
return err
}
defer service.Close()
if err := service.Start(); err != nil && !errors.Is(err, windows.ERROR_SERVICE_ALREADY_RUNNING) {
return err
return ensureServiceRunning(service, 30*time.Second)
}
func ensureServiceRunning(service *mgr.Service, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
issued := false
for {
status, err := service.Query()
if err != nil {
return err
}
switch status.State {
case svc.Running:
return nil
case svc.Stopped:
if issued {
return errors.New("RVBox service stopped while starting; inspect service-startup.log")
}
if err := service.Start(); err != nil && !errors.Is(err, windows.ERROR_SERVICE_ALREADY_RUNNING) {
return err
}
issued = true
case svc.Paused:
if !issued {
if _, err := service.Control(svc.Continue); err != nil {
return err
}
issued = true
}
case svc.StartPending, svc.StopPending, svc.ContinuePending, svc.PausePending:
default:
return errors.New("RVBox service is in an unsupported startup state")
}
if !time.Now().Before(deadline) {
return errors.New("timed out waiting for RVBox service to run")
}
time.Sleep(100 * time.Millisecond)
}
return nil
}
func stopNative(timeoutSeconds uint32) error {
@@ -21,4 +21,6 @@ func Run(context.Context, string, io.Writer) error { return ErrUnsupported }
func WaitReady(context.Context) error { return ErrUnsupported }
func IsReady() bool { return false }
func CloseCurrent(context.Context) error { return ErrUnsupported }
+50 -23
View File
@@ -493,25 +493,11 @@ func (window *trayWindow) applyResponse() {
// session. It confirms Shell has accepted an actual icon, not merely that a
// child process was spawned. It never opens daemon config or state.
func WaitReady(ctx context.Context) error {
class, _ := winapi.UTF16PtrFromString("RVBoxTrayWindowV1")
type identifier struct {
Size uint32
HWnd uintptr
UID uint32
GUID [16]byte
}
type rectangle struct{ Left, Top, Right, Bottom int32 }
timer := time.NewTicker(50 * time.Millisecond)
defer timer.Stop()
for {
hwnd, _, _ := procFindWindow.Call(uintptr(unsafe.Pointer(class)), 0)
if hwnd != 0 {
id := identifier{Size: uint32(unsafe.Sizeof(identifier{})), HWnd: hwnd, UID: trayIconID}
var rect rectangle
result, _, _ := procShellIconRect.Call(uintptr(unsafe.Pointer(&id)), uintptr(unsafe.Pointer(&rect)))
if iconQuerySucceeded(result) {
return nil
}
if IsReady() {
return nil
}
select {
case <-ctx.Done():
@@ -521,23 +507,64 @@ func WaitReady(ctx context.Context) error {
}
}
// IsReady probes this desktop once; it neither contacts SCM nor starts a tray.
func IsReady() bool {
class, _ := winapi.UTF16PtrFromString("RVBoxTrayWindowV1")
type identifier struct {
Size uint32
HWnd uintptr
UID uint32
GUID [16]byte
}
type rectangle struct{ Left, Top, Right, Bottom int32 }
hwnd, _, _ := procFindWindow.Call(uintptr(unsafe.Pointer(class)), 0)
if hwnd != 0 {
id := identifier{Size: uint32(unsafe.Sizeof(identifier{})), HWnd: hwnd, UID: trayIconID}
var rect rectangle
result, _, _ := procShellIconRect.Call(uintptr(unsafe.Pointer(&id)), uintptr(unsafe.Pointer(&rect)))
if iconQuerySucceeded(result) {
return true
}
}
return false
}
// CloseCurrent lets an elevated installer update the canonical executable
// without leaving this desktop's old frontend holding the image open. It is
// deliberately a same-session window message, not service session injection.
// a same-session window message followed by waiting for process exit.
func CloseCurrent(ctx context.Context) error {
class, _ := winapi.UTF16PtrFromString("RVBoxTrayWindowV1")
timer := time.NewTicker(50 * time.Millisecond)
defer timer.Stop()
for {
if err := ctx.Err(); err != nil {
return err
}
hwnd, _, _ := procFindWindow.Call(uintptr(unsafe.Pointer(class)), 0)
if hwnd == 0 {
return nil
}
var pid uint32
_, _, _ = user32Tray.NewProc("GetWindowThreadProcessId").Call(hwnd, uintptr(unsafe.Pointer(&pid)))
process, err := winapi.OpenProcess(winapi.SYNCHRONIZE, false, pid)
if err != nil {
if errors.Is(err, winapi.ERROR_INVALID_PARAMETER) {
continue
}
return fmt.Errorf("wait for old tray process: %w", err)
}
_, _, _ = procPostMessage.Call(hwnd, wmClose, 0, 0)
select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
for {
result, waitErr := winapi.WaitForSingleObject(process, 50)
if waitErr != nil || result == winapi.WAIT_OBJECT_0 {
_ = winapi.CloseHandle(process)
if waitErr != nil {
return waitErr
}
break
}
if ctx.Err() != nil {
_ = winapi.CloseHandle(process)
return ctx.Err()
}
}
}
}