-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
93 lines (78 loc) · 1.93 KB
/
repo.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
package main
import (
"encoding/xml"
"log"
"os"
)
type mirrorAction struct {
Name string
Source string
Destination string
ChecksumType string
Checksum string
Size int64
}
// RepositoryMirror initializes a mirror between a remote url and a local folder
func RepositoryMirror(repoURL string, destination string, dataDestination string) *RepoMetadata {
repomd := RepoMetadata{Origin: repoURL, Destination: destination, DataDestination: dataDestination}
url := repomd.RepomdURL()
logIt(1, "Fetching repomd url '"+url+"'")
result := getURL(url)
err := xml.Unmarshal(result, &repomd)
if err != nil {
logItFatal(err)
}
for _, data := range repomd.Data {
data.Origin = repoURL
data.Destination = destination
}
return &repomd
}
func (r *mirrorAction) mirrorCond(sizeCheck bool) bool {
if r.verify(sizeCheck) {
return true
}
logIt(1, "We don't have the correct version of '"+r.Name+"'. Downloading it...")
r.mirror()
return false
}
func (r *mirrorAction) mirror() {
source := r.Source
if *head {
touchURL(source)
return
}
destination := r.Destination
tmpDestination := destination + ".part"
makeDestination(tmpDestination)
writeURLToFile(source, tmpDestination)
if !r.verifyChecksumPath(tmpDestination) {
log.Fatal("WTF?")
}
os.Rename(tmpDestination, destination)
}
func (r *mirrorAction) verify(sizeCheck bool) bool {
if *head {
return false
}
if sizeCheck && r.Size > 0 {
return r.verifySize()
}
return r.verifyChecksum()
}
func (r *mirrorAction) verifySize() bool {
return r.verifySizePath(r.Destination)
}
func (r *mirrorAction) verifySizePath(path string) bool {
remote := r.Size
local := sizeForFile(path)
return remote == local
}
func (r *mirrorAction) verifyChecksum() bool {
return r.verifyChecksumPath(r.Destination)
}
func (r *mirrorAction) verifyChecksumPath(path string) bool {
remote := r.Checksum
local := checksumForFile(path, r.ChecksumType)
return remote == local
}