-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathunpack.go
More file actions
202 lines (174 loc) · 4.69 KB
/
unpack.go
File metadata and controls
202 lines (174 loc) · 4.69 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
package runtime
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/containers/image/types"
"github.com/pkg/errors"
)
func UnpackImage(src types.Image, target string) error {
ref := src.Reference()
unpackLayer, err := getLayerUnpacker(ref)
if err != nil {
return err
}
raw, err := ref.NewImageSource(nil)
if err != nil {
return err
}
for _, layer := range src.LayerInfos() {
rc, _, err := raw.GetBlob(layer)
if err != nil {
return err
}
if err := unpackLayer(target, rc); err != nil {
return err
}
}
return nil
}
func getLayerUnpacker(ref types.ImageReference) (func(string, io.Reader) error, error) {
transport := ref.Transport().Name()
switch transport {
case "docker-daemon":
return untar, nil
case "docker":
return untarGzip, nil
default:
return nil, fmt.Errorf("unsupported transport: %s", transport)
}
}
func untarGzip(dest string, r io.Reader) error {
gz, err := gzip.NewReader(r)
if err != nil {
return errors.Wrap(err, "error creating gzip reader")
}
defer gz.Close()
return untar(dest, gz)
}
func untar(dest string, r io.Reader) error {
entries := make(map[string]bool)
var dirs []*tar.Header
tr := tar.NewReader(r)
loop:
for {
hdr, err := tr.Next()
switch err {
case io.EOF:
break loop
case nil:
// success, continue below
default:
return errors.Wrapf(err, "error advancing tar stream")
}
hdr.Name = filepath.Clean(hdr.Name)
if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
// Not the root directory, ensure that the parent directory exists
parent := filepath.Dir(hdr.Name)
parentPath := filepath.Join(dest, parent)
if _, err2 := os.Lstat(parentPath); err2 != nil && os.IsNotExist(err2) {
if err3 := os.MkdirAll(parentPath, 0755); err3 != nil {
return err3
}
}
}
path := filepath.Join(dest, hdr.Name)
if entries[path] {
return fmt.Errorf("duplicate entry for %s", path)
}
entries[path] = true
rel, err := filepath.Rel(dest, path)
if err != nil {
return err
}
info := hdr.FileInfo()
if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
return fmt.Errorf("%q is outside of %q", hdr.Name, dest)
}
if strings.HasPrefix(info.Name(), ".wh.") {
path = strings.Replace(path, ".wh.", "", 1)
if err := os.RemoveAll(path); err != nil {
return errors.Wrap(err, "unable to delete whiteout path")
}
continue loop
}
switch hdr.Typeflag {
case tar.TypeDir:
if fi, err := os.Lstat(path); !(err == nil && fi.IsDir()) {
if err2 := os.MkdirAll(path, info.Mode()); err2 != nil {
return errors.Wrap(err2, "error creating directory")
}
}
case tar.TypeReg, tar.TypeRegA:
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, info.Mode())
if err != nil {
return errors.Wrap(err, "unable to open file")
}
if _, err := io.Copy(f, tr); err != nil {
f.Close()
return errors.Wrap(err, "unable to copy")
}
f.Close()
case tar.TypeLink:
target := filepath.Join(dest, hdr.Linkname)
trueTarget, err := filepath.EvalSymlinks(target)
if err != nil {
return err
}
if !strings.HasPrefix(trueTarget, dest) {
return fmt.Errorf("hardlink %q -> %q outside destination", target, hdr.Linkname)
}
if !strings.HasPrefix(target, dest) {
return fmt.Errorf("invalid hardlink %q -> %q", target, hdr.Linkname)
}
if err := os.Link(target, path); err != nil {
return err
}
case tar.TypeSymlink:
target := filepath.Join(filepath.Dir(path), hdr.Linkname)
trueTarget, err := filepath.EvalSymlinks(target)
if err != nil {
return err
}
if !strings.HasPrefix(trueTarget, dest) {
return fmt.Errorf("hardlink %q -> %q outside destination", target, hdr.Linkname)
}
if !strings.HasPrefix(target, dest) {
return fmt.Errorf("invalid symlink %q -> %q", path, hdr.Linkname)
}
err := os.Symlink(hdr.Linkname, path)
if err != nil {
if os.IsExist(err) {
if err := os.Remove(path); err != nil {
return err
}
if err := os.Symlink(hdr.Linkname, path); err != nil {
return err
}
}
}
case tar.TypeXGlobalHeader:
return nil
}
// Directory mtimes must be handled at the end to avoid further
// file creation in them to modify the directory mtime
if hdr.Typeflag == tar.TypeDir {
dirs = append(dirs, hdr)
}
}
for _, hdr := range dirs {
path := filepath.Join(dest, hdr.Name)
finfo := hdr.FileInfo()
// I believe the old version was using time.Now().UTC() to overcome an
// invalid error from chtimes.....but here we lose hdr.AccessTime like this...
if err := os.Chtimes(path, time.Now().UTC(), finfo.ModTime()); err != nil {
return errors.Wrap(err, "error changing time")
}
}
return nil
}