From 6cb233473aabc9b20579a8327a19e9a94fba6b35 Mon Sep 17 00:00:00 2001 From: Romain LE JEUNE Date: Thu, 2 Nov 2023 10:43:55 +0100 Subject: [PATCH] fix(GODT-3094): Clean up old update files on bridge startup. --- internal/app/app.go | 3 +++ internal/app/bridge.go | 2 +- internal/bridge/mocks.go | 4 ++++ internal/bridge/types.go | 1 + internal/bridge/updates.go | 6 ++++++ internal/updater/updater.go | 11 +++++++++-- 6 files changed, 24 insertions(+), 3 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 3c1d3d65..1b9657a7 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -276,6 +276,9 @@ func run(c *cli.Context) error { b.PushError(bridge.ErrVaultCorrupt) } + // Remove old updates files + b.RemoveOldUpdates() + // Start telemetry heartbeat process b.StartHeartbeat(b) diff --git a/internal/app/bridge.go b/internal/app/bridge.go index 0600e39c..d3cb79cf 100644 --- a/internal/app/bridge.go +++ b/internal/app/bridge.go @@ -155,7 +155,7 @@ func newUpdater(locations *locations.Locations) (*updater.Updater, error) { } return updater.NewUpdater( - updater.NewInstaller(versioner.New(updatesDir)), + versioner.New(updatesDir), verifier, constants.UpdateName, runtime.GOOS, diff --git a/internal/bridge/mocks.go b/internal/bridge/mocks.go index 4412e1f2..f0818752 100644 --- a/internal/bridge/mocks.go +++ b/internal/bridge/mocks.go @@ -154,3 +154,7 @@ func (testUpdater *TestUpdater) GetVersionInfo(_ context.Context, _ updater.Down func (testUpdater *TestUpdater) InstallUpdate(_ context.Context, _ updater.Downloader, _ updater.VersionInfo) error { return nil } + +func (testUpdater *TestUpdater) RemoveOldUpdates() error { + return nil +} diff --git a/internal/bridge/types.go b/internal/bridge/types.go index 5373257d..4ef68efa 100644 --- a/internal/bridge/types.go +++ b/internal/bridge/types.go @@ -53,4 +53,5 @@ type Autostarter interface { type Updater interface { GetVersionInfo(context.Context, updater.Downloader, updater.Channel) (updater.VersionInfo, error) InstallUpdate(context.Context, updater.Downloader, updater.VersionInfo) error + RemoveOldUpdates() error } diff --git a/internal/bridge/updates.go b/internal/bridge/updates.go index 4208ce5c..5e0f60e9 100644 --- a/internal/bridge/updates.go +++ b/internal/bridge/updates.go @@ -139,3 +139,9 @@ func (bridge *Bridge) installUpdate(ctx context.Context, job installJob) { } }, bridge.newVersionLock) } + +func (bridge *Bridge) RemoveOldUpdates() { + if err := bridge.updater.RemoveOldUpdates(); err != nil { + logrus.WithError(err).Error("Remove old updates fails") + } +} diff --git a/internal/updater/updater.go b/internal/updater/updater.go index 65a8d212..10f7cf66 100644 --- a/internal/updater/updater.go +++ b/internal/updater/updater.go @@ -26,6 +26,7 @@ import ( "github.com/Masterminds/semver/v3" "github.com/ProtonMail/gopenpgp/v2/crypto" + "github.com/ProtonMail/proton-bridge/v3/internal/versioner" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -46,15 +47,17 @@ type Installer interface { } type Updater struct { + versioner *versioner.Versioner installer Installer verifier *crypto.KeyRing product string platform string } -func NewUpdater(installer Installer, verifier *crypto.KeyRing, product, platform string) *Updater { +func NewUpdater(ver *versioner.Versioner, verifier *crypto.KeyRing, product, platform string) *Updater { return &Updater{ - installer: installer, + versioner: ver, + installer: NewInstaller(ver), verifier: verifier, product: product, platform: platform, @@ -109,6 +112,10 @@ func (u *Updater) InstallUpdate(ctx context.Context, downloader Downloader, upda return nil } +func (u *Updater) RemoveOldUpdates() error { + return u.versioner.RemoveOldVersions() +} + // getVersionFileURL returns the URL of the version file. // For example: // - https://protonmail.com/download/bridge/version_linux.json