-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
deb.go
308 lines (282 loc) · 7.71 KB
/
deb.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Package deb implements nfpm.Packager providing .deb bindings.
package deb
import (
"archive/tar"
"bytes"
"compress/gzip"
"path/filepath"
// #nosec
"crypto/md5"
"fmt"
"io"
"os"
"strings"
"text/template"
"time"
"github.com/blakesmith/ar"
"github.com/goreleaser/nfpm"
"github.com/pkg/errors"
)
func init() {
nfpm.Register("deb", Default)
}
var goarchToDebian = map[string]string{
"386": "i386",
"amd64": "amd64",
"arm": "armhf",
"arm64": "arm64",
"mips": "mips",
"mipsle": "mipsel",
}
// Default deb packager
var Default = &Deb{}
// Deb is a deb packager implementation
type Deb struct{}
// Package writes a new deb package to the given writer using the given info
func (*Deb) Package(info nfpm.Info, deb io.Writer) (err error) {
arch, ok := goarchToDebian[info.Arch]
if ok {
info.Arch = arch
}
dataTarGz, md5sums, instSize, err := createDataTarGz(info)
if err != nil {
return err
}
controlTarGz, err := createControl(instSize, md5sums, info)
if err != nil {
return err
}
var w = ar.NewWriter(deb)
if err := w.WriteGlobalHeader(); err != nil {
return errors.Wrap(err, "cannot write ar header to deb file")
}
if err := addArFile(w, "debian-binary", []byte("2.0\n")); err != nil {
return errors.Wrap(err, "cannot pack debian-binary")
}
if err := addArFile(w, "control.tar.gz", controlTarGz); err != nil {
return errors.Wrap(err, "cannot add control.tar.gz to deb")
}
if err := addArFile(w, "data.tar.gz", dataTarGz); err != nil {
return errors.Wrap(err, "cannot add data.tar.gz to deb")
}
return nil
}
func addArFile(w *ar.Writer, name string, body []byte) error {
var header = ar.Header{
Name: name,
Size: int64(len(body)),
Mode: 0644,
ModTime: time.Now(),
}
if err := w.WriteHeader(&header); err != nil {
return errors.Wrap(err, "cannot write file header")
}
_, err := w.Write(body)
return err
}
func createDataTarGz(info nfpm.Info) (dataTarGz, md5sums []byte, instSize int64, err error) {
var buf bytes.Buffer
var compress = gzip.NewWriter(&buf)
var out = tar.NewWriter(compress)
// the writers are properly closed later, this is just in case that we have
// an error in another part of the code.
defer out.Close() // nolint: errcheck
defer compress.Close() // nolint: errcheck
var created = map[string]bool{}
var md5buf bytes.Buffer
for _, files := range []map[string]string{
info.Files,
info.ConfigFiles,
} {
for src, dst := range files {
if err := createTree(out, dst, created); err != nil {
return nil, nil, 0, err
}
size, err := copyToTarAndDigest(out, &md5buf, src, dst)
if err != nil {
return nil, nil, 0, err
}
instSize += size
}
}
if err := out.Close(); err != nil {
return nil, nil, 0, errors.Wrap(err, "closing data.tar.gz")
}
if err := compress.Close(); err != nil {
return nil, nil, 0, errors.Wrap(err, "closing data.tar.gz")
}
return buf.Bytes(), md5buf.Bytes(), instSize, nil
}
func copyToTarAndDigest(tarw *tar.Writer, md5w io.Writer, src, dst string) (int64, error) {
file, err := os.OpenFile(src, os.O_RDONLY, 0600)
if err != nil {
return 0, errors.Wrap(err, "could not add file to the archive")
}
// don't care if it errs while closing...
defer file.Close() // nolint: errcheck
info, err := file.Stat()
if err != nil {
return 0, err
}
if info.IsDir() {
// TODO: this should probably return an error
return 0, nil
}
var header = tar.Header{
Name: dst[1:],
Size: info.Size(),
Mode: int64(info.Mode()),
ModTime: time.Now(),
Format: tar.FormatGNU,
}
if err := tarw.WriteHeader(&header); err != nil {
return 0, errors.Wrapf(err, "cannot write header of %s to data.tar.gz", header)
}
// #nosec
var digest = md5.New()
if _, err := io.Copy(tarw, io.TeeReader(file, digest)); err != nil {
return 0, errors.Wrap(err, "failed to copy")
}
if _, err := fmt.Fprintf(md5w, "%x %s\n", digest.Sum(nil), header.Name); err != nil {
return 0, errors.Wrap(err, "failed to write md5")
}
return info.Size(), nil
}
func createControl(instSize int64, md5sums []byte, info nfpm.Info) (controlTarGz []byte, err error) {
var buf bytes.Buffer
var compress = gzip.NewWriter(&buf)
var out = tar.NewWriter(compress)
// the writers are properly closed later, this is just in case that we have
// an error in another part of the code.
defer out.Close() // nolint: errcheck
defer compress.Close() // nolint: errcheck
var body bytes.Buffer
if err := writeControl(&body, controlData{
Info: info,
InstalledSize: instSize / 1024,
}); err != nil {
return nil, err
}
for name, content := range map[string][]byte{
"control": body.Bytes(),
"md5sums": md5sums,
"conffiles": conffiles(info),
} {
if err := newFileInsideTarGz(out, name, content); err != nil {
return nil, err
}
}
if err := out.Close(); err != nil {
return nil, errors.Wrap(err, "closing control.tar.gz")
}
if err := compress.Close(); err != nil {
return nil, errors.Wrap(err, "closing control.tar.gz")
}
return buf.Bytes(), nil
}
func newFileInsideTarGz(out *tar.Writer, name string, content []byte) error {
var header = tar.Header{
Name: name,
Size: int64(len(content)),
Mode: 0644,
ModTime: time.Now(),
Typeflag: tar.TypeReg,
Format: tar.FormatGNU,
}
if err := out.WriteHeader(&header); err != nil {
return errors.Wrapf(err, "cannot write header of %s file to control.tar.gz", name)
}
if _, err := out.Write(content); err != nil {
return errors.Wrapf(err, "cannot write %s file to control.tar.gz", name)
}
return nil
}
// this is needed because the data.tar.gz file should have the empty folders
// as well, so we walk through the dst and create all subfolders.
func createTree(tarw *tar.Writer, dst string, created map[string]bool) error {
for _, path := range pathsToCreate(dst) {
if created[path] {
// skipping dir that was previously created inside the archive
// (eg: usr/)
continue
}
if err := tarw.WriteHeader(&tar.Header{
Name: path + "/",
Mode: 0755,
Typeflag: tar.TypeDir,
Format: tar.FormatGNU,
ModTime: time.Now(),
}); err != nil {
return errors.Wrap(err, "failed to create folder")
}
created[path] = true
}
return nil
}
func pathsToCreate(dst string) []string {
var paths = []string{}
var base = dst[1:]
for {
base = filepath.Dir(base)
if base == "." {
break
}
paths = append(paths, base)
}
// we don't really need to create those things in order apparently, but,
// it looks really weird if we do.
var result = []string{}
for i := len(paths) - 1; i >= 0; i-- {
result = append(result, paths[i])
}
return result
}
func conffiles(info nfpm.Info) []byte {
var confs []string
for _, dst := range info.ConfigFiles {
confs = append(confs, dst)
}
return []byte(strings.Join(confs, "\n") + "\n")
}
var controlTemplate = `Package: {{.Info.Name}}
Version: {{.Info.Version}}
Section: {{.Info.Section}}
Priority: {{.Info.Priority}}
Architecture: {{.Info.Arch}}
Maintainer: {{.Info.Maintainer}}
Vendor: {{.Info.Vendor}}
Installed-Size: {{.InstalledSize}}
{{- with .Info.Replaces}}
Replaces: {{join .}}
{{- end }}
{{- with .Info.Provides}}
Provides: {{join .}}
{{- end }}
{{- with .Info.Depends}}
Depends: {{join .}}
{{- end }}
{{- with .Info.Recommends}}
Recommends: {{join .}}
{{- end }}
{{- with .Info.Suggests}}
Suggests: {{join .}}
{{- end }}
{{- with .Info.Conflicts}}
Conflicts: {{join .}}
{{- end }}
Homepage: {{.Info.Homepage}}
Description: {{.Info.Description}}
`
type controlData struct {
Info nfpm.Info
InstalledSize int64
}
func writeControl(w io.Writer, data controlData) error {
var tmpl = template.New("control")
tmpl.Funcs(template.FuncMap{
"join": func(strs []string) string {
return strings.Trim(strings.Join(strs, ", "), " ")
},
})
return template.Must(tmpl.Parse(controlTemplate)).Execute(w, data)
}