From e68eb4bc7cffadd0442829604d5f03332bcfd928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 26 Sep 2016 21:33:43 +0100 Subject: [PATCH] fdroidcl: fix cache format crash with repo refs The new cache format didn't encode the repos properly, which lead to crashes. Since we're modifying the cache format, introduce a version and discard all previous caches (defaulting to version 0). Fixes #19. --- cmd/fdroidcl/update.go | 33 ++++++++++++++++++++++----------- index.go | 12 ++++++------ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/cmd/fdroidcl/update.go b/cmd/fdroidcl/update.go index 517c6c4..7ea0cc1 100644 --- a/cmd/fdroidcl/update.go +++ b/cmd/fdroidcl/update.go @@ -42,8 +42,8 @@ func runUpdate(args []string) { } } if anyModified { - cache := filepath.Join(mustData(), "cache-gob") - os.Remove(cache) + cachePath := filepath.Join(mustData(), "cache-gob") + os.Remove(cachePath) } } @@ -143,12 +143,20 @@ func indexPath(name string) string { return filepath.Join(mustData(), name+".jar") } -func mustLoadIndexes() (apps []fdroidcl.App) { - cache := filepath.Join(mustData(), "cache-gob") - if f, err := os.Open(cache); err == nil { +const cacheVersion = 1 + +type cache struct { + Version int + Apps []fdroidcl.App +} + +func mustLoadIndexes() []fdroidcl.App { + cachePath := filepath.Join(mustData(), "cache-gob") + if f, err := os.Open(cachePath); err == nil { defer f.Close() - if err := gob.NewDecoder(f).Decode(&apps); err == nil { - return + var c cache + if err := gob.NewDecoder(f).Decode(&c); err == nil && c.Version == cacheVersion { + return c.Apps } } m := make(map[string]*fdroidcl.App) @@ -175,14 +183,17 @@ func mustLoadIndexes() (apps []fdroidcl.App) { m[app.ID].Apks = apks } } - apps = make([]fdroidcl.App, 0, len(m)) + apps := make([]fdroidcl.App, 0, len(m)) for _, a := range m { apps = append(apps, *a) } sort.Sort(fdroidcl.AppList(apps)) - if f, err := os.Create(cache); err == nil { + if f, err := os.Create(cachePath); err == nil { defer f.Close() - gob.NewEncoder(f).Encode(apps) + gob.NewEncoder(f).Encode(cache{ + Version: cacheVersion, + Apps: apps, + }) } - return + return apps } diff --git a/index.go b/index.go index fcce635..91188f4 100644 --- a/index.go +++ b/index.go @@ -86,7 +86,7 @@ func (a *App) IconURLForDensity(density IconDensity) string { if len(a.Apks) == 0 { return "" } - return fmt.Sprintf("%s/%s/%s", a.Apks[0].repo.URL, + return fmt.Sprintf("%s/%s/%s", a.Apks[0].repoURL, getIconsDir(density), a.Icon) } @@ -195,16 +195,16 @@ type Apk struct { Feats CommaList `xml:"features"` Hash HexHash `xml:"hash"` - AppID string `xml:"-"` - repo *Repo `xml:"-"` + AppID string `xml:"-"` + repoURL string `xml:"-"` } func (a *Apk) URL() string { - return fmt.Sprintf("%s/%s", a.repo.URL, a.ApkName) + return fmt.Sprintf("%s/%s", a.repoURL, a.ApkName) } func (a *Apk) SrcURL() string { - return fmt.Sprintf("%s/%s", a.repo.URL, a.SrcName) + return fmt.Sprintf("%s/%s", a.repoURL, a.SrcName) } func (a *Apk) IsCompatibleABI(ABIs []string) bool { @@ -260,7 +260,7 @@ func LoadIndexXML(r io.Reader) (*Index, error) { for j := range app.Apks { apk := &app.Apks[j] apk.AppID = app.ID - apk.repo = &index.Repo + apk.repoURL = index.Repo.URL } } return &index, nil