This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive.go
187 lines (171 loc) · 3.41 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
package desync
import (
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"time"
)
type NodeDirectory struct {
Name string
UID int
GID int
Mode os.FileMode
MTime time.Time
}
type NodeFile struct {
UID int
GID int
Mode os.FileMode
Name string
MTime time.Time
Data io.Reader
}
type NodeSymlink struct {
Name string
UID int
GID int
Mode os.FileMode
MTime time.Time
Target string
}
type NodeDevice struct {
Name string
UID int
GID int
Mode os.FileMode
Major uint64
Minor uint64
MTime time.Time
}
type ArchiveDecoder struct {
d FormatDecoder
dir string
last interface{}
}
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
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 FormatXAttr:
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 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 = filepath.Join(a.dir, name)
return NodeDirectory{
Name: a.dir,
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
}, nil
}
// Regular file
if payload != nil {
return NodeFile{
Name: filepath.Join(a.dir, name),
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Data: payload.Data,
}, nil
}
// Device
if device != nil {
return NodeDevice{
Name: filepath.Join(a.dir, name),
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Major: device.Major,
Minor: device.Minor,
}, nil
}
// Symlink
if symlink != nil {
return NodeSymlink{
Name: filepath.Join(a.dir, name),
UID: entry.UID,
GID: entry.GID,
Mode: entry.Mode,
MTime: entry.MTime,
Target: symlink.Target,
}, nil
}
return nil, nil
}