Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
Handle Ctrl+C; clean up when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
HACKERALERT authored Apr 26, 2024
1 parent f6a4f0e commit 7cbef79
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions cli/v2/picocrypt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"math"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -194,12 +195,13 @@ func work() int {
fmt.Println("Cannot get current working directory.")
return 1
}
file, err := os.CreateTemp("", "")
file, err := os.CreateTemp("", "picocrypt-cli-v2-*.tmp")
if err != nil {
fmt.Println("Cannot create temporary file.")
return 1
}
writer := zip.NewWriter(file)

for i, path := range files {
stat, err := os.Stat(path)
if err != nil {
Expand Down Expand Up @@ -281,6 +283,16 @@ func work() int {
fmt.Println("Error creating output file.")
return 1
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fin.Close()
fout.Close()
if err := os.Remove(fout.Name()); err == nil {
fmt.Print("\nSystem interrupt detected, cleaning up.")
}
}()

stat, err := os.Stat(fin_)
if err != nil {
Expand Down Expand Up @@ -381,7 +393,7 @@ func work() int {
fin.Close()
fout.Close()
os.Remove(fout_)
fmt.Println("The volume header is damaged.")
fmt.Println("The volume header is irrecoverably damaged.")
return 1
}
}
Expand Down Expand Up @@ -500,7 +512,7 @@ func work() int {
fin.Close()
fout.Close()
os.Remove(fout_)
fmt.Println("The input file is irrecoverably damaged.")
fmt.Println("\nThe input file is irrecoverably damaged.")
return 1
}
if i == MiB/128*136-136 && done+MiB/128*136 >= int(total) && padded {
Expand All @@ -516,7 +528,7 @@ func work() int {
fin.Close()
fout.Close()
os.Remove(fout_)
fmt.Println("The input file is irrecoverably damaged.")
fmt.Println("\nThe input file is irrecoverably damaged.")
return 1
}
src = append(src, tmp...)
Expand All @@ -526,7 +538,7 @@ func work() int {
fin.Close()
fout.Close()
os.Remove(fout_)
fmt.Println("The input file is irrecoverably damaged.")
fmt.Println("\nThe input file is irrecoverably damaged.")
return 1
}
src = append(src, unpad(tmp)...)
Expand All @@ -545,8 +557,9 @@ func work() int {
if err != nil {
fin.Close()
fout.Close()
os.Remove(fout_)
fmt.Println("Insufficient disk space.")
if err := os.Remove(fout.Name()); err == nil {
fmt.Print("\nSystem interrupt detected, cleaning up.")
}
return 1
}
if mode == "decrypt" && *reedsolo {
Expand Down Expand Up @@ -580,12 +593,8 @@ func work() int {
os.Remove(fout_)
fmt.Println("\nThe input volume is damaged or modified.")
if *reedsolo {
if !(*fix) {
fmt.Println("Fortunately, this volume is encoded with Reed-Solomon.")
fmt.Println("Try again using the '-f' flag to repair the corruption.")
} else {
fmt.Println("The corruption could not be fixed with Reed-Solomon.")
}
fmt.Println("Fortunately, this volume is encoded with Reed-Solomon.")
fmt.Println("Try again using the '-f' flag to repair the corruption.")
}
return 1
}
Expand All @@ -598,5 +607,11 @@ func work() int {
}

func main() {
g, err := filepath.Glob(filepath.Join(os.TempDir(), "picocrypt-cli-v2-*.tmp"))
if err == nil {
for _, v := range g {
os.Remove(v)
}
}
os.Exit(work())
}

0 comments on commit 7cbef79

Please sign in to comment.