-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar.go
320 lines (268 loc) · 7.46 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
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
// Package ar can read common ar archives. Those are often used in software development tools.
// Even *.deb files are actually a special case of the common ar archive.
// See http://en.wikipedia.org/wiki/Ar_(Unix) for more information on this file format.
package ar
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"os"
"strconv"
"syscall"
"time"
)
const (
magic = "!<arch>\n"
filemagic = "\x60\x0A"
)
type file struct {
name [16]uint8 // Filename in ASCII
}
type fileInfo struct {
name string
mode os.FileMode
size int64
mtime time.Time
}
// IsDir returns always false for ar archive members, because we don't support directories.
func (f *fileInfo) IsDir() bool { return false }
func (f *fileInfo) ModTime() time.Time { return f.mtime }
func (f *fileInfo) Mode() os.FileMode { return f.mode }
func (f *fileInfo) Name() string { return f.name }
func (f *fileInfo) Size() int64 { return f.size }
func (f *fileInfo) Sys() interface{} { return nil }
// Reader can read ar archives
type Reader struct {
buffer *bufio.Reader
valid bool
err error
section io.LimitedReader
hslice []byte
}
// Reset cancels all internal state/buffering and starts to read from in.
// Useful to avoid allocations, but otherwise has the same effect as r := NewReader(in)
func (r *Reader) Reset(in io.Reader) {
r.buffer.Reset(in)
r.valid = false
r.err = nil
r.section.R, r.section.N = nil, 0
}
// NewReader will start parsing a possible archive from r
func NewReader(r io.Reader) *Reader {
reader := &Reader{}
reader.buffer = bufio.NewReader(r)
reader.hslice = make([]byte, 60)
return reader
}
// sticks an error to the reader. From now on this error is returned
// for each following operation until Reset is called.
func (r *Reader) stick(err error) error {
r.err = err
return err
}
func (r *Reader) flushSection() error {
if r.section.R == nil {
panic("flush_section called, but no section present")
}
if r.section.N > 0 {
_, err := io.Copy(ioutil.Discard, &r.section)
return r.stick(err)
}
// skip padding byte.
if c, err := r.buffer.ReadByte(); err != nil {
return r.stick(err)
} else if c != '\n' {
// If it wasn't padding, put it back
r.buffer.UnreadByte()
}
r.section.R, r.section.N = nil, 0
return nil
}
// Next will advance to the next available file in the archive and return it's meta data.
// After calling r.Next, you can use r.Read() to actually read the file contained.
func (r *Reader) Next() (os.FileInfo, error) {
if r.err != nil {
return nil, r.err
}
if !r.valid {
if err := checkMagic(r.buffer); err != nil {
return nil, r.stick(err)
}
r.valid = true
}
if r.section.R != nil {
if err := r.flushSection(); err != nil {
return nil, r.stick(err)
}
}
if _, err := io.ReadFull(r.buffer, r.hslice); err != nil {
return nil, r.stick(err)
}
fi, err := parseFileHeader(r.hslice)
if err != nil {
return nil, r.stick(err)
}
r.section.R, r.section.N = r.buffer, fi.Size()
return fi, nil
}
// Read some bytes from the current archive member
func (r *Reader) Read(b []byte) (n int, err error) {
if r.err != nil {
return 0, r.err
}
if r.section.R != nil {
return r.section.Read(b)
}
return 0, os.ErrNotExist
}
// NotImplementedError will be returned for any features not implemented in this package.
// It means the archive may be valid, but it uses features detected and not (yet) supported by this archive
type NotImplementedError string
func (feature NotImplementedError) Error() string {
return "feature not implemented: " + string(feature)
}
// CorruptArchiveError will be returned, if this archive cannot be parsed.
type CorruptArchiveError string
func (c CorruptArchiveError) Error() string {
return "corrupt archive: " + string(c)
}
func parseFileMode(s string) (filemode os.FileMode, err error) {
mode, err := strconv.ParseUint(s, 8, 32)
if err != nil {
return filemode, CorruptArchiveError(err.Error())
}
if os.FileMode(mode) != (os.FileMode(mode) & (os.ModePerm | syscall.S_IFMT)) {
return filemode, CorruptArchiveError("invalid file mode")
}
switch mode & syscall.S_IFMT {
case 0: // no file type sepcified, assume regular file
case syscall.S_IFREG: // regular file, nothing to add
default:
return filemode, NotImplementedError("non-regular files")
}
return os.FileMode(mode) & os.ModePerm, nil
}
func parseFileHeader(header []byte) (*fileInfo, error) {
if len(header) != 60 {
panic("invalid file header")
}
if header[58] != filemagic[0] || header[59] != filemagic[1] {
return nil, CorruptArchiveError("per file magic not found")
}
name := string(bytes.TrimSpace(header[0:16]))
secs, err := strconv.ParseInt(string(bytes.TrimSpace(header[16:16+12])), 10, 64)
if err != nil {
return nil, CorruptArchiveError(err.Error())
}
filemode, err := parseFileMode(string(bytes.TrimSpace(header[40 : 40+8])))
if err != nil {
return nil, err
}
filesize, err := strconv.ParseInt(string(bytes.TrimSpace(header[48:48+10])), 10, 64)
if err != nil {
return nil, CorruptArchiveError(err.Error())
}
fi := &fileInfo{
name: name,
mtime: time.Unix(secs, 0),
mode: filemode,
size: filesize,
}
return fi, nil
}
func checkMagic(r io.Reader) error {
m := make([]byte, len(magic))
_, err := io.ReadFull(r, m)
if err != nil {
return err
}
if string(m) != magic {
return CorruptArchiveError("global archive header not found")
}
return nil
}
// Writer can write ar archives
type Writer struct {
buffer *bufio.Writer
err error
valid bool
}
// NewWriter returns an archive writer, which writes to w
func NewWriter(w io.Writer) *Writer {
return &Writer{
buffer: bufio.NewWriter(w),
}
}
// sticks an error to the writer. From now on this error is returned
// for each following operation until Reset is called.
func (w *Writer) stick(err error) error {
w.err = err
return err
}
func (w *Writer) writeArchiveHeader() (n int, err error) {
if w.valid {
return 0, nil
}
n, err = w.buffer.WriteString(magic)
if err == nil {
w.valid = true
}
return n, err
}
func (w *Writer) writeFileHeader(meta os.FileInfo) (n int, err error) {
name := meta.Name()
if len(name) > 16 {
return 0, NotImplementedError("file names longer than 16 bytes are not supported")
}
h := make([]byte, 60)
for i := range h {
h[i] = ' '
}
copy(h[0:], name)
copy(h[16:], strconv.FormatInt(meta.ModTime().Unix(), 10))
copy(h[28:], "0")
copy(h[34:], "0")
copy(h[40:], strconv.FormatUint(uint64(meta.Mode()), 8))
copy(h[48:], strconv.FormatInt(meta.Size(), 10))
copy(h[58:], filemagic)
return w.buffer.Write(h)
}
// WriteFile returns how much it has been written or an error, if one occured
func (w *Writer) WriteFile(meta os.FileInfo, r io.Reader) (written int64, err error) {
if w.err != nil {
return 0, w.err
}
n, err := w.writeArchiveHeader()
written += int64(n)
if err != nil {
return written, w.stick(err)
}
n, err = w.writeFileHeader(meta)
written += int64(n)
if err != nil {
return written, w.stick(err)
}
copied, err := io.CopyN(w.buffer, r, meta.Size())
written += int64(copied)
if err != nil {
return written, w.stick(err)
}
// padding on odd offsets in the archive
if written%2 == 1 {
err = w.buffer.WriteByte('\n')
if err != nil {
return written, w.stick(err)
}
written += 1
}
err = w.buffer.Flush()
return written, w.stick(err)
}
// Reset cancels all internal state/buffering and starts writing to out.
// Useful to avoid allocations, but otherwise has the same effect as w := NewWriter(out)
func (w *Writer) Reset(out io.Writer) {
w.buffer.Reset(out)
w.valid = false
w.err = nil
}