-
Notifications
You must be signed in to change notification settings - Fork 199
/
archive.go
243 lines (196 loc) · 5.57 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
package true_git
import (
"archive/tar"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"github.com/satori/go.uuid"
"github.com/flant/werf/pkg/logger"
)
type ArchiveOptions struct {
Commit string
PathFilter PathFilter
}
type ArchiveDescriptor struct {
Type ArchiveType
IsEmpty bool
}
type ArchiveType string
const (
FileArchive ArchiveType = "file"
DirectoryArchive ArchiveType = "directory"
)
func ArchiveWithSubmodules(out io.Writer, gitDir, workTreeDir string, opts ArchiveOptions) (*ArchiveDescriptor, error) {
return writeArchive(out, gitDir, workTreeDir, true, opts)
}
func Archive(out io.Writer, gitDir, workTreeDir string, opts ArchiveOptions) (*ArchiveDescriptor, error) {
return writeArchive(out, gitDir, workTreeDir, false, opts)
}
func debugArchive() bool {
return os.Getenv("WERF_TRUE_GIT_DEBUG_ARCHIVE") == "1"
}
func writeArchive(out io.Writer, gitDir, workTreeDir string, withSubmodules bool, opts ArchiveOptions) (*ArchiveDescriptor, error) {
var err error
gitDir, err = filepath.Abs(gitDir)
if err != nil {
return nil, fmt.Errorf("bad git dir `%s`: %s", gitDir, err)
}
workTreeDir, err = filepath.Abs(workTreeDir)
if err != nil {
return nil, fmt.Errorf("bad work tree dir `%s`: %s", workTreeDir, err)
}
if withSubmodules {
err := checkSubmoduleConstraint()
if err != nil {
return nil, err
}
}
err = switchWorkTree(gitDir, workTreeDir, opts.Commit)
if err != nil {
return nil, fmt.Errorf("cannot reset work tree `%s` to commit `%s`: %s", workTreeDir, opts.Commit, err)
}
if withSubmodules {
var err error
err = deinitSubmodules(gitDir, workTreeDir)
if err != nil {
return nil, fmt.Errorf("cannot deinit submodules: %s", err)
}
err = syncSubmodules(gitDir, workTreeDir)
if err != nil {
return nil, fmt.Errorf("cannot sync submodules: %s", err)
}
err = updateSubmodules(gitDir, workTreeDir)
if err != nil {
return nil, fmt.Errorf("cannot update submodules: %s", err)
}
}
desc := &ArchiveDescriptor{
IsEmpty: true,
}
tw := tar.NewWriter(out)
err = filepath.Walk(workTreeDir, func(absPath string, info os.FileInfo, accessErr error) error {
if accessErr != nil {
return fmt.Errorf("error accessing `%s`: %s", absPath, accessErr)
}
baseName := filepath.Base(absPath)
for _, p := range []string{".git"} {
if baseName == p {
return nil
}
}
var path string
if absPath == workTreeDir {
path = "."
} else {
path = strings.TrimPrefix(absPath, NormalizeDirectoryPrefix(workTreeDir))
}
if NormalizeAbsolutePath(path) == NormalizeAbsolutePath(opts.PathFilter.BasePath) {
if info.IsDir() {
desc.Type = DirectoryArchive
if debugArchive() {
fmt.Printf("Found BasePath `%s` directory: directory archive type\n", path)
}
} else {
desc.Type = FileArchive
if debugArchive() {
fmt.Printf("Found BasePath `%s` file: file archive\n", path)
}
}
}
if info.IsDir() {
return nil
}
if !opts.PathFilter.IsFilePathValid(path) {
if debugArchive() {
fmt.Printf("Excluded path `%s` by path filter %s\n", path, opts.PathFilter.String())
}
return nil
}
archivePath := opts.PathFilter.TrimFileBasePath(path)
desc.IsEmpty = false
if info.Mode()&os.ModeSymlink != 0 {
linkname, err := os.Readlink(absPath)
if err != nil {
return fmt.Errorf("cannot read symlink `%s`: %s", absPath, err)
}
err = tw.WriteHeader(&tar.Header{
Format: tar.FormatGNU,
Typeflag: tar.TypeSymlink,
Name: archivePath,
Linkname: string(linkname),
Mode: int64(info.Mode()),
Size: info.Size(),
ModTime: info.ModTime(),
AccessTime: info.ModTime(),
ChangeTime: info.ModTime(),
})
if err != nil {
return fmt.Errorf("unable to write tar symlink header for file `%s`: %s", archivePath, err)
}
if debugArchive() {
fmt.Printf("Added archive symlink `%s` -> `%s`\n", path, linkname)
}
return nil
}
err = tw.WriteHeader(&tar.Header{
Format: tar.FormatGNU,
Name: archivePath,
Mode: int64(info.Mode()),
Size: info.Size(),
ModTime: info.ModTime(),
AccessTime: info.ModTime(),
ChangeTime: info.ModTime(),
})
if err != nil {
return fmt.Errorf("unable to write tar header for file `%s`: %s", archivePath, err)
}
file, err := os.Open(absPath)
if err != nil {
return fmt.Errorf("unable to open file `%s`: %s", absPath, err)
}
_, err = io.Copy(tw, file)
if err != nil {
return fmt.Errorf("unable to write data to tar archive from file `%s`: %s", path, err)
}
err = file.Close()
if err != nil {
return fmt.Errorf("error closing file `%s`: %s", absPath, err)
}
if debugArchive() {
logger.LogServiceF("Added archive file '%s'\n", path)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("entries iteration failed in `%s`: %s", workTreeDir, err)
}
err = tw.Close()
if err != nil {
return nil, fmt.Errorf("cannot write tar archive: %s", err)
}
if desc.Type == "" {
return nil, fmt.Errorf("base path `%s` entry not found repo", opts.PathFilter.BasePath)
}
return desc, nil
}
func startMemprofile() {
runtime.MemProfileRate = 1
}
func stopMemprofile() {
memprofilePath := fmt.Sprintf("/tmp/create-tar-memprofile-%s", uuid.NewV4())
fmt.Fprintf(outStream, "Creating mem profile: %s\n", memprofilePath)
f, err := os.Create(memprofilePath)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
f.Close()
}