Repository navigation
Expand file tree
/
Copy pathrelease.go
More file actions
222 lines (202 loc) · 5.07 KB
/
Copy pathrelease.go
File metadata and controls
222 lines (202 loc) · 5.07 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zstd"
"github.com/ulikunitz/xz"
"github.com/ulikunitz/xz/lzma"
)
const (
xzStreams = 4
xzDictionarySize = 1 << 14
)
// runRelease compresses data/ipinfo-lite.csv to gzip, xz and zstd formats,
// all written concurrently.
func runRelease() error {
input := filepath.Join(dataDir, "ipinfo-lite.csv")
if _, err := os.Stat(input); err != nil {
return fmt.Errorf("%s not found", input)
}
targets := []struct {
ext string
compress func(src, dst string) error
}{
{".gz", func(src, dst string) error {
// BestSpeed is over three times faster on the CI-class benchmark host;
// the roughly 4 MiB size trade-off is modest for this daily artifact.
return compressFile(src, dst, func(w io.Writer) (io.WriteCloser, error) {
return gzip.NewWriterLevel(w, gzip.BestSpeed)
})
}},
{".xz", func(src, dst string) error {
// Prefer liblzma's parallel CLI when installed (including GitHub's
// Ubuntu runners), retaining the pure-Go path as a portable fallback.
return compressXZFile(src, dst, xzStreams)
}},
{".zst", func(src, dst string) error {
return compressFile(src, dst, func(w io.Writer) (io.WriteCloser, error) {
return zstd.NewWriter(w,
zstd.WithEncoderLevel(zstd.SpeedFastest),
zstd.WithEncoderConcurrency(2),
)
})
}},
}
var wg sync.WaitGroup
errs := make(chan error, len(targets))
for _, t := range targets {
wg.Add(1)
go func() {
defer wg.Done()
dst := input + t.ext
fmt.Printf("Creating %s...\n", dst)
if err := t.compress(input, dst); err != nil {
errs <- err
}
}()
}
wg.Wait()
close(errs)
for err := range errs {
return err
}
fmt.Println("Compression completed.")
return nil
}
// compressXZFile uses the optimized liblzma CLI when available and otherwise
// falls back to deterministic concatenated streams from the pure-Go encoder.
func compressXZFile(src, dst string, streams int) error {
if streams < 1 {
return fmt.Errorf("xz streams must be positive")
}
if command, err := exec.LookPath("xz"); err == nil {
return compressXZCommand(command, src, dst, streams)
}
config := xz.WriterConfig{
DictCap: xzDictionarySize,
Properties: &lzma.Properties{LC: 2, PB: 0},
}
return compressXZFileWithConfig(src, dst, streams, config)
}
func compressXZCommand(command, src, dst string, streams int) error {
temporary, err := os.CreateTemp(filepath.Dir(dst), "."+filepath.Base(dst)+".tmp-*")
if err != nil {
return err
}
temporaryPath := temporary.Name()
defer os.Remove(temporaryPath)
var stderr bytes.Buffer
cmd := exec.Command(command, "-0", fmt.Sprintf("-T%d", streams), "--stdout", "--", src)
cmd.Stdout = temporary
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
temporary.Close()
return fmt.Errorf("run xz: %w: %s", err, strings.TrimSpace(stderr.String()))
}
if err := temporary.Chmod(0o644); err != nil {
temporary.Close()
return err
}
if err := temporary.Close(); err != nil {
return err
}
return os.Rename(temporaryPath, dst)
}
// compressXZFileWithConfig writes deterministic concatenated XZ streams.
// Byte-range stream boundaries are transparent to compliant readers.
func compressXZFileWithConfig(src, dst string, streams int, config xz.WriterConfig) error {
if streams < 1 {
return fmt.Errorf("xz streams must be positive")
}
if config.DictCap < 1 {
return fmt.Errorf("xz dictionary size must be positive")
}
fIn, err := os.Open(src)
if err != nil {
return err
}
defer fIn.Close()
info, err := fIn.Stat()
if err != nil {
return err
}
if size := info.Size(); size < int64(streams) {
streams = max(1, int(size))
}
if streams == 0 { // empty file: still emit one valid stream
streams = 1
}
parts := make([]bytes.Buffer, streams)
errs := make(chan error, streams)
var wg sync.WaitGroup
for i := range streams {
start := info.Size() * int64(i) / int64(streams)
end := info.Size() * int64(i+1) / int64(streams)
wg.Add(1)
go func() {
defer wg.Done()
w, err := config.NewWriter(&parts[i])
if err != nil {
errs <- err
return
}
if _, err := io.Copy(w, io.NewSectionReader(fIn, start, end-start)); err != nil {
_ = w.Close()
errs <- err
return
}
if err := w.Close(); err != nil {
errs <- err
}
}()
}
wg.Wait()
close(errs)
for err := range errs {
return err
}
fOut, err := os.Create(dst)
if err != nil {
return err
}
for i := range parts {
if _, err := parts[i].WriteTo(fOut); err != nil {
fOut.Close()
return err
}
}
return fOut.Close()
}
func compressFile(src, dst string, newWriter func(io.Writer) (io.WriteCloser, error)) error {
fIn, err := os.Open(src)
if err != nil {
return err
}
defer fIn.Close()
fOut, err := os.Create(dst)
if err != nil {
return err
}
w, err := newWriter(fOut)
if err != nil {
fOut.Close()
return err
}
if _, err := io.Copy(w, fIn); err != nil {
w.Close()
fOut.Close()
return err
}
if err := w.Close(); err != nil {
fOut.Close()
return err
}
return fOut.Close()
}