GODT-1244: Refactor switching stable-early and factory reset

This commit is contained in:
Alexander Bilyak
2021-11-03 16:55:25 +00:00
committed by Jakub
parent b5b477a3ce
commit 41e15db442
12 changed files with 163 additions and 106 deletions

View File

@ -15,6 +15,7 @@
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
//go:build !darwin
// +build !darwin
package versioner
@ -23,6 +24,7 @@ import (
"os"
"github.com/Masterminds/semver/v3"
"github.com/ProtonMail/proton-bridge/internal/constants"
"github.com/sirupsen/logrus"
)
@ -58,6 +60,12 @@ func (v *Versioner) RemoveOtherVersions(versionToKeep *semver.Version) error {
if version.Equal(versionToKeep) {
continue
}
if version.Equal(semver.MustParse(constants.Version)) {
if err := v.RemoveCurrentVersion(); err != nil {
logrus.WithError(err).Error("Failed to remove current app version")
}
continue
}
if err := os.RemoveAll(version.path); err != nil {
logrus.WithError(err).Error("Failed to remove old app version")
}

View File

@ -15,6 +15,9 @@
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
//go:build darwin
// +build darwin
package versioner
import "github.com/Masterminds/semver/v3"
@ -30,3 +33,9 @@ func (v *Versioner) RemoveOtherVersions(versionToKeep *semver.Version) error {
// darwin does not use the versioner; removal is a noop.
return nil
}
// RemoveOtherVersions removes current app version unless it is base installed version.
func (v *Versioner) RemoveCurrentVersion() error {
// darwin does not use the versioner; removal is a noop.
return nil
}

View File

@ -0,0 +1,52 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
//go:build linux
// +build linux
package versioner
import (
"os"
"path/filepath"
"strings"
)
func (v *Versioner) RemoveCurrentVersion() error {
// get current executable
exec, err := os.Executable()
if err != nil {
return err
}
// Check that current executtable is update package so we won't
// delete base version (that is controlled by package manager).
// Get absolute paths to ensure there is no crazy stuff there.
absExec, err := filepath.Abs(exec)
if err != nil {
return err
}
absRoot, err := filepath.Abs(v.root)
if err != nil {
return err
}
if !strings.HasPrefix(absExec, absRoot) {
return ErrNoRemoveBase
}
return os.RemoveAll(filepath.Dir(absExec))
}

View File

@ -0,0 +1,55 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ProtonMail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
//go:build windows
// +build windows
package versioner
import (
"os"
"path/filepath"
"strings"
)
func (v *Versioner) RemoveCurrentVersion() error {
// get current executable
exec, err := os.Executable()
if err != nil {
return err
}
// Check that current executtable is update package so we won't
// delete base version (that is controlled by package manager).
// Get absolute paths to ensure there is no crazy stuff there.
absExec, err := filepath.Abs(exec)
if err != nil {
return err
}
absRoot, err := filepath.Abs(v.root)
if err != nil {
return err
}
if !strings.HasPrefix(absExec, absRoot) {
return ErrNoRemoveBase
}
// It is impossible delete running executable on Windows, so instead
// we rename it. Next time launcher will start it will remove this version
// as checksum won't match.
return os.Rename(absExec, filepath.Join(filepath.Dir(absExec), "_"+filepath.Base(absExec)))
}

View File

@ -29,6 +29,7 @@ import (
var (
ErrNoVersions = errors.New("no available versions")
ErrNoExecutable = errors.New("no executable found")
ErrNoRemoveBase = errors.New("can't remove base version")
)
// Versioner manages a directory of versioned app directories.