-
Notifications
You must be signed in to change notification settings - Fork 9
/
ar.go
101 lines (82 loc) · 2.12 KB
/
ar.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
// Copyright 2017 Debpkg authors. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package debpkg
import (
"fmt"
"io"
"os"
"time"
"github.com/xor-gate/ar"
)
func addArFileFromBuffer(now time.Time, w *ar.Writer, name string, body []byte) error {
hdr := ar.Header{
Name: name,
Size: int64(len(body)),
Mode: 0644,
ModTime: now,
}
if err := w.WriteHeader(&hdr); err != nil {
return fmt.Errorf("cannot write file header: %v", err)
}
_, err := w.Write(body)
return err
}
func addArFile(now time.Time, w *ar.Writer, dstname, filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
stat, err := f.Stat()
if err != nil {
f.Close()
return err
}
hdr := ar.Header{
Name: dstname,
Size: stat.Size(),
Mode: 0644,
ModTime: now,
}
if err := w.WriteHeader(&hdr); err != nil {
f.Close()
return fmt.Errorf("cannot write file header: %v", err)
}
_, err = io.Copy(w, f)
f.Close()
return err
}
func (deb *DebPkg) createDebAr(filename string) error {
removeDeb := true
fd, err := os.Create(filename)
if err != nil {
return fmt.Errorf("unable to create: %s", filename)
}
defer func() {
fd.Close()
if removeDeb {
os.Remove(filename)
}
}()
now := time.Now()
w := ar.NewWriter(fd)
if err := w.WriteGlobalHeader(); err != nil {
return fmt.Errorf("cannot write ar header to deb file: %v", err)
}
if err := addArFileFromBuffer(now, w, "debian-binary", []byte(deb.debianBinary)); err != nil {
return fmt.Errorf("cannot pack debian-binary: %v", err)
}
if err := addArFile(now, w, "control.tar.gz", deb.control.tgz.Name()); err != nil {
return fmt.Errorf("cannot add control.tar.gz to deb: %v", err)
}
if err := addArFile(now, w, "data.tar.gz", deb.data.tgz.Name()); err != nil {
return fmt.Errorf("cannot add data.tar.gz to deb: %v", err)
}
if deb.digest.clearsign != "" {
if err := addArFileFromBuffer(now, w, "digests.asc", []byte(deb.digest.clearsign)); err != nil {
return fmt.Errorf("cannot add digests.asc to deb: %v", err)
}
}
removeDeb = false
return nil
}