Debian Codegraph @

util/testutil/tar.go

raw
1 /*
2 Copyright The containerd Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package testutil
18
19 // This utility helps test codes to generate sample tar blobs.
20
21 import (
22 "archive/tar"
23 "fmt"
24 "io"
25 "os"
26 "strings"
27 "time"
28 )
29
30 // TarEntry is an entry of tar.
31 type TarEntry interface {
32 AppendTar(tw *tar.Writer, opts BuildTarOptions) error
33 }
34
35 // BuildTarOptions is a set of options used during building blob.
36 type BuildTarOptions struct {
37
38 // Prefix is the prefix string need to be added to each file name (e.g. "./", "/", etc.)
39 Prefix string
40 }
41
42 // BuildTarOption is an option used during building blob.
43 type BuildTarOption func(o *BuildTarOptions)
44
45 // WithPrefix is an option to add a prefix string to each file name (e.g. "./", "/", etc.)
46 func WithPrefix(prefix string) BuildTarOption {
47 return func(o *BuildTarOptions) {
48 o.Prefix = prefix
49 }
50 }
51
52 // BuildTar builds a tar blob
53 func BuildTar(ents []TarEntry, opts ...BuildTarOption) io.Reader {
54 var bo BuildTarOptions
55 for _, o := range opts {
56 o(&bo)
57 }
58 pr, pw := io.Pipe()
59 go func() {
60 tw := tar.NewWriter(pw)
61 for _, ent := range ents {
62 if err := ent.AppendTar(tw, bo); err != nil {
63 pw.CloseWithError(err)
64 return
65 }
66 }
67 if err := tw.Close(); err != nil {
68 pw.CloseWithError(err)
69 return
70 }
71 pw.Close()
72 }()
73 return pr
74 }
75
76 type tarEntryFunc func(*tar.Writer, BuildTarOptions) error
77
78 func (f tarEntryFunc) AppendTar(tw *tar.Writer, opts BuildTarOptions) error { return f(tw, opts) }
79
80 // DirectoryBuildTarOption is an option for a directory entry.
81 type DirectoryBuildTarOption func(o *dirOpts)
82
83 type dirOpts struct {
84 uid int
85 gid int
86 xattrs map[string]string
87 mode *os.FileMode
88 modTime time.Time
89 }
90
91 // WithDirModTime specifies the modtime of the dir.
92 func WithDirModTime(modTime time.Time) DirectoryBuildTarOption {
93 return func(o *dirOpts) {
94 o.modTime = modTime
95 }
96 }
97
98 // WithDirOwner specifies the owner of the directory.
99 func WithDirOwner(uid, gid int) DirectoryBuildTarOption {
100 return func(o *dirOpts) {
101 o.uid = uid
102 o.gid = gid
103 }
104 }
105
106 // WithDirXattrs specifies the extended attributes of the directory.
107 func WithDirXattrs(xattrs map[string]string) DirectoryBuildTarOption {
108 return func(o *dirOpts) {
109 o.xattrs = xattrs
110 }
111 }
112
113 // WithDirMode specifies the mode of the directory.
114 func WithDirMode(mode os.FileMode) DirectoryBuildTarOption {
115 return func(o *dirOpts) {
116 o.mode = &mode
117 }
118 }
119
120 // Dir is a directory entry
121 func Dir(name string, opts ...DirectoryBuildTarOption) TarEntry {
122 return tarEntryFunc(func(tw *tar.Writer, buildOpts BuildTarOptions) error {
123 var dOpts dirOpts
124 for _, o := range opts {
125 o(&dOpts)
126 }
127 if !strings.HasSuffix(name, "/") {
128 panic(fmt.Sprintf("missing trailing slash in dir %q ", name))
129 }
130 var mode int64 = 0755
131 if dOpts.mode != nil {
132 mode = permAndExtraMode2TarMode(*dOpts.mode)
133 }
134 return tw.WriteHeader(&tar.Header{
135 Typeflag: tar.TypeDir,
136 Name: buildOpts.Prefix + name,
137 Mode: mode,
138 ModTime: dOpts.modTime,
139 Xattrs: dOpts.xattrs,
140 Uid: dOpts.uid,
141 Gid: dOpts.gid,
142 })
143 })
144 }
145
146 // FileBuildTarOption is an option for a file entry.
147 type FileBuildTarOption func(o *fileOpts)
148
149 type fileOpts struct {
150 uid int
151 gid int
152 xattrs map[string]string
153 mode *os.FileMode
154 modTime time.Time
155 }
156
157 // WithFileOwner specifies the owner of the file.
158 func WithFileOwner(uid, gid int) FileBuildTarOption {
159 return func(o *fileOpts) {
160 o.uid = uid
161 o.gid = gid
162 }
163 }
164
165 // WithFileXattrs specifies the extended attributes of the file.
166 func WithFileXattrs(xattrs map[string]string) FileBuildTarOption {
167 return func(o *fileOpts) {
168 o.xattrs = xattrs
169 }
170 }
171
172 // WithFileModTime specifies the modtime of the file.
173 func WithFileModTime(modTime time.Time) FileBuildTarOption {
174 return func(o *fileOpts) {
175 o.modTime = modTime
176 }
177 }
178
179 // WithFileMode specifies the mode of the file.
180 func WithFileMode(mode os.FileMode) FileBuildTarOption {
181 return func(o *fileOpts) {
182 o.mode = &mode
183 }
184 }
185
186 // File is a regilar file entry
187 func File(name, contents string, opts ...FileBuildTarOption) TarEntry {
188 return tarEntryFunc(func(tw *tar.Writer, buildOpts BuildTarOptions) error {
189 var fOpts fileOpts
190 for _, o := range opts {
191 o(&fOpts)
192 }
193 if strings.HasSuffix(name, "/") {
194 return fmt.Errorf("bogus trailing slash in file %q", name)
195 }
196 var mode int64 = 0644
197 if fOpts.mode != nil {
198 mode = permAndExtraMode2TarMode(*fOpts.mode)
199 }
200 if err := tw.WriteHeader(&tar.Header{
201 Typeflag: tar.TypeReg,
202 Name: buildOpts.Prefix + name,
203 Mode: mode,
204 ModTime: fOpts.modTime,
205 Xattrs: fOpts.xattrs,
206 Size: int64(len(contents)),
207 Uid: fOpts.uid,
208 Gid: fOpts.gid,
209 }); err != nil {
210 return err
211 }
212 _, err := io.WriteString(tw, contents)
213 return err
214 })
215 }
216
217 // Symlink is a symlink entry
218 func Symlink(name, target string) TarEntry {
219 return tarEntryFunc(func(tw *tar.Writer, buildOpts BuildTarOptions) error {
220 return tw.WriteHeader(&tar.Header{
221 Typeflag: tar.TypeSymlink,
222 Name: buildOpts.Prefix + name,
223 Linkname: target,
224 Mode: 0644,
225 })
226 })
227 }
228
229 // Link is a hard-link entry
230 func Link(name, linkname string) TarEntry {
231 now := time.Now()
232 return tarEntryFunc(func(w *tar.Writer, buildOpts BuildTarOptions) error {
233 return w.WriteHeader(&tar.Header{
234 Typeflag: tar.TypeLink,
235 Name: buildOpts.Prefix + name,
236 Linkname: linkname,
237 ModTime: now,
238 AccessTime: now,
239 ChangeTime: now,
240 })
241 })
242 }
243
244 // Chardev is a character device entry
245 func Chardev(name string, major, minor int64) TarEntry {
246 now := time.Now()
247 return tarEntryFunc(func(w *tar.Writer, buildOpts BuildTarOptions) error {
248 return w.WriteHeader(&tar.Header{
249 Typeflag: tar.TypeChar,
250 Name: buildOpts.Prefix + name,
251 Devmajor: major,
252 Devminor: minor,
253 ModTime: now,
254 AccessTime: now,
255 ChangeTime: now,
256 })
257 })
258 }
259
260 // Blockdev is a block device entry
261 func Blockdev(name string, major, minor int64) TarEntry {
262 now := time.Now()
263 return tarEntryFunc(func(w *tar.Writer, buildOpts BuildTarOptions) error {
264 return w.WriteHeader(&tar.Header{
265 Typeflag: tar.TypeBlock,
266 Name: buildOpts.Prefix + name,
267 Devmajor: major,
268 Devminor: minor,
269 ModTime: now,
270 AccessTime: now,
271 ChangeTime: now,
272 })
273 })
274 }
275
276 // Fifo is a fifo entry
277 func Fifo(name string) TarEntry {
278 now := time.Now()
279 return tarEntryFunc(func(w *tar.Writer, buildOpts BuildTarOptions) error {
280 return w.WriteHeader(&tar.Header{
281 Typeflag: tar.TypeFifo,
282 Name: buildOpts.Prefix + name,
283 ModTime: now,
284 AccessTime: now,
285 ChangeTime: now,
286 })
287 })
288 }
289
290 // suid, guid, sticky bits for archive/tar
291 // https://github.com/golang/go/blob/release-branch.go1.13/src/archive/tar/common.go#L607-L609
292 const (
293 cISUID = 04000 // Set uid
294 cISGID = 02000 // Set gid
295 cISVTX = 01000 // Save text (sticky bit)
296 )
297
298 func permAndExtraMode2TarMode(fm os.FileMode) (tm int64) {
299 tm = int64(fm & os.ModePerm)
300 if fm&os.ModeSetuid != 0 {
301 tm |= cISUID
302 }
303 if fm&os.ModeSetgid != 0 {
304 tm |= cISGID
305 }
306 if fm&os.ModeSticky != 0 {
307 tm |= cISVTX
308 }
309 return
310 }