-
Notifications
You must be signed in to change notification settings - Fork 5
/
archive.go
285 lines (238 loc) · 5.54 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
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
package main
import (
"encoding/binary"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"syscall"
"golang.org/x/sys/unix"
)
var zeroes = make([]byte, cowAlignment)
func (c *car) writeTag(tagType, length uint16, out *os.File, data any) error {
t := tag{
Tag: tagType,
Length: length,
}
err := binary.Write(out, binary.BigEndian, &t)
if err != nil {
return err
}
if data != nil {
return binary.Write(out, binary.BigEndian, data)
}
return nil
}
func (c *car) writeData(out *os.File, e entry) error {
if e.size == 0 || e.Mode&unix.S_IFMT != unix.S_IFREG {
return c.writeTag(tagData, 0, out, nil)
}
pd := paddedData{
Size: e.size,
}
// To add padding, we need to know the current offset, so the file must be seekable
if offset, err := out.Seek(0, io.SeekCurrent); err == nil {
// tag + paddedData
const overhead = 4 + 12
newDataOffset := round4k(uint64(offset + overhead))
padding := newDataOffset - uint64(offset+overhead)
pd.Padding = uint32(padding)
err = c.writeTag(tagData, 12, out, &pd)
if err != nil {
return err
}
_, err = out.Seek(int64(padding), io.SeekCurrent)
if err != nil {
return err
}
} else {
err = c.writeTag(tagData, 12, out, &pd)
if err != nil {
return err
}
}
in, err := os.Open(e.localName)
if err != nil {
return err
}
defer in.Close()
err = reflinkToArchive(in, out, e.size)
if err != nil && !errors.Is(err, reflinkError) {
return err
}
_, err = io.Copy(out, in)
return err
}
func (c *car) writeHeader(out *os.File, e entry) error {
_, err := out.Write([]byte(cowMagic))
if err != nil {
fmt.Fprintln(os.Stderr, "Write error:", err)
return err
}
err = c.writeTag(tagHeader, uint16(binary.Size(e.fixedData)), out, &e.fixedData)
if err != nil {
return err
}
err = c.writeTag(tagName, uint16(len(e.name)), out, []byte(e.name))
if err != nil {
fmt.Fprintln(os.Stderr, "Write error:", err)
return err
}
if e.Mode&unix.S_IFMT == unix.S_IFLNK {
err = c.writeTag(tagLinkTarget, uint16(len(e.link)), out, []byte(e.link))
if err != nil {
return err
}
}
if e.Mode&unix.S_IFMT == unix.S_IFCHR || e.Mode&unix.S_IFMT == unix.S_IFBLK {
err = c.writeTag(tagDevice, 4, out, e.dev)
if err != nil {
return err
}
}
return c.writeData(out, e)
}
func unixMode(mode fs.FileMode) uint32 {
var unixMode uint32
switch {
case mode.IsRegular():
unixMode = unix.S_IFREG
case mode.IsDir():
unixMode = unix.S_IFDIR
case mode&fs.ModeSymlink != 0:
unixMode = unix.S_IFLNK
case mode&fs.ModeNamedPipe != 0:
unixMode = unix.S_IFIFO
/* fs.ModeCharDevice bit is contained also in fs.ModeDevice, so order is important */
case mode&fs.ModeCharDevice != 0:
unixMode = unix.S_IFCHR
case mode&fs.ModeDevice != 0:
unixMode = unix.S_IFBLK
case mode&fs.ModeSocket != 0:
unixMode = unix.S_IFSOCK
}
if mode&fs.ModeSetuid != 0 {
unixMode |= unix.S_ISUID
}
if mode&fs.ModeSetgid != 0 {
unixMode |= unix.S_ISGID
}
if mode&fs.ModeSticky != 0 {
unixMode |= unix.S_ISVTX
}
return unixMode | uint32(mode.Perm())
}
func (c *car) walker(strip int, p string, statinfo fs.FileInfo, err error, outFd *os.File) error {
if c.verbose {
fmt.Fprintln(c.infoFd, p)
}
if err != nil {
fmt.Fprintln(os.Stderr, "Error walking", p, err)
return err
}
info := statinfo.Mode()
switch {
case info&fs.ModeSocket != 0:
fallthrough
case info&fs.ModeIrregular != 0:
fmt.Fprintln(os.Stderr, "Skipping", p)
return nil
}
storedName := p[strip:]
if storedName[0] == '/' {
storedName = storedName[1:]
}
entry := entry{
fixedData: fixedData{
Mode: unixMode(info),
Mtime: statinfo.ModTime().UnixNano(),
},
name: storedName,
localName: p,
}
if sys, ok := statinfo.Sys().(*syscall.Stat_t); ok {
entry.Uid = sys.Uid
entry.Gid = sys.Gid
entry.dev = uint32(sys.Rdev)
}
switch {
case info&fs.ModeNamedPipe != 0:
break
case info&fs.ModeDevice != 0:
fallthrough
case info&fs.ModeCharDevice != 0:
break
case info.IsRegular():
entry.size = uint64(statinfo.Size())
case info.IsDir():
case info&fs.ModeSymlink != 0:
entry.link, err = os.Readlink(p)
if err != nil {
fmt.Fprintln(os.Stderr, "Error reading symlink", p, err)
return nil
}
}
err = c.writeHeader(outFd, entry)
if err != nil {
return err
}
return nil
}
func (c *car) walkPaths(paths []string, outFd *os.File) error {
for _, dir := range paths {
dir = filepath.Clean(dir)
err := filepath.Walk(dir, func(p string, i fs.FileInfo, err error) error {
topdir := filepath.Dir(dir)
if topdir == "." {
topdir = ""
}
return c.walker(len(topdir), p, i, err, outFd)
})
if err != nil {
return err
}
}
return nil
}
func (c *car) archive(paths []string, outFile string) error {
var err error
outFd := os.Stdout
c.infoFd = os.Stderr
if outFile != "" {
outFd, err = os.Create(outFile)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating output file", outFile, err)
return err
}
defer outFd.Close()
c.infoFd = os.Stdout
}
_, err = outFd.Seek(0, io.SeekCurrent)
if err == nil {
c.seekable = true
} else {
fmt.Fprintln(os.Stderr, "Warning: archive is not seekable, padding will be disabled")
}
err = c.walkPaths(paths, outFd)
if err != nil {
return err
}
_, err = outFd.Write([]byte(cowEnd))
if err != nil {
return err
}
if c.seekable {
offset, err := outFd.Seek(0, io.SeekEnd)
if err != nil {
return err
}
err = outFd.Truncate(int64(round4k(uint64(offset))))
} else {
// As we don't know the cursor position, write the maximum alignment
buf := make([]byte, cowAlignment)
_, err = outFd.Write(buf)
}
return err
}