forked from paultag/go-debian
/
ar.go
184 lines (151 loc) · 4.83 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
/* {{{ Copyright (c) Paul R. Tagliamonte <paultag@debian.org>, 2015
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. }}} */
package deb // import "github.com/systemos-linux/go-debian/deb"
import (
"fmt"
"io"
"strconv"
"strings"
)
// ArEntry {{{
// Container type to access the different parts of a Debian `ar(1)` Archive.
//
// The most interesting parts of this are the `Name` attribute, Data
// `io.Reader`, and the Tarfile helpers. This will allow the developer to
// programmatically inspect the information inside without forcing her to
// unpack the .deb to the filesystem.
type ArEntry struct {
Name string
Timestamp int64
OwnerID int64
GroupID int64
FileMode string
Size int64
Data *io.SectionReader
}
// }}}
// Ar {{{
// This struct encapsulates a Debian .deb flavored `ar(1)` archive.
type Ar struct {
in io.ReaderAt
offset int64
}
// LoadAr {{{
// Load an Ar archive reader from an io.ReaderAt
func LoadAr(in io.ReaderAt) (*Ar, error) {
offset, err := checkAr(in)
if err != nil {
return nil, err
}
debFile := Ar{in: in, offset: offset}
return &debFile, nil
}
// }}}
// Next {{{
// Function to jump to the next file in the Debian `ar(1)` archive, and
// return the next member.
func (d *Ar) Next() (*ArEntry, error) {
line := make([]byte, 60)
count, err := d.in.ReadAt(line, d.offset)
if err != nil {
return nil, err
}
if count == 1 && line[0] == '\n' {
return nil, io.EOF
}
if count != 60 {
return nil, fmt.Errorf("Caught a short read at the end")
}
entry, err := parseArEntry(line)
if err != nil {
return nil, err
}
entry.Data = io.NewSectionReader(d.in, d.offset+int64(count), entry.Size)
d.offset += int64(count) + entry.Size + (entry.Size % 2)
return entry, nil
}
// }}}
// toDecimal {{{
// Take a byte array, and return an int64
func toDecimal(input []byte) (int64, error) {
stream := strings.TrimSpace(string(input))
out, err := strconv.Atoi(stream)
return int64(out), err
}
// }}}
// }}}
// AR Format Hackery {{{
// parseArEntry {{{
// Take the AR format line, and create an ArEntry (without .Data set)
// to be returned to the user later.
//
// +-------------------------------------------------------
// | Offset Length Name Format
// +-------------------------------------------------------
// | 0 16 File name ASCII
// | 16 12 File modification timestamp Decimal
// | 28 6 Owner ID Decimal
// | 34 6 Group ID Decimal
// | 40 8 File mode Octal
// | 48 10 File size in bytes Decimal
// | 58 2 File magic 0x60 0x0A
//
func parseArEntry(line []byte) (*ArEntry, error) {
if len(line) != 60 {
return nil, fmt.Errorf("Malformed file entry line length")
}
if line[58] != 0x60 && line[59] != 0x0A {
return nil, fmt.Errorf("Malformed file entry line endings")
}
entry := ArEntry{
Name: strings.TrimSuffix(strings.TrimSpace(string(line[0:16])), "/"),
FileMode: strings.TrimSpace(string(line[48:58])),
}
for target, value := range map[*int64][]byte{
&entry.Timestamp: line[16:28],
&entry.OwnerID: line[28:34],
&entry.GroupID: line[34:40],
&entry.Size: line[48:58],
} {
intValue, err := toDecimal(value)
if err != nil {
return nil, err
}
*target = intValue
}
return &entry, nil
}
// }}}
// checkAr {{{
// Given a brand spank'n new os.File entry, go ahead and make sure it looks
// like an `ar(1)` archive, and not some random file.
func checkAr(reader io.ReaderAt) (int64, error) {
header := make([]byte, 8)
if _, err := reader.ReadAt(header, 0); err != nil {
return 0, err
}
if string(header) != "!<arch>\n" {
return 0, fmt.Errorf("Header doesn't look right!")
}
return int64(len(header)), nil
}
// }}}
// }}}
// vim: foldmethod=marker