Browse Source

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.
pull/20/head
Daniel Martí 10 years ago
parent
commit
e68eb4bc7c
  1. 33
      cmd/fdroidcl/update.go
  2. 12
      index.go

33
cmd/fdroidcl/update.go

@ -42,8 +42,8 @@ func runUpdate(args []string) {
} }
} }
if anyModified { if anyModified {
cache := filepath.Join(mustData(), "cache-gob") cachePath := filepath.Join(mustData(), "cache-gob")
os.Remove(cache) os.Remove(cachePath)
} }
} }
@ -143,12 +143,20 @@ func indexPath(name string) string {
return filepath.Join(mustData(), name+".jar") return filepath.Join(mustData(), name+".jar")
} }
func mustLoadIndexes() (apps []fdroidcl.App) { const cacheVersion = 1
cache := filepath.Join(mustData(), "cache-gob")
if f, err := os.Open(cache); err == nil { 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() defer f.Close()
if err := gob.NewDecoder(f).Decode(&apps); err == nil { var c cache
return if err := gob.NewDecoder(f).Decode(&c); err == nil && c.Version == cacheVersion {
return c.Apps
} }
} }
m := make(map[string]*fdroidcl.App) m := make(map[string]*fdroidcl.App)
@ -175,14 +183,17 @@ func mustLoadIndexes() (apps []fdroidcl.App) {
m[app.ID].Apks = apks m[app.ID].Apks = apks
} }
} }
apps = make([]fdroidcl.App, 0, len(m)) apps := make([]fdroidcl.App, 0, len(m))
for _, a := range m { for _, a := range m {
apps = append(apps, *a) apps = append(apps, *a)
} }
sort.Sort(fdroidcl.AppList(apps)) sort.Sort(fdroidcl.AppList(apps))
if f, err := os.Create(cache); err == nil { if f, err := os.Create(cachePath); err == nil {
defer f.Close() defer f.Close()
gob.NewEncoder(f).Encode(apps) gob.NewEncoder(f).Encode(cache{
Version: cacheVersion,
Apps: apps,
})
} }
return return apps
} }

12
index.go

@ -86,7 +86,7 @@ func (a *App) IconURLForDensity(density IconDensity) string {
if len(a.Apks) == 0 { if len(a.Apks) == 0 {
return "" 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) getIconsDir(density), a.Icon)
} }
@ -195,16 +195,16 @@ type Apk struct {
Feats CommaList `xml:"features"` Feats CommaList `xml:"features"`
Hash HexHash `xml:"hash"` Hash HexHash `xml:"hash"`
AppID string `xml:"-"` AppID string `xml:"-"`
repo *Repo `xml:"-"` repoURL string `xml:"-"`
} }
func (a *Apk) URL() string { 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 { 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 { func (a *Apk) IsCompatibleABI(ABIs []string) bool {
@ -260,7 +260,7 @@ func LoadIndexXML(r io.Reader) (*Index, error) {
for j := range app.Apks { for j := range app.Apks {
apk := &app.Apks[j] apk := &app.Apks[j]
apk.AppID = app.ID apk.AppID = app.ID
apk.repo = &index.Repo apk.repoURL = index.Repo.URL
} }
} }
return &index, nil return &index, nil

Loading…
Cancel
Save