- Add stable, alpha, and rolling dev release channel resolution in internal/version (including non-monotonic dev commit SHA detection) - Persist app_update_channel in config and expose App.SetAppUpdateChannel - Preserve previous executables (.old*) and downloaded update installers on startup (and avoid overwriting existing .old backups during swap) when alpha or dev channel is selected
165 lines
5.2 KiB
Go
165 lines
5.2 KiB
Go
// Package selfupdate swaps a downloaded Reims-Go executable in for the one
|
|
// that is running, without a separate updater process.
|
|
//
|
|
// Windows won't overwrite a running .exe but will rename it, so Swap moves the
|
|
// running file aside to "<exe>.old" and puts the new one at the target path
|
|
// (Target: the original path, or the new release's file name when the running
|
|
// file still has a release name); the running process keeps its mapped image.
|
|
// The caller then launches the new file with --wait-pid, which waits for this
|
|
// process to exit before the window takes the single-instance lock, and quits.
|
|
// The next start removes the ".old" file (Cleanup).
|
|
package selfupdate
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// releaseName matches the file name release assets carry, such as
|
|
// "reims-go-v0.2.6-windows-amd64.exe", "reims-go-v0.2.9-alpha.1-windows-amd64.exe",
|
|
// or "reims-go-v0.2.9-dev-66da194-windows-amd64.exe".
|
|
var releaseName = regexp.MustCompile(`(?i)^reims-go-v\d+\.\d+\.\d+(?:-[0-9a-z.-]+)?-windows-amd64\.exe$`)
|
|
|
|
// Target is where the update goes. A running file that still has a release
|
|
// name ("reims-go-v0.2.5-windows-amd64.exe") takes the new release's name, so
|
|
// the name keeps telling the version; any other name (say "reims-go.exe") is
|
|
// kept, so shortcuts to it keep working.
|
|
func Target(exe, assetName string) string {
|
|
if releaseName.MatchString(filepath.Base(exe)) && releaseName.MatchString(assetName) {
|
|
return filepath.Join(filepath.Dir(exe), assetName)
|
|
}
|
|
return exe
|
|
}
|
|
|
|
// ErrDevBuild is returned by Supported for `wails dev` builds, whose
|
|
// executable is the developer's build output, not an installed copy.
|
|
var ErrDevBuild = errors.New("in-app update is disabled in development builds")
|
|
|
|
// Supported reports whether this build can update itself.
|
|
func Supported() error {
|
|
if devBuild {
|
|
return ErrDevBuild
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
return fmt.Errorf("in-app update is only supported on Windows")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Executable returns the running executable's real path.
|
|
func Executable() (string, error) {
|
|
exe, err := os.Executable()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if real, err := filepath.EvalSymlinks(exe); err == nil {
|
|
exe = real
|
|
}
|
|
return exe, nil
|
|
}
|
|
|
|
// Writable reports whether files can be created next to the executable; an
|
|
// install under Program Files without elevation can't.
|
|
func Writable(dir string) error {
|
|
f, err := os.CreateTemp(dir, ".reims-go-update-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
name := f.Name()
|
|
f.Close()
|
|
return os.Remove(name)
|
|
}
|
|
|
|
// Swap installs newExe at target (see Target; usually exe itself) and moves
|
|
// the running exe aside to "<exe>.old" (or a timestamped name when an earlier
|
|
// one is still locked). A file already at target is replaced. On any failure
|
|
// the original executable is left in place.
|
|
func Swap(exe, target, newExe string) error {
|
|
return SwapWithOptions(exe, target, newExe, false)
|
|
}
|
|
|
|
// SwapWithOptions behaves like Swap, except when keepOld is true it never
|
|
// deletes an existing "<exe>.old" backup (falling back to a timestamped
|
|
// ".old-<ns>" name instead).
|
|
func SwapWithOptions(exe, target, newExe string, keepOld bool) error {
|
|
staged := target + ".new"
|
|
if err := copyFile(newExe, staged); err != nil {
|
|
os.Remove(staged)
|
|
return fmt.Errorf("stage new executable: %w", err)
|
|
}
|
|
old := exe + ".old"
|
|
if keepOld {
|
|
if _, err := os.Stat(old); err == nil {
|
|
old = exe + ".old-" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
}
|
|
} else if err := os.Remove(old); err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
old = exe + ".old-" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
}
|
|
if err := os.Rename(exe, old); err != nil {
|
|
os.Remove(staged)
|
|
return fmt.Errorf("move running executable aside: %w", err)
|
|
}
|
|
if err := os.Rename(staged, target); err != nil {
|
|
if rerr := os.Rename(old, exe); rerr != nil {
|
|
return fmt.Errorf("install new executable: %w (restoring the old one also failed: %v; it is at %s)", err, rerr, old)
|
|
}
|
|
os.Remove(staged)
|
|
return fmt.Errorf("install new executable: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CleanupStaging removes only an aborted "<exe>.new" staging file, preserving
|
|
// any previous "<exe>.old*" backups (used by alpha/dev channels).
|
|
func CleanupStaging(exe string) {
|
|
_ = os.Remove(exe + ".new")
|
|
}
|
|
|
|
// Cleanup removes what Swap leaves behind next to exe, including the moved
|
|
// aside file of an earlier release name. Files still locked by an exiting
|
|
// process are left for the next start.
|
|
func Cleanup(exe string) {
|
|
_ = os.Remove(exe + ".new")
|
|
dir := filepath.Dir(exe)
|
|
patterns := []string{exe + ".old", exe + ".old-*", filepath.Join(dir, "reims-go-v*-windows-amd64.exe.old*")}
|
|
for _, pat := range patterns {
|
|
matches, err := filepath.Glob(pat)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, p := range matches {
|
|
if p != exe {
|
|
_ = os.Remove(p)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func copyFile(src, dst string) error {
|
|
in, err := os.Open(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer in.Close()
|
|
out, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := io.Copy(out, in); err != nil {
|
|
out.Close()
|
|
return err
|
|
}
|
|
if err := out.Sync(); err != nil {
|
|
out.Close()
|
|
return err
|
|
}
|
|
return out.Close()
|
|
}
|