-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunpack.go
More file actions
336 lines (284 loc) · 8.14 KB
/
unpack.go
File metadata and controls
336 lines (284 loc) · 8.14 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package tarutil
import (
"archive/tar"
"context"
"io"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"unsafe"
"github.com/pkg/errors"
)
func luTimesNano(path string, ts []syscall.Timespec) error {
var (
_path *byte
err error
)
// These are not currently available in syscall
atFdCwd := -100
atSymLinkNoFollow := 0x100
if _path, err = syscall.BytePtrFromString(path); err != nil {
return err
}
_, _, res := syscall.Syscall6(syscall.SYS_UTIMENSAT, uintptr(atFdCwd), uintptr(unsafe.Pointer(_path)), uintptr(unsafe.Pointer(&ts[0])), uintptr(atSymLinkNoFollow), 0, 0)
if res != 0 && res != syscall.ENOSYS {
return errSyscallNotImplemented
}
return nil
}
func chtimes(name string, atime time.Time, mtime time.Time) error {
unixMinTime := time.Unix(0, 0)
unixMaxTime := maxTime
// If the modified time is prior to the Unix Epoch, or after the
// end of Unix Time, os.Chtimes has undefined behavior
// default to Unix Epoch in this case, just in case
if atime.Before(unixMinTime) || atime.After(unixMaxTime) {
atime = unixMinTime
}
if mtime.Before(unixMinTime) || mtime.After(unixMaxTime) {
mtime = unixMinTime
}
return os.Chtimes(name, atime, mtime)
}
func timeToTimespec(time time.Time) syscall.Timespec {
if time.IsZero() {
// Return UTIME_OMIT special value
ts := syscall.Timespec{
Sec: 0,
Nsec: ((1 << 30) - 2),
}
return ts
}
return syscall.NsecToTimespec(time.UnixNano())
}
func directoryExists(dirPath string) (bool, error) {
fi, err := os.Lstat(dirPath)
if err == nil && !fi.IsDir() {
return false, errors.Wrap(errPathIsNonDirectory, dirPath)
}
if err != nil {
return false, nil
}
return true, nil
}
func createDirectory(destPath string, fi os.FileInfo) error {
if _, err := directoryExists(destPath); err != nil {
return errors.Wrap(errDirectoryExists, destPath)
}
if err := os.Mkdir(destPath, fi.Mode()); err != nil {
return errors.Wrap(errDirectoryCreateFailed, destPath)
}
return nil
}
func createFile(destPath string, fi os.FileInfo, r io.Reader) error {
file, err := os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY, fi.Mode())
if err != nil {
return errors.Wrap(errFailedOpen, destPath)
}
defer file.Close()
if _, err := io.Copy(file, r); err != nil {
return errors.Wrap(errFailedWrite, destPath)
}
return nil
}
func createSymlink(dest, destPath string, header *tar.Header) error {
targetPath := filepath.Join(filepath.Dir(destPath), header.Linkname)
if !strings.HasPrefix(targetPath, dest) {
return errors.Wrap(errInvalidSymlink, header.Linkname)
}
return os.Symlink(header.Linkname, destPath)
}
func mkdev(major, minor int64) uint32 {
return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
}
func createBlockCharFifo(destPath string, header *tar.Header) error {
mode := uint32(header.Mode & 07777)
switch header.Typeflag {
case tar.TypeBlock:
mode |= syscall.S_IFBLK
case tar.TypeChar:
mode |= syscall.S_IFCHR
case tar.TypeFifo:
mode |= syscall.S_IFIFO
}
dev := int(mkdev(header.Devmajor, header.Devminor))
return syscall.Mknod(destPath, mode, dev)
}
func handleWhiteouts(destPath string, unpackedPaths stringMap) error {
base := filepath.Base(destPath)
dir := filepath.Dir(destPath)
walkFn := func(path string, info os.FileInfo, err error) error {
if err != nil {
if os.IsNotExist(err) {
err = nil // parent was deleted
}
return err
}
if path == dir {
return nil
}
if _, exists := unpackedPaths[path]; !exists {
return os.RemoveAll(path)
}
return nil
}
if base == whiteoutOpaqueDir {
if _, err := os.Lstat(dir); err != nil {
return err
}
return filepath.Walk(dir, walkFn)
}
originalBase := base[len(whiteoutPrefix):]
originalPath := filepath.Join(dir, originalBase)
return os.RemoveAll(originalPath)
}
func setPermissions(destPath string, header *tar.Header, options *Options) error {
if options == nil || !options.NoLchown {
if err := os.Lchown(destPath, header.Uid, header.Gid); err != nil {
return err
}
}
headerFi := header.FileInfo()
if header.Typeflag == tar.TypeLink {
fi, err := os.Lstat(header.Linkname)
if err == nil && (fi.Mode()&os.ModeSymlink == 0) {
return os.Chmod(destPath, headerFi.Mode())
}
} else if header.Typeflag != tar.TypeSymlink {
return os.Chmod(destPath, headerFi.Mode())
}
return nil
}
func setMtimeAndAtime(destPath string, header *tar.Header) error {
aTime := header.AccessTime
if aTime.Before(header.ModTime) {
aTime = header.ModTime
}
// system.Chtimes doesn't support a NOFOLLOW flag atm
if header.Typeflag == tar.TypeLink {
fi, err := os.Lstat(header.Linkname)
if err == nil && (fi.Mode()&os.ModeSymlink == 0) {
return chtimes(destPath, aTime, header.ModTime)
}
} else if header.Typeflag != tar.TypeSymlink {
return chtimes(destPath, aTime, header.ModTime)
} else {
ts := []syscall.Timespec{timeToTimespec(aTime), timeToTimespec(header.ModTime)}
return luTimesNano(destPath, ts)
}
return nil
}
func handleTarEntry(targetPaths map[string]string, fullPath, dest string, header *tar.Header, tr io.Reader, options *Options) error {
var err error
fi := header.FileInfo()
switch header.Typeflag {
case tar.TypeDir:
err = createDirectory(fullPath, fi)
case tar.TypeReg, tar.TypeRegA:
if _, ok := targetPaths[header.Name]; ok {
return errors.Wrapf(errInvalidLink, "%q: file already exists", header.Name)
}
err = createFile(fullPath, fi, tr)
targetPaths[header.Name] = fullPath
case tar.TypeLink:
if targ, ok := targetPaths[header.Linkname]; header.Linkname != "" && ok {
if err := os.Link(targ, fullPath); err != nil {
err = errors.Wrapf(errInvalidLink, "%s: %v", header.Name, err.Error)
}
} else {
err = errors.Wrapf(errInvalidLink, "%s: invalid link name", header.Name)
}
case tar.TypeBlock, tar.TypeChar, tar.TypeFifo:
err = createBlockCharFifo(fullPath, header)
case tar.TypeSymlink:
err = createSymlink(dest, fullPath, header)
default:
err = errors.Wrapf(errUnknownHeader, "(type: %c, path: %q)", header.Typeflag, fullPath)
}
if err != nil {
return err
}
err = setPermissions(fullPath, header, options)
if err != nil {
return err
}
return setMtimeAndAtime(fullPath, header)
}
func changeDirTimes(dirs []*tar.Header, dest string) error {
for _, hdr := range dirs {
path := filepath.Join(dest, hdr.Name)
if err := chtimes(path, hdr.AccessTime, hdr.ModTime); err != nil {
return err
}
}
return nil
}
func createDest(dest string) error {
fi, err := os.Lstat(dest)
if os.IsNotExist(err) {
if err := os.MkdirAll(dest, 0700); err != nil {
return errors.Wrap(err, dest)
}
} else if os.IsExist(err) && !fi.IsDir() {
return errors.Wrap(errRead, "destination is not a directory")
} else if err != nil {
return errors.Wrap(err, dest)
}
return nil
}
// Unpack unpacks a tar file into the destination.
func Unpack(ctx context.Context, r io.Reader, dest string, options *Options) error {
if err := createDest(dest); err != nil {
return err
}
tr := tar.NewReader(r)
unpackedPaths := make(stringMap)
targetPaths := map[string]string{}
var dirs []*tar.Header
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return errors.Wrap(errRead, err.Error())
}
fullPath := filepath.Join(dest, hdr.Name)
if hdr.Name != "/" && hdr.Name != "." {
if err := handleTarEntry(targetPaths, fullPath, dest, hdr, tr, options); err != nil {
return err
}
}
if hdr.Typeflag == tar.TypeDir {
dirs = append(dirs, hdr)
}
unpackedPaths[fullPath] = struct{}{}
}
return changeDirTimes(dirs, dest)
}
// OpenAndUnpack unpacks a specified file into the destination.
func OpenAndUnpack(ctx context.Context, layerPath, dest string, options *Options) error {
tarFile, err := os.Open(layerPath)
if err != nil {
return errors.Wrap(errFailedOpen, layerPath)
}
defer tarFile.Close()
return Unpack(ctx, tarFile, dest, options)
}
// OpenAndUnpackMulti unpacks multiple files into the destination.
func OpenAndUnpackMulti(ctx context.Context, layers []string, dest string, options *Options) error {
for i := range layers {
err := OpenAndUnpack(ctx, layers[i], dest, options)
if err != nil {
return err
}
}
return nil
}