-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipkg.go
259 lines (220 loc) · 6.04 KB
/
ipkg.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
251
252
253
254
255
256
257
258
259
// This file is part of *kellner*
//
// Copyright (C) 2015, Travelping GmbH <copyright@travelping.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha1"
"encoding/hex"
"fmt"
"hash"
"io"
"io/ioutil"
"net/textproto"
"os"
"path"
"strconv"
"strings"
"github.com/blakesmith/ar"
)
type ipkArchive struct {
Name string
Control string // content of 'control' file
Header map[string]string
FileInfo os.FileInfo
Md5 string
Sha1 string
ScanLocation string // location where the ipk was found
}
// ControlToHeader parses 'control' and stores the result in ipkg.Header
func (ipk *ipkArchive) ControlToHeader(control string) error {
reader := bufio.NewReader(strings.NewReader(control))
protoReader := textproto.NewReader(reader)
for {
line, err := protoReader.ReadContinuedLine()
if err == io.EOF {
break
}
i := strings.IndexByte(line, ':')
if i == -1 {
return fmt.Errorf("invalid package-field %q", line)
}
ipk.Header[line[:i]] = strings.TrimSpace(line[i+1:])
}
return nil
}
func (ipk *ipkArchive) EnhanceHeader() {
ipk.Header["Size"] = strconv.FormatInt(ipk.FileInfo.Size(), 10)
if ipk.Md5 != "" {
ipk.Header["MD5Sum"] = ipk.Md5
}
if ipk.Sha1 != "" {
ipk.Header["SHA1"] = ipk.Sha1
}
}
func (ipk *ipkArchive) DirEntry() dirEntry {
descr := ipk.Header["Description"]
if len(descr) > 64 {
descr = descr[:64] + "..."
}
return dirEntry{
Name: ipk.Name,
ModTime: ipk.FileInfo.ModTime(),
Size: ipk.FileInfo.Size(),
Descr: descr,
RawDescr: ipk.Header["Description"],
}
}
// HeaderTo writes the package-header to 'to'.
// according to https://www.debian.org/doc/debian-policy/ch-controlfields.html
// the order of the fields does not matter
// according to https://wiki.debian.org/RepositoryFormat#A.22Packages.22_Indices
// 'Packages' should be the first field.
func (ipk *ipkArchive) HeaderTo(w io.Writer) {
p, ok := ipk.Header["Package"]
if ok {
fmt.Fprintf(w, "Package: %s\n", p)
}
for key := range ipk.Header {
if key != "Package" {
fmt.Fprintf(w, "%s: %s\n", key, ipk.Header[key])
}
}
}
// ControlAndChecksumTo writes the 'control' file,
// the file name, the size and the checksums to the
// writer 'w'
func (ipk *ipkArchive) ControlAndChecksumTo(w io.Writer) {
io.WriteString(w, ipk.Control)
fmt.Fprintf(w, "Filename: %s\n", ipk.Name)
fmt.Fprintf(w, "Size: %d\n", ipk.FileInfo.Size())
if ipk.Md5 != "" {
fmt.Fprintf(w, "MD5Sum: %s\n", ipk.Md5)
}
if ipk.Sha1 != "" {
fmt.Fprintf(w, "SHA1: %s\n", ipk.Sha1)
}
}
type ipkArchiveChan chan *ipkArchive
// extract 'control' file from 'reader'. the contents of a 'control' file
// is a set of key-value pairs as described in
// https://www.debian.org/doc/debian-policy/ch-controlfields.html
func extractControlFromIpk(reader io.Reader) (string, error) {
var (
arReader *ar.Reader
tarReader *tar.Reader
gzReader *gzip.Reader
)
arReader = ar.NewReader(reader)
for {
header, err := arReader.Next()
if err != nil && err != io.EOF {
return "", fmt.Errorf("extracting contents: %v", err)
} else if header == nil {
break
}
// NOTE: strangeley the name of the files end with a "/" ... content error?
if header.Name == "control.tar.gz/" || header.Name == "control.tar.gz" {
gzReader, err = gzip.NewReader(arReader)
if err != nil {
return "", fmt.Errorf("analyzing control.tar.gz: %v", err)
}
break
}
}
if gzReader == nil {
return "", fmt.Errorf("missing control.tar.gz entry")
}
defer gzReader.Close()
buffer := bytes.NewBuffer(nil)
tarReader = tar.NewReader(gzReader)
for {
header, err := tarReader.Next()
if err != nil && err != io.EOF {
return "", fmt.Errorf("extracting control.tar.gz: %v", err)
} else if header == nil {
break
}
if header.Name != "./control" {
continue
}
io.Copy(buffer, tarReader)
break
}
if buffer.Len() == 0 {
return "", fmt.Errorf("missing or empty 'control' file inside 'control.tar.gz'")
}
return buffer.String(), nil
}
func newIpkFromFile(fullName string, doMD5, doSHA1 bool) (*ipkArchive, error) {
var (
file *os.File
writer = make([]io.Writer, 0, 3)
err error
md5er hash.Hash
sha1er hash.Hash
)
file, err = os.Open(fullName)
if err != nil {
return nil, fmt.Errorf("openening %q: %v", fullName, err)
}
defer file.Close()
writer = append(writer, ioutil.Discard)
if doMD5 {
md5er = md5.New()
writer = append(writer, md5er)
}
if doSHA1 {
sha1er = sha1.New()
writer = append(writer, sha1er)
}
tee := io.TeeReader(file, io.MultiWriter(writer...))
control, err := extractControlFromIpk(tee)
if err != nil {
return nil, fmt.Errorf("extract pkg-info from %q: %v", fullName, err)
}
archive := &ipkArchive{
Name: path.Base(fullName),
Control: control,
Header: make(map[string]string),
ScanLocation: fullName}
if err := archive.ControlToHeader(control); err != nil {
return nil, fmt.Errorf("header parse error in %q: %v", fullName, err)
}
// consume the rest of the file to calculate md5/sha1
io.Copy(ioutil.Discard, tee)
file.Close() // close to free handles, 'collector' might block freeing otherwise
archive.FileInfo, _ = os.Lstat(fullName)
if md5er != nil {
archive.Md5 = hex.EncodeToString(md5er.Sum(nil))
}
if sha1er != nil {
archive.Sha1 = hex.EncodeToString(sha1er.Sum(nil))
}
return archive, nil
}
func newIpkFromCache(name, cachepath string, doMD5, doSHA1 bool) (*ipkArchive, error) {
var (
ctrlName = genCachedControlName(name, cachepath)
control []byte
err error
)
if control, err = ioutil.ReadFile(ctrlName); err != nil {
return nil, fmt.Errorf("reading cache %q: %v\n", ctrlName, err)
}
var archive = &ipkArchive{Name: name,
Control: string(control),
Header: make(map[string]string),
}
archive.FileInfo, _ = os.Stat(ctrlName)
archive.ControlToHeader(archive.Control)
return archive, nil
}