This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
package_bundle.go
153 lines (139 loc) · 4 KB
/
package_bundle.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
package bitsgo
import (
"archive/zip"
"crypto/sha1"
"encoding/hex"
"io"
"io/ioutil"
"os"
"strconv"
"time"
"go.uber.org/zap"
"github.com/pkg/errors"
"github.com/cenkalti/backoff"
)
func CreateTempZipFileFrom(bundlesPayload []Fingerprint,
zipReader *zip.Reader,
minimumSize, maximumSize uint64,
blobstore Blobstore,
metricsService MetricsService,
logger *zap.SugaredLogger,
) (tempFilename string, err error) {
tempZipFile, e := ioutil.TempFile("", "bundles")
if e != nil {
return "", errors.Wrap(e, "Could not create temp file")
}
defer func() {
if err != nil {
os.Remove(tempZipFile.Name())
}
}()
defer tempZipFile.Close()
zipWriter := zip.NewWriter(tempZipFile)
if zipReader != nil {
for _, zipInputFileEntry := range zipReader.File {
if !zipInputFileEntry.FileInfo().Mode().IsRegular() {
continue
}
zipFileEntryWriter, e := zipWriter.CreateHeader(zipEntryHeaderWithModifiedTime(zipInputFileEntry.Name, zipInputFileEntry.FileInfo().Mode(), zipInputFileEntry.FileHeader.Modified))
if e != nil {
return "", errors.Wrap(e, "Could not create header in zip file")
}
zipEntryReader, e := zipInputFileEntry.Open()
if e != nil {
return "", errors.Wrap(e, "Could not open zip file entry")
}
defer zipEntryReader.Close()
tempFile, e := ioutil.TempFile("", "app-stash")
if e != nil {
return "", errors.Wrap(e, "Could not create tempfile")
}
defer os.Remove(tempFile.Name())
defer tempFile.Close()
sha := sha1.New()
tempFileSize, e := io.Copy(io.MultiWriter(zipFileEntryWriter, tempFile, sha), zipEntryReader)
if e != nil {
return "", errors.Wrap(e, "Could not copy content from zip entry")
}
e = tempFile.Close()
if e != nil {
return "", errors.Wrap(e, "Could not close temp file")
}
e = zipEntryReader.Close()
if e != nil {
return "", errors.Wrap(e, "Could not close zip entry reader")
}
e = backoff.RetryNotify(func() error {
tempFile, e = os.Open(tempFile.Name())
if e != nil {
return errors.Wrap(e, "Could not open temp file for reading")
}
defer tempFile.Close()
if uint64(tempFileSize) >= minimumSize && uint64(tempFileSize) <= maximumSize {
sha := hex.EncodeToString(sha.Sum(nil))
e = blobstore.Put(sha, tempFile)
if e != nil {
if _, ok := e.(*NoSpaceLeftError); ok {
return backoff.Permanent(e)
}
return errors.Wrapf(e, "Could not upload file to blobstore. SHA: '%v'", sha)
}
}
return nil
}, backoff.NewExponentialBackOff(), func(e error, backOffDelay time.Duration) {
metricsService.SendCounterMetric("appStashPutRetries", 1)
})
if e != nil {
return "", e
}
os.Remove(tempFile.Name())
}
}
for _, entry := range bundlesPayload {
zipEntry, e := zipWriter.CreateHeader(zipEntryHeaderWithModifiedTime(entry.Fn, fileModeFrom(entry.Mode), time.Now()))
if e != nil {
return "", errors.Wrap(e, "Could create header in zip file")
}
e = backoff.RetryNotify(func() error {
b, e := blobstore.Get(entry.Sha1)
if e != nil {
if _, ok := e.(*NotFoundError); ok {
return backoff.Permanent(NewNotFoundErrorWithKey(entry.Sha1))
}
return errors.Wrapf(e, "Could not get file from blobstore. SHA: '%v'", entry.Sha1)
}
defer b.Close()
_, e = io.Copy(zipEntry, b)
if e != nil {
return errors.Wrapf(e, "Could not copy file to zip entry. SHA: %v", entry.Sha1)
}
return nil
},
backoff.NewExponentialBackOff(),
func(e error, backOffDelay time.Duration) {
metricsService.SendCounterMetric("appStashGetRetries", 1)
},
)
if e != nil {
return "", e
}
}
zipWriter.Close()
return tempZipFile.Name(), nil
}
func fileModeFrom(s string) os.FileMode {
mode, e := strconv.ParseInt(s, 8, 32)
if e != nil {
return 0744
}
return os.FileMode(mode)
}
func zipEntryHeaderWithModifiedTime(name string, mode os.FileMode, modified time.Time) *zip.FileHeader {
header := &zip.FileHeader{
Name: name,
Method: zip.Deflate,
Modified: modified,
}
header.SetMode(mode)
return header
}