www.fgks.org   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s2c: Handle sigint better #239

Merged
merged 2 commits into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions s2/cmd/s2c/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -67,10 +68,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 All @@ -97,7 +101,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 @@ -141,11 +145,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 @@ -154,9 +161,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 @@ -86,11 +87,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 @@ -111,7 +115,7 @@ Options:`)
}
var out io.Writer
switch {
case *bench > 0:
case *bench > 0 || *verify:
out = ioutil.Discard
case *stdout:
out = os.Stdout
Expand All @@ -131,11 +135,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 @@ -146,7 +153,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