From dba610683dd90a01fb098dfb2791c67fcbbfb957 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 2 Oct 2023 13:47:43 +0200 Subject: [PATCH 1/4] Exclude files that cannot be downloaded from version selection --- updater/resource.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/updater/resource.go b/updater/resource.go index 5b94516d..c9094d49 100644 --- a/updater/resource.go +++ b/updater/resource.go @@ -112,7 +112,26 @@ func (rv *ResourceVersion) EqualsVersion(version string) bool { // A version is selectable if it's not blacklisted and either already locally // available or ready to be downloaded. func (rv *ResourceVersion) isSelectable() bool { - return !rv.Blacklisted && (rv.Available || rv.resource.registry.Online) + switch { + case rv.Blacklisted: + // Should not be used. + return false + case rv.Available: + // Is available locally, use! + return true + case !rv.resource.registry.Online: + // Cannot download, because registry is set to offline. + return false + case rv.resource.Index == nil: + // Cannot download, because resource is not part of an index. + return false + case !rv.resource.Index.AutoDownload: + // Cannot download, because index may not automatically download. + return false + default: + // Is not available locally, but we are allowed to download it on request! + return true + } } // isBetaVersionNumber checks if rv is marked as a beta version by checking @@ -290,8 +309,13 @@ func (res *Resource) selectVersion() { sort.Sort(res) // export after we finish + var fallback bool defer func() { - log.Tracef("updater: selected version %s for resource %s", res.SelectedVersion, res.Identifier) + if fallback { + log.Tracef("updater: selected version %s (as fallback) for resource %s", res.SelectedVersion, res.Identifier) + } else { + log.Debugf("updater: selected version %s for resource %s", res.SelectedVersion, res.Identifier) + } if res.inUse() && res.SelectedVersion != res.ActiveVersion && // new selected version does not match previously selected version @@ -356,7 +380,7 @@ func (res *Resource) selectVersion() { // 5) Default to newest. res.SelectedVersion = res.Versions[0] - log.Warningf("updater: falling back to version %s for %s because we failed to find a selectable one", res.SelectedVersion, res.Identifier) + fallback = true } // Blacklist blacklists the specified version and selects a new version. From 7f749464dc2e7ee9437310424da1baf9329ed879 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 2 Oct 2023 13:48:15 +0200 Subject: [PATCH 2/4] Improve method naming and update status data --- updater/updating.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/updater/updating.go b/updater/updating.go index f5dc5233..dab15fac 100644 --- a/updater/updating.go +++ b/updater/updating.go @@ -47,7 +47,7 @@ func (reg *ResourceRegistry) UpdateIndexes(ctx context.Context) error { // Get pending resources and update status. pendingResourceVersions, _ := reg.GetPendingDownloads(true, false) reg.state.ReportUpdateCheck( - identifiersFromResourceVersions(pendingResourceVersions), + humanInfoFromResourceVersions(pendingResourceVersions), nil, ) @@ -183,14 +183,14 @@ func (reg *ResourceRegistry) downloadIndex(ctx context.Context, client *http.Cli } // DownloadUpdates checks if updates are available and downloads updates of used components. -func (reg *ResourceRegistry) DownloadUpdates(ctx context.Context, automaticOnly bool) error { +func (reg *ResourceRegistry) DownloadUpdates(ctx context.Context, includeManual bool) error { // Start registry operation. reg.state.StartOperation(StateDownloading) defer reg.state.EndOperation() // Get pending updates. - toUpdate, missingSigs := reg.GetPendingDownloads(!automaticOnly, true) - downloadDetailsResources := identifiersFromResourceVersions(toUpdate) + toUpdate, missingSigs := reg.GetPendingDownloads(includeManual, true) + downloadDetailsResources := humanInfoFromResourceVersions(toUpdate) reg.state.UpdateOperationDetails(&StateDownloadingDetails{ Resources: downloadDetailsResources, }) @@ -348,11 +348,11 @@ func (reg *ResourceRegistry) GetPendingDownloads(manual, auto bool) (resources, return toUpdate, missingSigs } -func identifiersFromResourceVersions(resourceVersions []*ResourceVersion) []string { +func humanInfoFromResourceVersions(resourceVersions []*ResourceVersion) []string { identifiers := make([]string, len(resourceVersions)) for i, rv := range resourceVersions { - identifiers[i] = rv.resource.Identifier + identifiers[i] = fmt.Sprintf("%s v%s", rv.resource.Identifier, rv.VersionNumber) } return identifiers From a9dffddd7e66fbef09d683a8c8ef09d246001869 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 2 Oct 2023 16:01:45 +0200 Subject: [PATCH 3/4] Improve documentation --- updater/registry.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/updater/registry.go b/updater/registry.go index b05d1ade..0db2fa58 100644 --- a/updater/registry.go +++ b/updater/registry.go @@ -43,8 +43,12 @@ type ResourceRegistry struct { // version. Even if false, a pre-release version will still be used if it is // defined as the current version by an index. UsePreReleases bool - DevMode bool - Online bool + + // DevMode specifies if a local 0.0.0 version should be always chosen, when available. + DevMode bool + + // Online specifies if resources may be downloaded if not available locally. + Online bool // StateNotifyFunc may be set to receive any changes to the registry state. // The specified function may lock the state, but may not block or take a From 85db3d9776909fffa5aa0ec1cfc884f5f2af4226 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 2 Oct 2023 16:01:55 +0200 Subject: [PATCH 4/4] Fix version selection test --- updater/resource_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/updater/resource_test.go b/updater/resource_test.go index 31775444..ceb51e9f 100644 --- a/updater/resource_test.go +++ b/updater/resource_test.go @@ -45,6 +45,8 @@ func TestVersionSelection(t *testing.T) { registry.UsePreReleases = true registry.DevMode = true registry.Online = true + res.Index = &Index{AutoDownload: true} + res.selectVersion() if res.SelectedVersion.VersionNumber != "0.0.0" { t.Errorf("selected version should be 0.0.0, not %s", res.SelectedVersion.VersionNumber)