158 lines
4.8 KiB
Go
158 lines
4.8 KiB
Go
// Package selfupdate replaces the running Windows executable by renaming it to
|
|
// "<exe>.old", moving the verified update into place, and relaunching with
|
|
// --wait-pid so the new process waits before acquiring the single-instance lock.
|
|
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()
|
|
}
|