From 7043f051441b18d7e7f810d3f409e68fd55150f6 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 22 May 2019 16:02:31 +0200 Subject: [PATCH] Upgrade pmctl to only do a self upgrade when the update is already available --- pmctl/upgrade.go | 2 +- updates/get.go | 26 +++++++++++++++++++++++--- updates/updater.go | 7 ++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/pmctl/upgrade.go b/pmctl/upgrade.go index 2d2ff5cf..cba12fb8 100644 --- a/pmctl/upgrade.go +++ b/pmctl/upgrade.go @@ -13,7 +13,7 @@ import ( func checkForUpgrade() (update *updates.File) { info := info.GetInfo() - file, err := getFile("pmctl/pmctl") + file, err := updates.GetLocalPlatformFile("pmctl/pmctl") if err != nil { return nil } diff --git a/updates/get.go b/updates/get.go index d6e0be9f..9f8385f4 100644 --- a/updates/get.go +++ b/updates/get.go @@ -13,6 +13,7 @@ import ( var ( ErrNotFound = errors.New("the requested file could not be found") + ErrNotAvailableLocally = errors.New("the requested file is not available locally") ) // GetPlatformFile returns the latest platform specific file identified by the given identifier. @@ -21,13 +22,28 @@ func GetPlatformFile(identifier string) (*File, error) { // From https://golang.org/pkg/runtime/#GOARCH // GOOS is the running program's operating system target: one of darwin, freebsd, linux, and so on. // GOARCH is the running program's architecture target: one of 386, amd64, arm, s390x, and so on. - return loadOrFetchFile(identifier) + return loadOrFetchFile(identifier, true) +} + +// GetLocalPlatformFile returns the latest platform specific file identified by the given identifier, that is available locally. +func GetLocalPlatformFile(identifier string) (*File, error) { + identifier = path.Join(fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH), identifier) + // From https://golang.org/pkg/runtime/#GOARCH + // GOOS is the running program's operating system target: one of darwin, freebsd, linux, and so on. + // GOARCH is the running program's architecture target: one of 386, amd64, arm, s390x, and so on. + return loadOrFetchFile(identifier, false) } // GetFile returns the latest generic file identified by the given identifier. func GetFile(identifier string) (*File, error) { identifier = path.Join("all", identifier) - return loadOrFetchFile(identifier) + return loadOrFetchFile(identifier, true) +} + +// GetLocalFile returns the latest generic file identified by the given identifier, that is available locally. +func GetLocalFile(identifier string) (*File, error) { + identifier = path.Join("all", identifier) + return loadOrFetchFile(identifier, false) } func getLatestFilePath(identifier string) (versionedFilePath, version string, stable bool, ok bool) { @@ -49,7 +65,7 @@ func getLatestFilePath(identifier string) (versionedFilePath, version string, st return GetVersionedPath(identifier, version), version, false, true } -func loadOrFetchFile(identifier string) (*File, error) { +func loadOrFetchFile(identifier string, fetch bool) (*File, error) { versionedFilePath, version, stable, ok := getLatestFilePath(identifier) if !ok { // TODO: if in development mode, search updates dir for sideloaded apps @@ -70,6 +86,10 @@ func loadOrFetchFile(identifier string) (*File, error) { return nil, fmt.Errorf("could not prepare tmp directory for download: %s", err) } + if (!fetch) { + return nil, ErrNotAvailableLocally + } + // download file log.Tracef("updates: starting download of %s", versionedFilePath) for tries := 0; tries < 5; tries++ { diff --git a/updates/updater.go b/updates/updater.go index a6a86710..250719a6 100644 --- a/updates/updater.go +++ b/updates/updater.go @@ -23,9 +23,14 @@ func updater() { func CheckForUpdates() error { + // be sure that pmctl is part of the "used" updates + _, err := GetLocalFile("pmctl/pmctl") + if err != nil { + log.Errorf("updates: failed to mark pmctl/pmctl as used to ensure updates: %s", err) + } + // download new index var data []byte - var err error for tries := 0; tries < 3; tries++ { data, err = fetchData("stable.json", tries) if err == nil {