1
1
mirror of https://github.com/goreleaser/nfpm synced 2024-11-19 03:25:08 +01:00
nfpm/deb/deb.go

231 lines
6.1 KiB
Go
Raw Normal View History

2018-02-05 02:53:22 +01:00
// Package deb implements nfpm.Packager providing .deb bindings.
2018-01-04 13:31:22 +01:00
package deb
import (
"archive/tar"
2018-01-04 13:31:22 +01:00
"bytes"
"compress/gzip"
2018-02-16 03:16:44 +01:00
// #nosec
"crypto/md5"
"fmt"
"io"
2018-01-04 13:31:22 +01:00
"os"
"strings"
2018-01-04 13:31:22 +01:00
"text/template"
"time"
2018-01-04 13:31:22 +01:00
"github.com/blakesmith/ar"
2018-02-05 02:53:22 +01:00
"github.com/goreleaser/nfpm"
2018-02-12 17:09:59 +01:00
"github.com/pkg/errors"
2018-01-04 13:31:22 +01:00
)
2018-02-12 16:50:25 +01:00
func init() {
nfpm.Register("deb", Default)
}
2018-01-10 14:16:07 +01:00
// 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
2018-02-05 02:53:22 +01:00
func (*Deb) Package(info nfpm.Info, deb io.Writer) (err error) {
var now = time.Now()
2018-01-10 14:16:07 +01:00
dataTarGz, md5sums, instSize, err := createDataTarGz(now, info)
2018-01-04 13:31:22 +01:00
if err != nil {
return err
2018-01-04 13:31:22 +01:00
}
controlTarGz, err := createControl(now, instSize, md5sums, info)
2018-01-04 13:31:22 +01:00
if err != nil {
return err
}
var w = ar.NewWriter(deb)
if err := w.WriteGlobalHeader(); err != nil {
2018-02-12 17:09:59 +01:00
return errors.Wrap(err, "cannot write ar header to deb file")
2018-01-04 13:31:22 +01:00
}
if err := addArFile(now, w, "debian-binary", []byte("2.0\n")); err != nil {
2018-02-12 17:09:59 +01:00
return errors.Wrap(err, "cannot pack debian-binary")
2018-01-04 13:31:22 +01:00
}
if err := addArFile(now, w, "control.tar.gz", controlTarGz); err != nil {
2018-02-12 17:09:59 +01:00
return errors.Wrap(err, "cannot add control.tar.gz to deb")
}
if err := addArFile(now, w, "data.tar.gz", dataTarGz); err != nil {
2018-02-12 17:09:59 +01:00
return errors.Wrap(err, "cannot add data.tar.gz to deb")
}
return nil
2018-01-04 13:31:22 +01:00
}
func addArFile(now time.Time, w *ar.Writer, name string, body []byte) error {
var header = ar.Header{
Name: name,
Size: int64(len(body)),
Mode: 0644,
ModTime: now,
2018-01-04 13:31:22 +01:00
}
if err := w.WriteHeader(&header); err != nil {
2018-02-12 17:09:59 +01:00
return errors.Wrap(err, "cannot write file header")
}
_, err := w.Write(body)
2018-01-04 13:31:22 +01:00
return err
}
2018-02-05 02:53:22 +01:00
func createDataTarGz(now time.Time, info nfpm.Info) (dataTarGz, md5sums []byte, instSize int64, err error) {
var buf bytes.Buffer
var compress = gzip.NewWriter(&buf)
var out = tar.NewWriter(compress)
2018-02-16 02:58:37 +01:00
// 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 md5buf bytes.Buffer
2018-02-16 22:11:52 +01:00
for _, files := range []map[string]string{
info.Files,
info.ConfigFiles,
} {
2018-02-06 03:28:30 +01:00
for src, dst := range files {
2018-02-16 03:12:54 +01:00
size, err := copyToTarAndDigest(out, &md5buf, now, src, dst)
2018-02-06 03:28:30 +01:00
if err != nil {
2018-02-16 03:12:54 +01:00
return nil, nil, 0, err
2018-02-16 02:58:37 +01:00
}
2018-02-16 03:12:54 +01:00
instSize += size
}
}
if err := out.Close(); err != nil {
2018-02-12 17:09:59 +01:00
return nil, nil, 0, errors.Wrap(err, "closing data.tar.gz")
}
if err := compress.Close(); err != nil {
2018-02-12 17:09:59 +01:00
return nil, nil, 0, errors.Wrap(err, "closing data.tar.gz")
}
return buf.Bytes(), md5buf.Bytes(), instSize, nil
}
2018-02-16 03:12:54 +01:00
func copyToTarAndDigest(tarw *tar.Writer, md5w io.Writer, now time.Time, 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() {
return 0, nil
}
var header = tar.Header{
Name: dst,
Size: info.Size(),
Mode: int64(info.Mode()),
ModTime: now,
}
if err := tarw.WriteHeader(&header); err != nil {
return 0, errors.Wrapf(err, "cannot write header of %s to data.tar.gz", header)
}
if _, err := io.Copy(tarw, file); err != nil {
return 0, errors.Wrapf(err, "cannot write %s to data.tar.gz", header)
}
2018-02-16 03:16:44 +01:00
// #nosec
2018-02-16 03:12:54 +01:00
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[2:]); err != nil {
return 0, errors.Wrap(err, "failed to write md5")
}
return info.Size(), nil
}
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}}
2018-02-05 02:42:03 +01:00
Replaces: {{join .Info.Replaces}}
Provides: {{join .Info.Provides}}
Depends: {{join .Info.Depends}}
Conflicts: {{join .Info.Conflicts}}
Homepage: {{.Info.Homepage}}
Description: {{.Info.Description}}
2018-01-04 13:31:22 +01:00
`
type controlData struct {
2018-02-05 02:53:22 +01:00
Info nfpm.Info
InstalledSize int64
}
2018-02-05 02:53:22 +01:00
func createControl(now time.Time, 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)
2018-02-16 02:58:37 +01:00
// 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
var tmpl = template.New("control")
tmpl.Funcs(template.FuncMap{
"join": func(strs []string) string {
return strings.Trim(strings.Join(strs, ", "), " ")
},
2018-01-04 13:31:22 +01:00
})
if err := template.Must(tmpl.Parse(controlTemplate)).Execute(&body, controlData{
Info: info,
InstalledSize: instSize / 1024,
}); err != nil {
return nil, err
2018-01-04 13:31:22 +01:00
}
for name, content := range map[string][]byte{
"control": body.Bytes(),
"md5sums": md5sums,
"conffiles": conffiles(info),
} {
if err := newFileInsideTarGz(out, name, content, now); 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, now time.Time) error {
var header = tar.Header{
Name: name,
Size: int64(len(content)),
Mode: 0644,
ModTime: now,
Typeflag: tar.TypeReg,
}
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
}
func conffiles(info nfpm.Info) []byte {
var confs []string
for _, dst := range info.ConfigFiles {
confs = append(confs, dst)
2018-01-04 13:31:22 +01:00
}
return []byte(strings.Join(confs, "\n") + "\n")
2018-01-04 13:31:22 +01:00
}