1
0
mirror of https://github.com/byReqz/go-etcher.git synced 2024-11-23 07:14:55 +00:00

add output verification

This commit is contained in:
Nils 2022-02-08 12:28:49 +01:00
parent 15b7e2f781
commit 947660b773
Signed by: byreqz
GPG Key ID: 396A62D7D436749E
2 changed files with 48 additions and 21 deletions

View File

@ -12,6 +12,7 @@ arguments:
-d, --device string target device -d, --device string target device
-f, --force override safety features -f, --force override safety features
-i, --input string input file -i, --input string input file
-n, --no-hash disable hash verification
``` ```
If no image or device is specified, etcher will enter interactive mode and prompt for the missing parameters. If no image or device is specified, etcher will enter interactive mode and prompt for the missing parameters.

68
main.go
View File

@ -8,6 +8,7 @@ import (
"strings" "strings"
"runtime" "runtime"
"strconv" "strconv"
"crypto/sha256"
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/briandowns/spinner" "github.com/briandowns/spinner"
@ -18,11 +19,13 @@ import (
var device string var device string
var input string var input string
var force bool var force bool
var disable_hash bool
func init() { func init() {
flag.StringVarP(&device, "device", "d", "", "target device") flag.StringVarP(&device, "device", "d", "", "target device")
flag.StringVarP(&input, "input", "i", "", "input file") flag.StringVarP(&input, "input", "i", "", "input file")
flag.BoolVarP(&force, "force", "f", false, "override safety features") flag.BoolVarP(&force, "force", "f", false, "override safety features")
flag.BoolVarP(&disable_hash, "no-hash", "n", false, "disable hash verification")
flag.Parse() flag.Parse()
} }
@ -88,26 +91,6 @@ func WriteImage(image *os.File, target *os.File, size int64) (int64, error) {
return written, err return written, err
} }
func Sync(image *os.File, target *os.File) error {
err := image.Sync()
if err != nil {
return err
}
err = target.Sync()
if err != nil {
return err
}
err = image.Close()
if err != nil {
return err
}
err = target.Close()
if err != nil {
return err
}
return nil
}
func PrintAvail() { func PrintAvail() {
if runtime.GOOS == "linux" { if runtime.GOOS == "linux" {
block, _ := os.ReadDir("/sys/block") block, _ := os.ReadDir("/sys/block")
@ -216,6 +199,17 @@ func main() {
targetisblock = true targetisblock = true
_, _ = target.Seek(0, 0) _, _ = target.Seek(0, 0)
} }
prehash := sha256.New()
if ! (force || disable_hash) {
if err != nil {
s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
log.Fatal(err)
}
_, err = io.Copy(prehash, image)
_, _ = image.Seek(0, 0)
}
if err != nil { if err != nil {
s.Stop() s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ") fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
@ -253,6 +247,7 @@ func main() {
} }
} }
written, err := WriteImage(image, target, inputsize) written, err := WriteImage(image, target, inputsize)
_, _ = target.Seek(0, 0)
if err != nil { if err != nil {
fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written ") fmt.Println("\r[", color.RedString("✘"), "] Writing image,", written, "bytes written ")
log.Fatal(err) log.Fatal(err)
@ -263,7 +258,13 @@ func main() {
s.Prefix = "[ " s.Prefix = "[ "
s.Suffix = " ] Syncing" s.Suffix = " ] Syncing"
s.Start() s.Start()
err = Sync(image, target) err = image.Sync()
if err != nil {
s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
log.Fatal(err)
}
err = target.Sync()
if err != nil { if err != nil {
s.Stop() s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Syncing ") fmt.Println("\r[", color.RedString("✘"), "] Syncing ")
@ -272,4 +273,29 @@ func main() {
s.Stop() s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Syncing ") fmt.Println("\r[", color.GreenString("✓"), "] Syncing ")
} }
if ! (force || disable_hash) {
s.Prefix = "[ "
s.Suffix = " ] Verifying"
s.Start()
posthash := sha256.New()
_, err = io.CopyN(posthash, target, inputsize)
presum := fmt.Sprintf("%x", prehash.Sum(nil))
postsum := fmt.Sprintf("%x", posthash.Sum(nil))
if err != nil || presum != postsum {
s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Verifying ")
log.Fatal(err)
} else {
s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Verifying ")
}
}
err = image.Close()
if err != nil {
log.Fatal(err)
}
err = target.Close()
if err != nil {
log.Fatal(err)
}
} }