-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
264 lines (216 loc) · 6.92 KB
/
Copy pathmain.go
File metadata and controls
264 lines (216 loc) · 6.92 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
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
package main
import (
"bufio"
"errors"
"fmt"
"io"
"io/fs"
"log"
"os"
"strconv"
"strings"
"time"
"macsmol.pl/logpack/pack"
)
const (
MAX_DISK_READ_BYTES = 5 * 1000 * 1000
)
func main() {
if len(os.Args) == 2 {
tryDoPack(os.Args[1], pack.COMPRESSION_LEVEL_DEFAULT)
} else if len(os.Args) == 3 {
if os.Args[1] == "-d" {
flp := openFileForReadingOrDie(os.Args[2])
defer flp.Close()
outputFileName := deriveOutputFileNameOrDie(os.Args[2])
unpackedFile := createFileForWritingOrDie(outputFileName, "Cannot unpack %v")
defer unpackedFile.Close()
start := time.Now()
totalBytesRead, totalBytesWritten := unpackFile(flp, unpackedFile)
{
elapsed := time.Since(start)
var megabytesRead float32 = float32(totalBytesRead) / 1000_000.0
var megabytesWritten float32 = float32(totalBytesWritten) / 1000_000.0
var speed_MBps float32 = float32(totalBytesRead) / float32(elapsed.Microseconds())
fmt.Printf("%.2f MB unpacked to %.2f MB in %.2fs (%5.2f MB/s)\n",
megabytesRead, megabytesWritten, elapsed.Seconds(), speed_MBps)
}
} else if compressionLevel, err := tryToParseCompressionLevel(os.Args[1]); err == nil {
tryDoPack(os.Args[2], compressionLevel)
} else {
printUsageAndExit()
}
} else {
printUsageAndExit()
}
}
func deriveOutputFileNameOrDie(inputFilename string) string {
outputFileName, suffixFound := strings.CutSuffix(inputFilename, ".lp")
if !suffixFound {
fmt.Printf("Unknown file extension (.lp expected). Ignoring.\n")
os.Exit(0)
}
return outputFileName
}
func openFileForReadingOrDie(filePath string) *os.File {
flp, err := os.Open(filePath)
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
log.Default().Fatalf("Cannot open %s. File does not exist\n", filePath)
}
log.Default().Fatalf("Cannot open: %v", err)
}
return flp
}
func createFileForWritingOrDie(outputFileName, fmtString string) *os.File {
var file *os.File
var err error
file, err = os.OpenFile(outputFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
if err != nil {
if errors.Is(err, fs.ErrExist) {
fmt.Printf("File %s already exists. Overwrite (y/n) ? ", outputFileName)
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
text := scanner.Text()
if text == "y" {
file, err = os.Create(outputFileName)
if err != nil {
log.Default().Fatalf(fmtString, err)
}
} else {
fmt.Printf("Not overwritten\n")
os.Exit(0)
}
} else {
log.Default().Fatalf(fmtString, err)
}
}
return file
}
func tryDoPack(inputFilePath string, compressionLevel int) {
//------------------ OPEN raw log file
f := openFileForReadingOrDie(inputFilePath)
defer f.Close()
//------------------ CREATE packed log file
outputFileName := inputFilePath + ".lp"
flp := createFileForWritingOrDie(outputFileName, "Cannot unpack %v")
defer flp.Close()
start := time.Now()
totalBytesRead, totalBytesWritten := packFile(f, flp, compressionLevel)
{
elapsed := time.Since(start)
var megabytesRead float32 = float32(totalBytesRead) / 1000_000.0
var megabytesWritten float32 = float32(totalBytesWritten) / 1000_000.0
var compRatioPercent float32 = float32(100*totalBytesWritten) / float32(totalBytesRead)
var speed_MBps float32 = float32(totalBytesRead) / float32(elapsed.Microseconds())
fmt.Printf("(%s => %s) %.2f MB packed to %.2f MB (%.1f%%) in %.2fs; average speed: %.1f MB/s\n",
inputFilePath, outputFileName,
megabytesRead, megabytesWritten, compRatioPercent,
elapsed.Seconds(), speed_MBps)
}
}
func tryToParseCompressionLevel(arg string) (int, error) {
if len(arg) != 2 || arg[0] != '-' {
return -1, errors.New("cannot parse compression level")
}
return strconv.Atoi(arg[1:])
}
func printUsageAndExit() {
fmt.Printf(`Usage is:
Packing:
logpack [Options.. ] file.log
Unpacking:
logpack -d file.lp
Options:
-# Desired compression level, where '#' is a number between 1 and 9;
lower numbers provide faster compression, higher numbers yield
better compression ratios. [Default: 4]
`)
os.Exit(0)
}
func packFile(inFile, outFile *os.File, compressionLevel int) (totalBytesRead, totalBytesWritten int64) {
fi, err := inFile.Stat()
if err != nil {
log.Fatal(err)
}
inputFileSizeBytes := fi.Size()
chunkSize := pack.DecompressBound()
inBuff := make([]byte, MAX_DISK_READ_BYTES)
outBuff := make([]byte, chunkSize)
for {
n, err := inFile.ReadAt(inBuff, totalBytesRead)
if err != nil && err != io.EOF {
log.Fatal(err)
}
inRemainder := inBuff[:n]
// write compressed until input buffer is read completely.
for len(inRemainder) > 0 {
read, written := pack.Compress(outBuff, inRemainder, compressionLevel)
_, err2 := outFile.Write(outBuff[:written])
if err2 != nil {
log.Fatal(err2)
}
inRemainder = inRemainder[read:]
totalBytesWritten += int64(written)
}
totalBytesRead += int64(n)
{
var megabytesRead float32 = float32(totalBytesRead) / 1000_000.0
var inputMegabytes float32 = float32(inputFileSizeBytes) / 1000_000.0
var compRatioPercent float32 = float32(100*totalBytesWritten) / float32(totalBytesRead)
fmt.Printf("%7.2f MB / %.2f MB packed (%.1f%%)\r",
megabytesRead, inputMegabytes, compRatioPercent)
}
if err == io.EOF {
break
}
}
return
}
func unpackFile(packed, dstFile *os.File) (totalBytesRead, totalBytesWritten int64) {
fi, err := packed.Stat()
if err != nil {
log.Fatal(err)
}
inputFileSizeBytes := fi.Size()
inBuff := make([]byte, MAX_DISK_READ_BYTES)
unpackedBuff := make([]byte, pack.DecompressBound())
for {
n, err := packed.ReadAt(inBuff, totalBytesRead)
if err != nil && err != io.EOF {
log.Fatal(err)
}
inRemainder := inBuff[:n]
// write decompressed until input buffer is read completely
for len(inRemainder) > 0 {
compressedBytesRead, uncompressedBytesWritten := pack.Decompress(unpackedBuff, inRemainder)
if compressedBytesRead == pack.CORRUPT_INPUT {
log.Fatalf("Error: Cannot unpack \"%s\". Input file is corrupted or is not a Logpack archive\n", packed.Name())
}
// inRemainder did not contain full chunk; break to read more from disk on fresh buffer
if compressedBytesRead == pack.NOT_ENOUGH_INPUT {
// header declares that there is more input but we're at the end
if err == io.EOF {
log.Fatalf("Error: Cannot unpack \"%s\". Input file is corrupted or is not a Logpack archive\n", packed.Name())
}
break
}
inRemainder = inRemainder[compressedBytesRead:]
totalBytesRead += int64(compressedBytesRead)
totalBytesWritten += int64(uncompressedBytesWritten)
_, err2 := dstFile.Write(unpackedBuff[:uncompressedBytesWritten])
if err2 != nil {
log.Fatal(err2)
}
}
{
var megabytesRead float32 = float32(totalBytesRead) / 1000_000.0
var inputMegabytes float32 = float32(inputFileSizeBytes) / 1000_000.0
fmt.Printf("%.2f MB / %.2f MB unpacked\r", megabytesRead, inputMegabytes)
}
if err == io.EOF {
break
}
}
return
}