-
Notifications
You must be signed in to change notification settings - Fork 0
/
apt-mirror-go.go
250 lines (216 loc) · 5.31 KB
/
apt-mirror-go.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// See https://github.com/Patrolavia/apt-mirror-go for detail.
package main
import (
"compress/gzip"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"github.com/Patrolavia/ratelimit"
)
var (
dryRun bool
cfgFile string
)
func init() {
flag.BoolVar(&dryRun, "n", false, "Log message only, not to download package files")
flag.Parse()
cfgFile = flag.Arg(0)
if cfgFile == "" {
cfgFile = "/etc/apt/mirror.list"
}
}
func main() {
log.Printf("Reading config file from %s", cfgFile)
cfgStr, err := ioutil.ReadFile(cfgFile)
if err != nil {
log.Fatalf("Cannot read config from %s: %s", cfgFile, err)
}
cfg, err := ParseConfig(string(cfgStr))
if err != nil {
log.Fatalf("Error parsing config files: %s", err)
}
nthreads := cfg.GetInt("nthreads")
if nthreads < 1 {
nthreads = 1
}
var bucket *ratelimit.Bucket = nil
rate := cfg.GetInt("ratelimit")
if rate > 0 {
log.Printf("Limit overall transfer rate to %dkb/s", rate)
bucket = ratelimit.NewFromRate(float64(rate*1024), int64(rate*1024), 0)
}
dlMgr := NewManager(
func(u *url.URL) string {
return fmt.Sprintf("URL scheme %s of %s is not supported", u.Scheme, u)
},
bucket,
http.DefaultClient,
nthreads,
)
log.Printf("Path holding temp files(skel_path): %s", cfg.Variables["skel_path"])
log.Printf("Path holding mirrored files(mirror_path): %s", cfg.Variables["mirror_path"])
log.Printf("Default architecture: %s", cfg.Variables["defaultarch"])
log.Printf("Spawning %d goroutines to download packages.", nthreads)
ch := make(chan Package)
finish := make(chan int)
for i := 0; i < nthreads; i++ {
go worker(i, cfg, dlMgr, ch, finish)
}
// download info files, process packages file and generate file list
debs := make(map[string]bool)
for _, repo := range cfg.Repositories {
repo.DownloadInfoFiles(cfg, dlMgr)
for _, comp := range repo.Components {
pkgFile := cfg.SkelPath(repo.Packages(comp))
var f io.ReadCloser
f, err := os.Open(pkgFile)
if err != nil {
// maybe file not found, use gzipped file instead
pkgFile = cfg.SkelPath(repo.PackagesGZ(comp))
f, err = os.Open(pkgFile)
if err == nil {
f, err = gzip.NewReader(f)
}
}
if err != nil {
log.Fatalf("Cannot open package file %s: %s", pkgFile, err)
}
pkgs, err := ParsePackage(repo, f)
if err != nil {
log.Fatalf("Cannot parse Packages file %s: %s", pkgFile, err)
}
f.Close()
for _, p := range pkgs {
m := cfg.MirrorPath(p.URL)
debs[m] = true
ch <- p
}
}
}
close(ch)
log.Printf("Got %d package files ... ", len(debs))
// wait for download finish
for i := 0; i < nthreads; i++ {
<-finish
}
for c := range cfg.Clean {
log.Printf("Cleaning %s", c)
clean(c, cfg, debs)
}
if !dryRun {
movefiles(cfg.Variables["skel_path"], cfg.Variables["mirror_path"])
}
}
func worker(id int, cfg *Config, dlMgr *DownloadManager, ch chan Package, finish chan int) {
for p := range ch {
if p.Test(cfg) {
continue
}
debugMsg := ""
// ========== debug log
debugMsg = " :"
m := cfg.MirrorPath(p.URL)
s := cfg.SkelPath(p.URL)
statM, errM := os.Stat(m)
statS, errS := os.Stat(s)
switch {
case errM == nil:
debugMsg += fmt.Sprintf("Size %d not %d", statM.Size(), p.Size)
case errS == nil:
debugMsg += fmt.Sprintf("Size %d not %d", statS.Size(), p.Size)
default:
debugMsg += "File not found"
}
// ========== end of debug
log.Printf("Worker#%d downloading %s%s", id, p.URL, debugMsg)
if !dryRun {
// max retry 3 times
maxRetry := 3
var err error
for i := 0; i < maxRetry; i++ {
if err = p.Download(cfg, dlMgr.Dispatch(p.URL)); err == nil {
break
}
}
if err != nil {
log.Fatalf("Error downloading %s: %s", p.URL, err)
}
}
}
finish <- 1
}
func clean(urlStr string, cfg *Config, debs map[string]bool) {
u, err := url.Parse(urlStr)
if err != nil {
log.Fatalf("%s is not a valid url: %s", urlStr, err)
}
mirrorPath := cfg.MirrorPath(u)
doClean(mirrorPath, debs)
}
func doClean(dir string, debs map[string]bool) {
log.Printf("Cleaning %s", dir)
base, err := os.Open(dir)
if err != nil {
return
}
defer base.Close()
abs := func(path string) string {
return dir + "/" + path
}
children, err := base.Readdir(-1)
if err != nil {
return
}
for _, child := range children {
if child.IsDir() {
dirn := path.Join(dir, child.Name())
doClean(dirn, debs)
if c, err := os.Open(dirn); err == nil {
remove := false
if children, err := c.Readdirnames(-1); err == nil && len(children) == 0 {
remove = true
}
c.Close()
if remove {
log.Printf("Remove empty directory %s", dirn)
if !dryRun {
os.Remove(dirn)
}
}
}
continue
}
p := abs(child.Name())
if _, ok := debs[p]; !ok {
log.Printf("Remove out-dated file %s", p)
if !dryRun {
os.Remove(p)
}
}
}
}
func movefiles(src, dst string) {
f, err := os.Open(src)
if err != nil {
log.Fatalf("Cannot open dir %s for read: %s", src, err)
}
defer f.Close()
dirs, err := f.Readdirnames(-1)
if err != nil {
log.Fatalf("Cannot read contents of dir %s for read: %s", src, err)
}
for _, dir := range dirs {
fn := path.Join(src, dir)
if err := exec.Command("cp", "-r", fn, dst).Run(); err != nil {
log.Fatalf("Cannot move %s to %s: %s", fn, dst, err)
}
exec.Command("rm", "-fr", fn).Run()
}
}