Skip to content

Commit

Permalink
s2c: Handle sigint better (#239)
Browse files Browse the repository at this point in the history
* s2c/s2d: Handle sigint.
* Tweak output

* Simplify.
  • Loading branch information
klauspost authored Apr 12, 2020
1 parent e2d2089 commit cc0d76e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
25 changes: 19 additions & 6 deletions s2/cmd/s2c/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
Expand Down Expand Up @@ -79,10 +80,13 @@ Options:`)

// No args, use stdin/stdout
if len(args) == 1 && args[0] == "-" {
// Catch interrupt, so we don't exit at once.
// os.Stdin will return EOF, so we should be able to get everything.
signal.Notify(make(chan os.Signal), os.Interrupt)
wr.Reset(os.Stdout)
_, err := io.Copy(wr, os.Stdin)
exitErr(err)
exitErr(wr.Close())
_, err = wr.ReadFrom(os.Stdin)
printErr(err)
printErr(wr.Close())
return
}
var files []string
Expand Down Expand Up @@ -137,7 +141,7 @@ Options:`)
dstFilename = "(discarded)"
}
if !*quiet {
fmt.Println("Compressing", filename, "->", dstFilename)
fmt.Print("Compressing ", filename, " -> ", dstFilename)
}
// Input file.
file, err := os.Open(filename)
Expand Down Expand Up @@ -181,11 +185,14 @@ Options:`)
elapsed := time.Since(start)
mbpersec := (float64(input) / (1024 * 1024)) / (float64(elapsed) / (float64(time.Second)))
pct := float64(wc.n) * 100 / float64(input)
fmt.Printf("%d -> %d [%.02f%%]; %.01fMB/s\n", input, wc.n, pct, mbpersec)
fmt.Printf(" %d -> %d [%.02f%%]; %.01fMB/s\n", input, wc.n, pct, mbpersec)
}
if *remove {
closeOnce.Do(func() {
file.Close()
if !*quiet {
fmt.Println("Removing", filename)
}
err := os.Remove(filename)
exitErr(err)
})
Expand All @@ -194,9 +201,15 @@ Options:`)
}
}

func printErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "\nERROR:", err.Error())
}
}

func exitErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR:", err.Error())
fmt.Fprintln(os.Stderr, "\nERROR:", err.Error())
os.Exit(2)
}
}
Expand Down
17 changes: 12 additions & 5 deletions s2/cmd/s2d/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

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

func() {
var closeOnce sync.Once
if !*quiet {
fmt.Println("Decompressing", filename, "->", dstFilename)
fmt.Print("Decompressing ", filename, " -> ", dstFilename)
}
// Input file.
file, err := os.Open(filename)
Expand All @@ -113,7 +117,7 @@ Options:`)
}
var out io.Writer
switch {
case *bench > 0:
case *bench > 0 || *verify:
out = ioutil.Discard
case *stdout:
out = os.Stdout
Expand All @@ -133,11 +137,14 @@ Options:`)
elapsed := time.Since(start)
mbPerSec := (float64(output) / (1024 * 1024)) / (float64(elapsed) / (float64(time.Second)))
pct := float64(output) * 100 / float64(rc.n)
fmt.Printf("%d -> %d [%.02f%%]; %.01fMB/s\n", rc.n, output, pct, mbPerSec)
fmt.Printf(" %d -> %d [%.02f%%]; %.01fMB/s\n", rc.n, output, pct, mbPerSec)
}
if *remove {
if *remove && !*verify {
closeOnce.Do(func() {
file.Close()
if !*quiet {
fmt.Println("Removing", filename)
}
err := os.Remove(filename)
exitErr(err)
})
Expand All @@ -148,7 +155,7 @@ Options:`)

func exitErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR:", err.Error())
fmt.Fprintln(os.Stderr, "\nERROR:", err.Error())
os.Exit(2)
}
}
Expand Down

0 comments on commit cc0d76e

Please sign in to comment.