-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathupdate.go
218 lines (197 loc) · 4.99 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2015, Daniel Martí <[email protected]>
// See LICENSE for licensing information
package main
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"sort"
"time"
"github.com/schollz/progressbar/v3"
"mvdan.cc/fdroidcl/fdroid"
)
var cmdUpdate = &Command{
UsageLine: "update",
Short: "Update the index",
}
func init() {
cmdUpdate.Run = runUpdate
}
func runUpdate(args []string) error {
anyModified := false
for _, r := range config.Repos {
if !r.Enabled {
continue
}
if err := r.updateIndex(); err == errNotModified {
} else if err != nil {
return fmt.Errorf("could not update index: %v", err)
} else {
anyModified = true
}
}
if anyModified {
cachePath := filepath.Join(mustCache(), "cache-gob")
os.Remove(cachePath)
}
return nil
}
const jarFile = "index-v1.jar"
func (r *repo) updateIndex() error {
url := fmt.Sprintf("%s/%s", r.URL, jarFile)
return downloadEtag(url, indexPath(r.ID), nil)
}
func (r *repo) loadIndex() (*fdroid.Index, error) {
p := indexPath(r.ID)
f, err := os.Open(p)
if os.IsNotExist(err) {
return nil, fmt.Errorf("index does not exist; try 'fdroidcl update'")
} else if err != nil {
return nil, fmt.Errorf("could not open index: %v", err)
}
stat, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("could not stat index: %v", err)
}
return fdroid.LoadIndexJar(f, stat.Size(), nil)
}
func respEtag(resp *http.Response) string {
etags, e := resp.Header["Etag"]
if !e || len(etags) == 0 {
return ""
}
return etags[0]
}
var errNotModified = fmt.Errorf("not modified")
var httpClient = &http.Client{}
func downloadEtag(url, target_path string, sum []byte) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
etagPath := target_path + "-etag"
if _, err := os.Stat(target_path); err == nil {
etag, _ := os.ReadFile(etagPath)
req.Header.Add("If-None-Match", string(etag))
}
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return fmt.Errorf("%s download failed: %d %s",
url, resp.StatusCode, http.StatusText(resp.StatusCode))
}
if resp.StatusCode == http.StatusNotModified {
fmt.Printf("%s not modified\n", url)
return errNotModified
}
f, err := os.OpenFile(target_path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
return err
}
defer f.Close()
bar := progressbar.NewOptions64(
resp.ContentLength,
progressbar.OptionSetDescription(url),
progressbar.OptionSetWriter(os.Stdout),
progressbar.OptionShowBytes(true),
progressbar.OptionThrottle(50*time.Millisecond),
progressbar.OptionShowCount(),
progressbar.OptionOnCompletion(func() {
fmt.Fprint(os.Stdout, "\n")
}),
progressbar.OptionSpinnerType(14),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionUseANSICodes(runtime.GOOS != "windows"),
progressbar.OptionFullWidth(),
)
if sum == nil {
_, err := io.Copy(io.MultiWriter(f, bar), resp.Body)
if err != nil {
return err
}
} else {
hash := sha256.New()
_, err := io.Copy(io.MultiWriter(f, bar, hash), resp.Body)
if err != nil {
return err
}
got := hash.Sum(nil)
if !bytes.Equal(sum, got[:]) {
return fmt.Errorf("%s sha256 mismatch", url)
}
}
if err := os.WriteFile(etagPath, []byte(respEtag(resp)), 0o644); err != nil {
return err
}
return nil
}
func indexPath(name string) string {
return filepath.Join(mustData(), name+".jar")
}
const cacheVersion = 2
type cache struct {
Version int
Apps []fdroid.App
}
type apkPtrList []*fdroid.Apk
func (al apkPtrList) Len() int { return len(al) }
func (al apkPtrList) Swap(i, j int) { al[i], al[j] = al[j], al[i] }
func (al apkPtrList) Less(i, j int) bool { return al[i].VersCode > al[j].VersCode }
func loadIndexes() ([]fdroid.App, error) {
cachePath := filepath.Join(mustCache(), "cache-gob")
if f, err := os.Open(cachePath); err == nil {
defer f.Close()
var c cache
if err := gob.NewDecoder(f).Decode(&c); err == nil && c.Version == cacheVersion {
return c.Apps, nil
}
}
m := make(map[string]*fdroid.App)
for _, r := range config.Repos {
if !r.Enabled {
continue
}
index, err := r.loadIndex()
if err != nil {
return nil, fmt.Errorf("error while loading %s: %v", r.ID, err)
}
for i := range index.Apps {
app := index.Apps[i]
app.FdroidRepoName = r.ID
app.FdroidRepoURL = r.URL
orig, e := m[app.PackageName]
if !e {
m[app.PackageName] = &app
continue
}
apks := append(orig.Apks, app.Apks...)
// We use a stable sort so that repository order
// (priority) is preserved amongst apks with the same
// vercode on apps
sort.Stable(apkPtrList(apks))
m[app.PackageName].Apks = apks
}
}
apps := make([]fdroid.App, 0, len(m))
for _, a := range m {
apps = append(apps, *a)
}
sort.Sort(fdroid.AppList(apps))
if f, err := os.Create(cachePath); err == nil {
defer f.Close()
gob.NewEncoder(f).Encode(cache{
Version: cacheVersion,
Apps: apps,
})
}
return apps, nil
}