1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2025-09-19 20:15:59 +08:00

chore: improve backup and replace logic in updater

This commit is contained in:
xishang0128
2025-07-26 22:49:20 +09:00
parent deec7aafe5
commit 01cd7e2c0e

View File

@ -73,7 +73,7 @@ func (u *CoreUpdater) Update(currentExePath string) (err error) {
u.mu.Lock() u.mu.Lock()
defer u.mu.Unlock() defer u.mu.Unlock()
info, err := os.Stat(currentExePath) _, err = os.Stat(currentExePath)
if err != nil { if err != nil {
return fmt.Errorf("check currentExePath %q: %w", currentExePath, err) return fmt.Errorf("check currentExePath %q: %w", currentExePath, err)
} }
@ -146,8 +146,6 @@ func (u *CoreUpdater) Update(currentExePath string) (err error) {
return fmt.Errorf("backuping: %w", err) return fmt.Errorf("backuping: %w", err)
} }
_ = os.Chmod(updateExePath, info.Mode())
err = u.replace(updateExePath, currentExePath) err = u.replace(updateExePath, currentExePath)
if err != nil { if err != nil {
return fmt.Errorf("replacing: %w", err) return fmt.Errorf("replacing: %w", err)
@ -253,12 +251,19 @@ func (u *CoreUpdater) unpack(updateDir, packagePath string) error {
return nil return nil
} }
// backup makes a backup of the current executable file // backup creates a backup of the current executable file.
func (u *CoreUpdater) backup(currentExePath, backupExePath, backupDir string) (err error) { func (u *CoreUpdater) backup(currentExePath, backupExePath, backupDir string) (err error) {
log.Infoln("updater: backing up current ExecFile:%s to %s", currentExePath, backupExePath) log.Infoln("updater: backing up current ExecFile:%s to %s", currentExePath, backupExePath)
_ = os.Mkdir(backupDir, 0o755) _ = os.Mkdir(backupDir, 0o755)
err = os.Rename(currentExePath, backupExePath) // On Windows, since the running executable cannot be overwritten or deleted, it uses os.Rename to move the file to the backup path.
// On other platforms, it copies the file to the backup path, preserving the original file and its permissions.
// The backup directory is created if it does not exist.
if runtime.GOOS == "windows" {
err = os.Rename(currentExePath, backupExePath)
} else {
err = u.copyFile(currentExePath, backupExePath)
}
if err != nil { if err != nil {
return err return err
} }
@ -268,20 +273,15 @@ func (u *CoreUpdater) backup(currentExePath, backupExePath, backupDir string) (e
// replace moves the current executable with the updated one // replace moves the current executable with the updated one
func (u *CoreUpdater) replace(updateExePath, currentExePath string) error { func (u *CoreUpdater) replace(updateExePath, currentExePath string) error {
var err error
log.Infoln("replacing: %s to %s", updateExePath, currentExePath) log.Infoln("replacing: %s to %s", updateExePath, currentExePath)
if runtime.GOOS == "windows" {
// rename fails with "File in use" error // Use copyFile to retain the original file attributes
err = u.copyFile(updateExePath, currentExePath) err := u.copyFile(updateExePath, currentExePath)
} else {
err = os.Rename(updateExePath, currentExePath)
}
if err != nil { if err != nil {
return err return err
} }
log.Infoln("updater: renamed: %s to %s", updateExePath, currentExePath) log.Infoln("updater: copy: %s to %s", updateExePath, currentExePath)
return nil return nil
} }