Skip to content

Commit cc0d76e

Browse files
authored
s2c: Handle sigint better (#239)
* s2c/s2d: Handle sigint. * Tweak output * Simplify.
1 parent e2d2089 commit cc0d76e

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

s2/cmd/s2c/main.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"log"
1111
"os"
12+
"os/signal"
1213
"path/filepath"
1314
"runtime"
1415
"runtime/pprof"
@@ -79,10 +80,13 @@ Options:`)
7980

8081
// No args, use stdin/stdout
8182
if len(args) == 1 && args[0] == "-" {
83+
// Catch interrupt, so we don't exit at once.
84+
// os.Stdin will return EOF, so we should be able to get everything.
85+
signal.Notify(make(chan os.Signal), os.Interrupt)
8286
wr.Reset(os.Stdout)
83-
_, err := io.Copy(wr, os.Stdin)
84-
exitErr(err)
85-
exitErr(wr.Close())
87+
_, err = wr.ReadFrom(os.Stdin)
88+
printErr(err)
89+
printErr(wr.Close())
8690
return
8791
}
8892
var files []string
@@ -137,7 +141,7 @@ Options:`)
137141
dstFilename = "(discarded)"
138142
}
139143
if !*quiet {
140-
fmt.Println("Compressing", filename, "->", dstFilename)
144+
fmt.Print("Compressing ", filename, " -> ", dstFilename)
141145
}
142146
// Input file.
143147
file, err := os.Open(filename)
@@ -181,11 +185,14 @@ Options:`)
181185
elapsed := time.Since(start)
182186
mbpersec := (float64(input) / (1024 * 1024)) / (float64(elapsed) / (float64(time.Second)))
183187
pct := float64(wc.n) * 100 / float64(input)
184-
fmt.Printf("%d -> %d [%.02f%%]; %.01fMB/s\n", input, wc.n, pct, mbpersec)
188+
fmt.Printf(" %d -> %d [%.02f%%]; %.01fMB/s\n", input, wc.n, pct, mbpersec)
185189
}
186190
if *remove {
187191
closeOnce.Do(func() {
188192
file.Close()
193+
if !*quiet {
194+
fmt.Println("Removing", filename)
195+
}
189196
err := os.Remove(filename)
190197
exitErr(err)
191198
})
@@ -194,9 +201,15 @@ Options:`)
194201
}
195202
}
196203

204+
func printErr(err error) {
205+
if err != nil {
206+
fmt.Fprintln(os.Stderr, "\nERROR:", err.Error())
207+
}
208+
}
209+
197210
func exitErr(err error) {
198211
if err != nil {
199-
fmt.Fprintln(os.Stderr, "ERROR:", err.Error())
212+
fmt.Fprintln(os.Stderr, "\nERROR:", err.Error())
200213
os.Exit(2)
201214
}
202215
}

s2/cmd/s2d/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
var (
2121
safe = flag.Bool("safe", false, "Do not overwrite output files")
22+
verify = flag.Bool("verify", false, "Verify files, but do not write output")
2223
stdout = flag.Bool("c", false, "Write all output to stdout. Multiple input files will be concatenated")
2324
remove = flag.Bool("rm", false, "Delete source file(s) after successful decompression")
2425
quiet = flag.Bool("q", false, "Don't write any output to terminal, except errors")
@@ -88,11 +89,14 @@ Options:`)
8889
if *bench > 0 {
8990
dstFilename = "(discarded)"
9091
}
92+
if *verify {
93+
dstFilename = "(verify)"
94+
}
9195

9296
func() {
9397
var closeOnce sync.Once
9498
if !*quiet {
95-
fmt.Println("Decompressing", filename, "->", dstFilename)
99+
fmt.Print("Decompressing ", filename, " -> ", dstFilename)
96100
}
97101
// Input file.
98102
file, err := os.Open(filename)
@@ -113,7 +117,7 @@ Options:`)
113117
}
114118
var out io.Writer
115119
switch {
116-
case *bench > 0:
120+
case *bench > 0 || *verify:
117121
out = ioutil.Discard
118122
case *stdout:
119123
out = os.Stdout
@@ -133,11 +137,14 @@ Options:`)
133137
elapsed := time.Since(start)
134138
mbPerSec := (float64(output) / (1024 * 1024)) / (float64(elapsed) / (float64(time.Second)))
135139
pct := float64(output) * 100 / float64(rc.n)
136-
fmt.Printf("%d -> %d [%.02f%%]; %.01fMB/s\n", rc.n, output, pct, mbPerSec)
140+
fmt.Printf(" %d -> %d [%.02f%%]; %.01fMB/s\n", rc.n, output, pct, mbPerSec)
137141
}
138-
if *remove {
142+
if *remove && !*verify {
139143
closeOnce.Do(func() {
140144
file.Close()
145+
if !*quiet {
146+
fmt.Println("Removing", filename)
147+
}
141148
err := os.Remove(filename)
142149
exitErr(err)
143150
})
@@ -148,7 +155,7 @@ Options:`)
148155

149156
func exitErr(err error) {
150157
if err != nil {
151-
fmt.Fprintln(os.Stderr, "ERROR:", err.Error())
158+
fmt.Fprintln(os.Stderr, "\nERROR:", err.Error())
152159
os.Exit(2)
153160
}
154161
}

0 commit comments

Comments
 (0)