/
archive.go
301 lines (244 loc) · 7.4 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package util
import (
"archive/tar"
"context"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/filemode"
"github.com/go-git/go-git/v5/plumbing/format/index"
"github.com/werf/logboek"
)
func CreateArchiveBasedOnAnotherOne(ctx context.Context, sourceArchivePath, destinationArchivePath string, pathsToExclude []string, f func(tw *tar.Writer) error) error {
return CreateArchive(destinationArchivePath, func(tw *tar.Writer) error {
source, err := os.Open(sourceArchivePath)
if err != nil {
return fmt.Errorf("unable to open %q: %s", sourceArchivePath, err)
}
defer source.Close()
tr := tar.NewReader(source)
ArchiveCopying:
for {
hdr, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("unable to read archive %q: %s", sourceArchivePath, err)
}
for _, pathToExclude := range pathsToExclude {
if hdr.Name == filepath.ToSlash(pathToExclude) {
if debugArchiveUtil() {
logboek.Context(ctx).Debug().LogF("Source archive file was excluded: %q\n", hdr.Name)
}
continue ArchiveCopying
}
}
if err := tw.WriteHeader(hdr); err != nil {
return fmt.Errorf("unable to write header %q from %q archive to %q: %s", hdr.Name, sourceArchivePath, destinationArchivePath, err)
}
if _, err := io.Copy(tw, tr); err != nil {
return fmt.Errorf("unable to copy file %q from %q archive to %q: %s", hdr.Name, sourceArchivePath, destinationArchivePath, err)
}
if debugArchiveUtil() {
logboek.Context(ctx).Debug().LogF("Source archive file was added: %q\n", hdr.Name)
}
}
return f(tw)
})
}
func CreateArchive(archivePath string, f func(tw *tar.Writer) error) error {
if err := os.MkdirAll(filepath.Dir(archivePath), 0o777); err != nil {
return fmt.Errorf("unable to create dir %q: %s", filepath.Dir(archivePath), err)
}
file, err := os.Create(archivePath)
if err != nil {
return fmt.Errorf("unable to create %q: %s", archivePath, err)
}
defer file.Close()
tw := tar.NewWriter(file)
defer tw.Close()
return f(tw)
}
func CopyFileIntoTar(tw *tar.Writer, tarEntryName string, filePath string) error {
stat, err := os.Lstat(filePath)
if err != nil {
return fmt.Errorf("unable to stat file %q: %s", filePath, err)
}
if stat.Mode().IsDir() {
return fmt.Errorf("directory %s cannot be added to tar archive", filePath)
}
header := &tar.Header{
Name: tarEntryName,
Mode: int64(stat.Mode()),
Size: stat.Size(),
ModTime: stat.ModTime(),
AccessTime: stat.ModTime(),
ChangeTime: stat.ModTime(),
}
if stat.Mode()&os.ModeSymlink != 0 {
linkname, err := os.Readlink(filePath)
if err != nil {
return fmt.Errorf("unable to read link %q: %s", filePath, err)
}
header.Linkname = linkname
header.Typeflag = tar.TypeSymlink
}
if err := tw.WriteHeader(header); err != nil {
return fmt.Errorf("unable to write tar header for file %s: %s", tarEntryName, err)
}
if stat.Mode().IsRegular() {
f, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("unable to open file %q: %s", filePath, err)
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return nil
}
if _, err := tw.Write(data); err != nil {
return fmt.Errorf("unable to write data to tar archive from file %q: %s", filePath, err)
}
}
return nil
}
func CopyGitIndexEntryIntoTar(tw *tar.Writer, tarEntryName string, entry *index.Entry, obj plumbing.EncodedObject) error {
r, err := obj.Reader()
if err != nil {
return err
}
header := &tar.Header{
Name: tarEntryName,
Mode: int64(entry.Mode),
Size: int64(entry.Size),
ModTime: entry.ModifiedAt,
AccessTime: entry.ModifiedAt,
ChangeTime: entry.ModifiedAt,
}
if entry.Mode == filemode.Symlink {
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
linkname := string(data)
header.Linkname = linkname
header.Typeflag = tar.TypeSymlink
}
if err := tw.WriteHeader(header); err != nil {
return fmt.Errorf("unable to write tar header for git index entry %s: %s", tarEntryName, err)
}
if entry.Mode.IsFile() {
if _, err := io.Copy(tw, r); err != nil {
return fmt.Errorf("unable to write data to tar for git index entry %q: %s", tarEntryName, err)
}
}
return nil
}
func ExtractTar(tarFileReader io.Reader, dstDir string) error {
tarReader := tar.NewReader(tarFileReader)
for {
tarEntryHeader, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("unable to Next() while extracting tar: %s", err)
}
tarEntryPath := filepath.Join(dstDir, tarEntryHeader.Name)
tarEntryFileInfo := tarEntryHeader.FileInfo()
switch tarEntryHeader.Typeflag {
case tar.TypeDir:
if err = os.MkdirAll(tarEntryPath, tarEntryFileInfo.Mode()); err != nil {
return fmt.Errorf("unable to create new dir %q while extracting tar: %s", tarEntryPath, err)
}
case tar.TypeReg, tar.TypeSymlink, tar.TypeLink, tar.TypeGNULongName, tar.TypeGNULongLink:
if err := os.MkdirAll(filepath.Dir(tarEntryPath), os.ModePerm); err != nil {
return fmt.Errorf("unable to create new directory %q while extracting tar: %w", filepath.Dir(tarEntryPath), err)
}
file, err := os.OpenFile(tarEntryPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, tarEntryFileInfo.Mode())
if err != nil {
return fmt.Errorf("unable to create new file %q while extracting tar: %s", tarEntryPath, err)
}
defer file.Close()
_, err = io.Copy(file, tarReader)
if err != nil {
return err
}
default:
return fmt.Errorf("tar entry %q of unexpected type: %b", tarEntryHeader.Name, tarEntryHeader.Typeflag)
}
}
return nil
}
func WriteDirAsTar(dir string, w io.Writer) error {
tarWriter := tar.NewWriter(w)
err := filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error accessing %q: %s", path, err)
}
relPath, err := filepath.Rel(dir, path)
if err != nil {
return err
}
if debugArchiveUtil() {
fmt.Printf("filepath.Walk %q\n", relPath)
}
if info.Mode().IsDir() {
header := &tar.Header{
Name: relPath,
Size: info.Size(),
Mode: int64(info.Mode()),
ModTime: info.ModTime(),
Typeflag: tar.TypeDir,
}
err = tarWriter.WriteHeader(header)
if err != nil {
return fmt.Errorf("could not tar write header for %q: %s", path, err)
}
if debugArchiveUtil() {
fmt.Printf("Written dir %q\n", relPath)
}
return nil
}
header := &tar.Header{
Name: relPath,
Size: info.Size(),
Mode: int64(info.Mode()),
ModTime: info.ModTime(),
Typeflag: tar.TypeReg,
}
err = tarWriter.WriteHeader(header)
if err != nil {
return fmt.Errorf("could not tar write header for %q: %s", path, err)
}
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("unable to open %q: %s", path, err)
}
n, err := io.Copy(tarWriter, file)
if err != nil {
return fmt.Errorf("unable to write %q into tar: %s", path, err)
}
if err := file.Close(); err != nil {
return fmt.Errorf("unable to close %q: %s", path, err)
}
if debugArchiveUtil() {
fmt.Printf("Written file %q (%d bytes)\n", relPath, n)
}
return nil
})
if err != nil {
return err
}
if err := tarWriter.Close(); err != nil {
return fmt.Errorf("unable to close tar writer: %s", err)
}
return nil
}
func debugArchiveUtil() bool {
return os.Getenv("WERF_DEBUG_ARCHIVE_UTIL") == "1"
}