Skip to content

Commit 2b92415

Browse files
committed
feat: support reading of compressed trace files
1 parent 6b699fa commit 2b92415

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

pkg/cmd/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func ReadTraceFile(filename string) lt.TraceFile {
174174
tracefile lt.TraceFile
175175
)
176176
// Read data file
177-
data, err := os.ReadFile(filename)
177+
filename, data, err := file.ReadAndUncompress(filename)
178178
// Check success
179179
if err == nil {
180180
// Check file extension
@@ -220,7 +220,7 @@ func ReadTraceFile(filename string) lt.TraceFile {
220220
func ReadBatchedTraceFile(filename string) []lt.TraceFile {
221221
var (
222222
stats = util.NewPerfStats()
223-
lines = file.ReadInputFile(filename)
223+
lines = file.ReadInputFileAsLines(filename)
224224
traces = make([]lt.TraceFile, 0)
225225
)
226226
// Read constraints line by line

pkg/test/util/check_valid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (p *traceId) String() string {
378378
// ReadTracesFile reads a file containing zero or more traces expressed as JSON, where
379379
// each trace is on a separate line.
380380
func ReadTracesFile(filename string) []lt.TraceFile {
381-
lines := file.ReadInputFile(filename)
381+
lines := file.ReadInputFileAsLines(filename)
382382
traces := make([]lt.TraceFile, len(lines))
383383
// Read constraints line by line
384384
for i, line := range lines {

pkg/util/file/files.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,58 @@ package file
1515
import (
1616
"bufio"
1717
"compress/bzip2"
18+
"compress/gzip"
1819
"errors"
1920
"io"
2021
"os"
2122
"path"
23+
"strings"
2224
)
2325

24-
// ReadInputFile reads an input file as a sequence of lines.
25-
func ReadInputFile(filename string) []string {
26+
// ReadAndUncompress reads a given file and, if its extension indicates it is
27+
// compressed, then it decompresses it before returning the decompressed byte
28+
// and the underlying filename (e.g. if original filename was "file.lt.gz" then
29+
// it returns "file.lt"). Supported compression formats are "bz2" and "gz".
30+
func ReadAndUncompress(filename string) (string, []byte, error) {
31+
var (
32+
ext = path.Ext(filename)
33+
reader io.Reader
34+
err error
35+
data []byte
36+
)
37+
// Open file for reading
38+
file, err := os.Open(filename)
39+
// Setup file close behaviour
40+
defer func() {
41+
if err := file.Close(); err != nil {
42+
panic(err)
43+
}
44+
}()
45+
//
46+
reader = file
47+
// Check for error
48+
if err == nil {
49+
// check extension
50+
switch ext {
51+
case ".bz2":
52+
reader = bzip2.NewReader(reader)
53+
filename = strings.ReplaceAll(filename, ext, "")
54+
case ".gz":
55+
if reader, err = gzip.NewReader(reader); err != nil {
56+
return filename, nil, err
57+
}
58+
//
59+
filename = strings.ReplaceAll(filename, ext, "")
60+
}
61+
//
62+
data, err = io.ReadAll(reader)
63+
}
64+
//
65+
return filename, data, err
66+
}
67+
68+
// ReadInputFileAsLines reads an input file as a sequence of lines.
69+
func ReadInputFileAsLines(filename string) []string {
2670
file, err := os.Open(filename)
2771
// Check whether file exists
2872
if errors.Is(err, os.ErrNotExist) {

0 commit comments

Comments
 (0)