-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeb_writer.go
138 lines (130 loc) · 2.9 KB
/
deb_writer.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
package deb
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/myml/ar"
"github.com/ulikunitz/xz"
)
// Writer Deb包Writer,类似tar的API
type Writer struct {
tempDir string
arWriter *ar.Writer
controlFile *os.File
dataFile *os.File
gzipWriter io.WriteCloser
tarWriter *tar.Writer
}
func NewWriter(w io.Writer) *Writer {
deb := &Writer{arWriter: ar.NewWriter(w)}
return deb
}
func (w *Writer) WriteHeader(head *tar.Header) error {
var err error
if len(w.tempDir) == 0 {
w.tempDir, err = ioutil.TempDir("", "archive-deb_")
if err != nil {
return err
}
}
if head.Name == "." || head.Name == "DEBIAN" {
return nil
}
if strings.HasPrefix(head.Name, "DEBIAN") {
head.Name = head.Name[len("DEBIAN/"):]
if w.controlFile == nil {
w.controlFile, err = os.Create(filepath.Join(w.tempDir, "control.tar.gz"))
if err != nil {
return err
}
w.gzipWriter = gzip.NewWriter(w.controlFile)
w.tarWriter = tar.NewWriter(w.gzipWriter)
}
} else {
if w.dataFile == nil {
w.dataFile, err = os.Create(filepath.Join(w.tempDir, "data.tar.xz"))
if err != nil {
return err
}
err = w.close()
if err != nil {
return err
}
w.gzipWriter, err = xz.NewWriter(w.dataFile)
if err != nil {
return fmt.Errorf("new xz writer")
}
w.tarWriter = tar.NewWriter(w.gzipWriter)
}
}
head.Name = "./" + head.Name
head.Format = tar.FormatGNU
return w.tarWriter.WriteHeader(head)
}
func (w *Writer) Write(b []byte) (int, error) {
return w.tarWriter.Write(b)
}
func (w *Writer) Close() error {
err := w.close()
if err != nil {
return fmt.Errorf("writer close %w", err)
}
err = w.arWriter.WriteGlobalHeader()
if err != nil {
return fmt.Errorf("writer global header %w", err)
}
err = w.arWriter.WriteHeader(&ar.Header{Name: "debian-binary", ModTime: time.Now(), Mode: 0655, Size: 4})
if err != nil {
return fmt.Errorf("write debian-binary header")
}
_, err = w.arWriter.Write([]byte("2.0\n"))
if err != nil {
return fmt.Errorf("write debian-binary data")
}
for _, f := range []*os.File{w.controlFile, w.dataFile} {
err = f.Close()
if err != nil {
return fmt.Errorf("close file %w", err)
}
f, err := os.Open(f.Name())
defer f.Close()
if err != nil {
return fmt.Errorf("read file %w", err)
}
stat, err := f.Stat()
if err != nil {
return fmt.Errorf("stat %w", err)
}
err = w.arWriter.WriteHeader(&ar.Header{
Name: stat.Name(),
ModTime: stat.ModTime(),
Mode: int64(stat.Mode()),
Size: stat.Size(),
})
if err != nil {
return fmt.Errorf("write header %w", err)
}
_, err = io.Copy(w.arWriter, f)
if err != nil {
return fmt.Errorf("write data %w", err)
}
}
return nil
}
func (w *Writer) close() error {
err := w.tarWriter.Close()
if err != nil {
return err
}
err = w.gzipWriter.Close()
if err != nil {
return err
}
return nil
}