This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
238 lines (213 loc) · 5.49 KB
/
main.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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/mholt/archiver/v3"
"github.com/pkg/errors"
)
var configs = []Config{
{
BaseAddress: "https://p64.arikawa-hi.me/$repo/$arch",
Format: "tar.xz",
Repos: []string{"danctnix", "phosh", "pine64"},
},
{
BaseAddress: "https://repo.lohl1kohl.de/$repo/$arch",
Format: "tar.xz",
Repos: []string{"beryllium"},
},
{
BaseAddress: "https://raw.githubusercontent.com/arch-beryllium/ui-packages/packages/$repo",
Format: "tar.xz",
Repos: []string{"plasma-mobile", "lomiri"},
},
{
BaseAddress: "http://mirror.archlinuxarm.org/$arch/$repo",
Format: "tar.gz",
Repos: []string{"alarm", "aur", "community", "core", "extra"},
},
}
type Config struct {
BaseAddress string
Format string
Repos []string
}
func main() {
for _, config := range configs {
for _, repo := range config.Repos {
dirPath := filepath.Join("mirror", repo, "aarch64")
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
err = os.MkdirAll(dirPath, 0755)
if err != nil {
fmt.Printf("Failed to create %s: %v\n", dirPath, err)
os.Exit(1)
}
}
dbFilePath := filepath.Join(dirPath, fmt.Sprintf("%s.%s", repo, config.Format))
dbFileURL := fmt.Sprintf("%s/%s.db", buildURL(config.BaseAddress, repo), repo)
err := downloadFile(dbFilePath, dbFileURL)
if err != nil {
fmt.Printf("Failed to download %s: %v\n", dbFileURL, err)
os.Exit(1)
}
err = copyFile(dbFilePath, filepath.Join(dirPath, fmt.Sprintf("%s.db", repo)))
if err != nil {
fmt.Printf("Failed to copy %s : %v\n", dbFilePath, err)
os.Exit(1)
}
var tmpDir string
tmpDir, err = ioutil.TempDir("", "arch-repo-mirror-*")
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
err = archiver.Unarchive(dbFilePath, tmpDir)
if err != nil {
fmt.Printf("Failed to read %s: %v\n", dbFilePath, err)
os.Exit(1)
}
var dirs []os.FileInfo
dirs, err = ioutil.ReadDir(tmpDir)
if err != nil {
fmt.Printf("Failed to read dir %s: %v\n", tmpDir, err)
os.Exit(1)
}
var fileNames []string
for _, dir := range dirs {
descFilePath := filepath.Join(tmpDir, dir.Name(), "desc")
var content []byte
content, err = ioutil.ReadFile(descFilePath)
if err != nil {
fmt.Printf("Failed to read %s: %v\n", descFilePath, err)
os.Exit(1)
}
fileName := ""
lines := strings.Split(string(content), "\n")
for i, line := range lines {
if line == "%FILENAME%" {
fileName = lines[i+1]
break
}
}
fileNames = append(fileNames, fileName)
filePath := filepath.Join(dirPath, fileName)
fileURL := fmt.Sprintf("%s/%s", buildURL(config.BaseAddress, repo), fileName)
if _, err = os.Stat(filePath); os.IsNotExist(err) {
err = downloadFile(filePath, fileURL)
if err != nil {
fmt.Printf("Failed to download %s: %v\n", fileURL, err)
os.Exit(1)
}
}
}
files, err := ioutil.ReadDir(dirPath)
if err != nil {
fmt.Printf("Failed to read dir %s: %v\n", tmpDir, err)
os.Exit(1)
}
for _, file := range files {
if file.Name() == fmt.Sprintf("%s.db", repo) || file.Name() == fmt.Sprintf("%s.%s", repo, config.Format) {
continue
}
found := false
for _, fileName := range fileNames {
if file.Name() == fileName {
found = true
}
}
if !found {
fmt.Printf("Deleting %s\n", file.Name())
err = os.Remove(filepath.Join(dirPath, file.Name()))
if err != nil {
fmt.Printf("Failed to remove %s: %v\n", tmpDir, err)
os.Exit(1)
}
}
}
err = os.RemoveAll(tmpDir)
if err != nil {
fmt.Printf("Failed to remove %s: %v\n", tmpDir, err)
os.Exit(1)
}
}
}
}
func printDownloadPercent(done chan chan struct{}, path string, expectedSize int64) {
var completedCh chan struct{}
for {
fi, err := os.Stat(path)
if err != nil {
fmt.Printf("%v\n", err)
}
size := fi.Size()
if size == 0 {
size = 1
}
var percent = float64(size) / float64(expectedSize) * 100
fmt.Printf("\033[2K\r %.0f %% / 100 %%", percent)
if completedCh != nil {
close(completedCh)
return
}
select {
case completedCh = <-done:
case <-time.After(time.Second / 60):
}
}
}
func downloadFile(filepath string, url string) error {
fmt.Println(url)
start := time.Now()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
expectedSize, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return errors.Wrap(err, "failed to get Content-Length header")
}
doneCh := make(chan chan struct{})
go printDownloadPercent(doneCh, filepath, int64(expectedSize))
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
doneCompletedCh := make(chan struct{})
doneCh <- doneCompletedCh
<-doneCompletedCh
elapsed := time.Since(start)
fmt.Printf("\033[2K\rDownload completed in %.2fs\n", elapsed.Seconds())
return nil
}
func buildURL(baseAddress, repo string) string {
baseAddress = strings.ReplaceAll(baseAddress, "$repo", repo)
baseAddress = strings.ReplaceAll(baseAddress, "$arch", "aarch64")
return baseAddress
}
func copyFile(src, dest string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
file, err := os.Create(dest)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, in)
return err
}