-
Notifications
You must be signed in to change notification settings - Fork 44
/
archive.go
216 lines (199 loc) · 4.27 KB
/
archive.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
package desync
import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"time"
)
type Xattrs map[string]string
// NodeDirectory represents a directory in a catar archive
type NodeDirectory struct {
Name string
UID int
GID int
Mode os.FileMode
MTime time.Time
Xattrs Xattrs
}
// NodeFile holds file permissions and data in a catar archive
type NodeFile struct {
UID int
GID int
Mode os.FileMode
Name string
MTime time.Time
Xattrs Xattrs
Size uint64
Data io.Reader
}
// NodeSymlink holds symlink information in a catar archive
type NodeSymlink struct {
Name string
UID int
GID int
Mode os.FileMode
MTime time.Time
Xattrs Xattrs
Target string
}
// NodeDevice holds device information in a catar archive
type NodeDevice struct {
Name string
UID int
GID int
Mode os.FileMode
Major uint64
Minor uint64
Xattrs Xattrs
MTime time.Time
}
// ArchiveDecoder is used to decode a catar archive.
type ArchiveDecoder struct {
d FormatDecoder
dir string
last interface{}
}
// NewArchiveDecoder initializes a decoder for a catar archive.
func NewArchiveDecoder(r io.Reader) ArchiveDecoder {
return ArchiveDecoder{d: NewFormatDecoder(r), dir: "."}
}
// Next returns a node from an archive, or nil if the end is reached. If NodeFile
// is returned, the caller should read the file body before calling Next() again
// as that invalidates the reader.
func (a *ArchiveDecoder) Next() (interface{}, error) {
var (
entry *FormatEntry
payload *FormatPayload
symlink *FormatSymlink
device *FormatDevice
xattrs map[string]string
name string
c interface{}
err error
)
loop:
for {
// First process any elements left over from the last loop before reading
// new ones from the decoder
if a.last != nil {
c = a.last
a.last = nil
} else {
c, err = a.d.Next()
if err != nil {
return nil, err
}
}
switch d := c.(type) {
case FormatEntry:
if entry != nil {
return nil, InvalidFormat{}
}
entry = &d
case FormatUser: // Not supported yet
case FormatGroup:
case FormatSELinux:
case FormatACLUser:
case FormatACLGroup:
case FormatACLGroupObj:
case FormatACLDefault:
case FormatFCaps:
case FormatPayload:
if entry == nil {
return nil, InvalidFormat{}
}
payload = &d
break loop
case FormatXAttr:
idx := strings.IndexRune(d.NameAndValue, '\000')
if entry == nil || idx == -1 {
return nil, InvalidFormat{}
}
if xattrs == nil {
xattrs = make(map[string]string)
}
xattrs[d.NameAndValue[0:idx]] = d.NameAndValue[idx+1:]
case FormatSymlink:
if entry == nil {
return nil, InvalidFormat{}
}
symlink = &d
case FormatDevice:
if entry == nil {
return nil, InvalidFormat{}
}
device = &d
case FormatFilename:
if entry != nil { // Store and come back to it in the next iteration
a.last = c
break loop
}
name = d.Name
case FormatGoodbye: // This will effectively be a "cd .."
if entry != nil {
a.last = c
break loop
}
a.dir = filepath.Dir(a.dir)
case nil:
return nil, nil
default:
return nil, fmt.Errorf("unsupported element %s in archive", reflect.TypeOf(d))
}
}
// If it doesn't have a payload or is a device/symlink, it must be a directory
if payload == nil && device == nil && symlink == nil {
a.dir = path.Join(a.dir, name)
return NodeDirectory{
Name: a.dir,
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
}, nil
}
// Regular file
if payload != nil {
return NodeFile{
Name: path.Join(a.dir, name),
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
Size: payload.Size - 16,
Data: payload.Data,
}, nil
}
// Device
if device != nil {
return NodeDevice{
Name: path.Join(a.dir, name),
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
Major: device.Major,
Minor: device.Minor,
}, nil
}
// Symlink
if symlink != nil {
return NodeSymlink{
Name: path.Join(a.dir, name),
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Xattrs: xattrs,
Target: symlink.Target,
}, nil
}
return nil, nil
}