-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpackages.go
208 lines (180 loc) · 4.81 KB
/
packages.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
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"errors"
"github.com/blakesmith/ar"
lzma "github.com/xi2/xz"
)
type Compression int
const (
LZMA Compression = iota
GZIP
)
func inspectPackage(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
return "", fmt.Errorf("error opening package file %s: %s", filename, err)
}
if *verbose {
log.Printf("Inpecting package file \"%s\"", filename)
}
arReader := ar.NewReader(f)
defer f.Close()
var controlBuf bytes.Buffer
for {
header, err := arReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", fmt.Errorf("error in inspectPackage loop: %s", err)
}
if strings.Contains(header.Name, "control.tar") {
var compression Compression
if strings.TrimRight(header.Name, "/") == "control.tar.gz" {
compression = GZIP
} else if strings.TrimRight(header.Name, "/") == "control.tar.xz" {
compression = LZMA
} else {
log.Println("\t No control file found")
err := errors.New("No control file found")
return "", err
}
io.Copy(&controlBuf, arReader)
if *verbose {
log.Println("\t Found a package control file")
}
return inspectPackageControl(compression, controlBuf)
}
}
return "", nil
}
func inspectPackageControl(compression Compression, filename bytes.Buffer) (string, error) {
var tarReader *tar.Reader
var err error
switch compression {
case GZIP:
var compFile *gzip.Reader
compFile, err = gzip.NewReader(bytes.NewReader(filename.Bytes()))
tarReader = tar.NewReader(compFile)
if *verbose {
log.Println("\t GZIP Control file found")
}
break
case LZMA:
var compFile *lzma.Reader
compFile, err = lzma.NewReader(bytes.NewReader(filename.Bytes()), lzma.DefaultDictMax)
tarReader = tar.NewReader(compFile)
if *verbose {
log.Println("\t LZMA Control file found")
}
break
}
if err != nil {
return "", fmt.Errorf("error creating gzip/lzma reader: %s", err)
}
var controlBuf bytes.Buffer
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return "", fmt.Errorf("failed to inspect package: %s", err)
}
name := header.Name
switch header.Typeflag {
case tar.TypeDir:
continue
case tar.TypeReg:
switch name {
case "control", "./control":
io.Copy(&controlBuf, tarReader)
return strings.TrimRight(controlBuf.String(), "\n") + "\n", nil
}
default:
log.Printf(
"Unable to figure out type : %c in file %s\n",
header.Typeflag, name,
)
}
}
return "", nil
}
func createPackagesGz(config conf, distro, section, arch string) error {
if *verbose {
log.Printf("Rebuilding Packages.gz file for %s %s %s", distro, section, arch)
}
packageFile, err := os.Create(filepath.Join(config.ArchPath(distro, section, arch), "Packages"))
if err != nil {
return fmt.Errorf("failed to create Packages: %s", err)
}
packageGzFile, err := os.Create(filepath.Join(config.ArchPath(distro, section, arch), "Packages.gz"))
if err != nil {
return fmt.Errorf("failed to create packages.gz: %s", err)
}
defer packageFile.Close()
defer packageGzFile.Close()
gzOut := gzip.NewWriter(packageGzFile)
defer gzOut.Close()
writer := io.MultiWriter(packageFile, gzOut)
// loop through each directory
// run inspectPackage
dirList, err := ioutil.ReadDir(config.ArchPath(distro, section, arch))
if err != nil {
return fmt.Errorf("scanning: %s: %s", config.ArchPath(distro, section, arch), err)
}
for i, debFile := range dirList {
if strings.HasSuffix(debFile.Name(), "deb") {
var packBuf bytes.Buffer
debPath := filepath.Join(config.ArchPath(distro, section, arch), debFile.Name())
tempCtlData, err := inspectPackage(debPath)
if err != nil {
return err
}
packBuf.WriteString(tempCtlData)
dir := filepath.ToSlash(filepath.Join("dists", distro, section, "binary-"+arch, debFile.Name()))
fmt.Fprintf(&packBuf, "Filename: %s\n", dir)
fmt.Fprintf(&packBuf, "Size: %d\n", debFile.Size())
f, err := os.Open(debPath)
if err != nil {
log.Println("error opening deb file: ", err)
}
defer f.Close()
var (
md5hash = md5.New()
sha1hash = sha1.New()
sha256hash = sha256.New()
)
_, err = io.Copy(io.MultiWriter(md5hash, sha1hash, sha256hash), f)
if err != nil {
return fmt.Errorf("Error hashing file for Packages file: %s", err)
}
fmt.Fprintf(&packBuf, "MD5sum: %s\n",
hex.EncodeToString(md5hash.Sum(nil)))
fmt.Fprintf(&packBuf, "SHA1: %s\n",
hex.EncodeToString(sha1hash.Sum(nil)))
fmt.Fprintf(&packBuf, "SHA256: %s\n",
hex.EncodeToString(sha256hash.Sum(nil)))
if i != (len(dirList) - 1) {
packBuf.WriteString("\n\n")
}
writer.Write(packBuf.Bytes())
f = nil
}
}
return nil
}