diff --git a/cmds/portmaster-start/main.go b/cmds/portmaster-start/main.go index 46df0c89..d6b48f1e 100644 --- a/cmds/portmaster-start/main.go +++ b/cmds/portmaster-start/main.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "log" "os" "os/signal" @@ -14,8 +13,6 @@ import ( "github.com/safing/portmaster/updates/helper" - "github.com/tidwall/gjson" - "github.com/safing/portbase/dataroot" "github.com/safing/portbase/info" portlog "github.com/safing/portbase/log" @@ -188,11 +185,11 @@ func ensureLoggingDir() error { } func updateRegistryIndex(mustLoadIndex bool) error { - // Get release channel from config. - releaseChannel := getReleaseChannel(dataRoot) - // Set indexes based on the release channel. - helper.SetIndexes(registry, releaseChannel) + warning := helper.SetIndexes(registry, "", false) + if warning != nil { + log.Printf("WARNING: %s\n", warning) + } // Load indexes from disk or network, if needed and desired. err := registry.LoadIndexes(context.Background()) @@ -232,26 +229,3 @@ func detectInstallationDir() string { return parent } - -func getReleaseChannel(dataRoot *utils.DirStructure) string { - configData, err := ioutil.ReadFile(filepath.Join(dataRoot.Path, "config.json")) - if err != nil { - if !os.IsNotExist(err) { - log.Printf("WARNING: failed to read config.json to get release channel: %s\n", err) - } - return helper.ReleaseChannelStable - } - - // Get release channel from config, validate and return it. - channel := gjson.GetBytes(configData, helper.ReleaseChannelJSONKey).String() - switch channel { - case helper.ReleaseChannelStable, - helper.ReleaseChannelBeta, - helper.ReleaseChannelStaging, - helper.ReleaseChannelSupport: - return channel - default: - log.Printf("WARNING: config.json has unknown release channel %q, falling back to stable channel\n", channel) - return helper.ReleaseChannelStable - } -} diff --git a/updates/config.go b/updates/config.go index f4aec988..62e3e124 100644 --- a/updates/config.go +++ b/updates/config.go @@ -123,7 +123,10 @@ func updateRegistryConfig(_ context.Context, _ interface{}) error { if releaseChannel() != previousReleaseChannel { previousReleaseChannel = releaseChannel() - helper.SetIndexes(registry, releaseChannel()) + warning := helper.SetIndexes(registry, releaseChannel(), true) + if warning != nil { + log.Warningf("updates: %s", warning) + } changed = true } diff --git a/updates/helper/indexes.go b/updates/helper/indexes.go index 69b73d12..69443604 100644 --- a/updates/helper/indexes.go +++ b/updates/helper/indexes.go @@ -1,6 +1,10 @@ package helper import ( + "fmt" + "os" + "path/filepath" + "github.com/safing/portbase/updater" ) @@ -15,7 +19,7 @@ const ( // SetIndexes sets the update registry indexes and also configures the registry // to use pre-releases based on the channel. -func SetIndexes(registry *updater.ResourceRegistry, releaseChannel string) { +func SetIndexes(registry *updater.ResourceRegistry, releaseChannel string, deleteUnusedIndexes bool) (warning error) { usePreReleases := false // Be reminded that the order is important, as indexes added later will @@ -30,29 +34,50 @@ func SetIndexes(registry *updater.ResourceRegistry, releaseChannel string) { }) // Add beta index if in beta or staging channel. + indexPath := ReleaseChannelBeta + ".json" if releaseChannel == ReleaseChannelBeta || - releaseChannel == ReleaseChannelStaging { + releaseChannel == ReleaseChannelStaging || + (releaseChannel == "" && indexExists(registry, indexPath)) { registry.AddIndex(updater.Index{ - Path: ReleaseChannelBeta + ".json", + Path: indexPath, PreRelease: true, }) usePreReleases = true + } else if deleteUnusedIndexes { + err := deleteIndex(registry, indexPath) + if err != nil { + warning = fmt.Errorf("failed to delete unused index %s: %w", indexPath, err) + } } // Add staging index if in staging channel. - if releaseChannel == ReleaseChannelStaging { + indexPath = ReleaseChannelStaging + ".json" + if releaseChannel == ReleaseChannelStaging || + (releaseChannel == "" && indexExists(registry, indexPath)) { registry.AddIndex(updater.Index{ - Path: ReleaseChannelStaging + ".json", + Path: indexPath, PreRelease: true, }) usePreReleases = true + } else if deleteUnusedIndexes { + err := deleteIndex(registry, indexPath) + if err != nil { + warning = fmt.Errorf("failed to delete unused index %s: %w", indexPath, err) + } } // Add support index if in support channel. - if releaseChannel == ReleaseChannelSupport { + indexPath = ReleaseChannelSupport + ".json" + if releaseChannel == ReleaseChannelSupport || + (releaseChannel == "" && indexExists(registry, indexPath)) { registry.AddIndex(updater.Index{ - Path: ReleaseChannelSupport + ".json", + Path: indexPath, }) + } else if deleteUnusedIndexes { + err := deleteIndex(registry, indexPath) + if err != nil { + warning = fmt.Errorf("failed to delete unused index %s: %w", indexPath, err) + } } // Add the intel index last, as it updates the fastest and should not be @@ -64,4 +89,19 @@ func SetIndexes(registry *updater.ResourceRegistry, releaseChannel string) { // Set pre-release usage. registry.SetUsePreReleases(usePreReleases) + + return +} + +func indexExists(registry *updater.ResourceRegistry, indexPath string) bool { + _, err := os.Stat(filepath.Join(registry.StorageDir().Path, indexPath)) + return err == nil +} + +func deleteIndex(registry *updater.ResourceRegistry, indexPath string) error { + err := os.Remove(filepath.Join(registry.StorageDir().Path, indexPath)) + if err != nil && !os.IsNotExist(err) { + return err + } + return nil } diff --git a/updates/main.go b/updates/main.go index f9f26344..46e9ec92 100644 --- a/updates/main.go +++ b/updates/main.go @@ -114,7 +114,10 @@ func start() error { } // Set indexes based on the release channel. - helper.SetIndexes(registry, initialReleaseChannel) + warning := helper.SetIndexes(registry, initialReleaseChannel, true) + if warning != nil { + log.Warningf("updates: %s", warning) + } err = registry.LoadIndexes(module.Ctx) if err != nil {