-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
343 lines (298 loc) · 7.2 KB
/
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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package ar
import (
"bytes"
"encoding/binary"
"errors"
"io"
"strconv"
"time"
)
var (
ErrWriteAfterClose = errors.New("ar: write after close")
ErrWriteTooLong = errors.New("ar: write too long")
ErrHeaderTooLong = errors.New("ar: header too long")
)
// entry contains the entry name an the byte offset to its header in the
// file entries buffer.
type entry struct {
Name string
Offset int64
}
// Writer provides sequential writing to an ar archive using the GNU format.
// WriteHeader triggers a new entry to be written, aftwards the writer can be
// used as an io.Writer.
type Writer struct {
writer io.Writer
symbols []*entry // Contains the list for the GNU symbol table.
strings *bytes.Buffer // Contains the GNU strings table.
buf *bytes.Buffer // Contains standard file entries.
uw int64 // Unwritten bytes for the current entry.
pad bool // If the entry should contain the padding byte.
closed bool
}
// NewWriter creates a Writer writing to w.
func NewWriter(w io.Writer) *Writer {
return &Writer{
writer: w,
symbols: make([]*entry, 0),
strings: new(bytes.Buffer),
buf: new(bytes.Buffer),
}
}
// WriteHeader creates a new file entry for header. Calling after it's closed
// will return ErrWriteAfterClose. ErrHeaderTooLong is returned if the header
// won't fit.
func (arw *Writer) WriteHeader(header *Header) error {
if arw.closed {
return ErrWriteAfterClose
}
err := arw.fillUnwritten(arw.buf)
if err != nil {
return err
}
hdr, err := arw.createHeader(true, header)
if err != nil {
return err
}
_, err = arw.buf.Write(hdr)
return err
}
// Write writes b to the current file entry. It returns ErrWriteTooLong if more
// bytes are being written than the header allows.
func (arw *Writer) Write(b []byte) (int, error) {
if arw.closed {
return 0, ErrWriteAfterClose
}
overwrite := false
// Trim so we don't overwrite.
if int64(len(b)) > arw.uw {
b = b[:arw.uw]
overwrite = true
}
n, err := arw.buf.Write(b)
arw.uw -= int64(n)
if err == nil && overwrite {
err = ErrWriteTooLong
}
return n, err
}
// Close closes the ar archive creating the symbol/string tables. All writing
// to the underlying writer is delayed until Close.
func (arw *Writer) Close() error {
if arw.closed {
return nil
}
arw.closed = true
err := arw.fillUnwritten(arw.buf)
if err != nil {
return err
}
// Create strings header, only populated if there's any data in the entry.
strHeader := make([]byte, 0)
if arw.strings.Len() > 0 {
strHeader, err = arw.createHeader(false, &Header{
Name: "//",
ModTime: time.Now(),
Uid: 0,
Gid: 0,
Mode: 0,
Size: int64(arw.strings.Len()),
})
if err != nil {
return err
}
}
// Calculate the size of the data before file entries, used to complete
// the symbol offsets.
size := int64(68) // Magic num + header size.
size += int64(4 + (4 * len(arw.symbols))) // Size of symbol table.
for _, entry := range arw.symbols {
size += int64(len(entry.Name + "\u0000"))
}
if (size-68)%2 != 0 {
size++
}
size += int64(len(strHeader) + arw.strings.Len()) // Strings header + table.
if arw.strings.Len()%2 != 0 {
size++
}
// Create the symbol table.
var symTable bytes.Buffer
err = binary.Write(&symTable, binary.BigEndian, int32(len(arw.symbols)))
if err != nil {
return err
}
for _, entry := range arw.symbols {
// Also set the final offset.
entry.Offset += size
err = binary.Write(&symTable, binary.BigEndian, int32(entry.Offset))
if err != nil {
return err
}
}
for _, entry := range arw.symbols {
_, err = symTable.Write([]byte(entry.Name + "\u0000"))
if err != nil {
return err
}
}
// Create symbol header.
symHeader, err := arw.createHeader(false, &Header{
Name: "/",
ModTime: time.Now(),
Uid: 0,
Gid: 0,
Mode: 0,
Size: int64(symTable.Len()),
})
if err != nil {
return err
}
_, err = arw.writer.Write([]byte("!<arch>\n"))
if err != nil {
return err
}
// Write the symbol table.
_, err = arw.writer.Write(symHeader)
if err != nil {
return err
}
if symTable.Len()%2 != 0 {
_, err = symTable.Write([]byte("\n"))
if err != nil {
return err
}
}
_, err = io.Copy(arw.writer, &symTable)
if err != nil {
return err
}
// Write strings table if needed.
if len(strHeader) > 0 {
_, err = arw.writer.Write(strHeader)
if err != nil {
return err
}
if arw.strings.Len()%2 != 0 {
_, err = arw.strings.Write([]byte("\n"))
if err != nil {
return err
}
}
_, err = io.Copy(arw.writer, arw.strings)
if err != nil {
return err
}
}
_, err = io.Copy(arw.writer, arw.buf)
return err
}
// createHeader creates the header entry, if standard / is added to names, and
// strings/symbol tables are written.
func (arw *Writer) createHeader(standard bool, header *Header) ([]byte, error) {
// Get name and detect if extended.
offset := ""
name := toASCII(header.Name)
if standard {
name += "/"
}
if len(name) > 16 {
if !standard {
return nil, ErrHeaderTooLong
}
offset = name
name = "/" + strconv.Itoa(arw.strings.Len())
}
// Get modtime, and ensure it fits.
mod := strconv.FormatInt(header.ModTime.Unix(), 10)
if len(mod) > 12 {
return nil, ErrHeaderTooLong
}
// Get uid/gid, and ensure they'll fit.
uid := strconv.Itoa(header.Uid)
if len(uid) > 6 {
return nil, ErrHeaderTooLong
}
gid := strconv.Itoa(header.Gid)
if len(gid) > 6 {
return nil, ErrHeaderTooLong
}
// Format the mode, and ensure it fits.
mode := strconv.FormatInt(header.Mode, 8)
if standard {
mode = "100" + mode
}
if len(mode) > 8 {
return nil, ErrHeaderTooLong
}
// Get size, and ensure it fits.
size := strconv.FormatInt(header.Size, 10)
if len(size) > 10 {
return nil, ErrHeaderTooLong
}
// Set unwritten and padding.
arw.uw = header.Size
if header.Size%2 == 0 {
arw.pad = false
} else {
arw.pad = true
}
// Write to strings buffer if extended.
if offset != "" {
_, err := arw.strings.Write([]byte(offset + "\n"))
if err != nil {
return nil, err
}
}
// Add item to symbol table.
if standard {
entry := &entry{Name: name, Offset: int64(arw.buf.Len())}
if offset != "" {
entry.Name = offset
}
arw.symbols = append(arw.symbols, entry)
}
// Add content to fields.
hdr := make([]byte, 60)
arw.fillField(hdr[:16], name)
arw.fillField(hdr[16:28], mod)
arw.fillField(hdr[28:34], uid)
arw.fillField(hdr[34:40], gid)
arw.fillField(hdr[40:48], mode)
arw.fillField(hdr[48:58], size)
arw.fillField(hdr[58:], "`\n")
return hdr, nil
}
// fillUnwritten writes any unwritten bytes and writes the padding byte to w.
func (arw *Writer) fillUnwritten(w io.Writer) error {
fill := make([]byte, arw.uw)
for i := range fill {
fill[i] = ' '
}
if arw.pad {
fill = append(fill, '\n')
}
_, err := w.Write(fill)
return err
}
// fillField writes a string and any padding to a byte slice.
func (arw *Writer) fillField(field []byte, contents string) {
clen := len(contents)
for i := range field {
if i >= clen {
field[i] = ' '
continue
}
field[i] = contents[i]
}
}
// toASCII strips non ascii characters from s.
func toASCII(s string) string {
n := make([]rune, 0)
for _, c := range s {
if c < 0x80 {
n = append(n, c)
}
}
return string(n)
}