1
0
mirror of https://github.com/byReqz/go-etcher.git synced 2025-07-03 11:30:50 +00:00

11 Commits
0.0.1 ... main

Author SHA1 Message Date
4e655c43da bump deps 2024-04-17 18:41:38 +02:00
d0f0957932 make linter happy 2022-12-29 23:01:03 +01:00
17bb76627f add lint workflow 2022-12-29 19:24:23 +01:00
427c4fd990 fmt 2022-03-26 11:12:58 +01:00
947660b773 add output verification 2022-02-08 12:28:49 +01:00
15b7e2f781 add headline to device overview 2022-02-07 13:03:38 +01:00
30afa9ae21 make force flag actually do something 2022-02-07 12:44:07 +01:00
e9ac06c8cf remove clutter 2022-02-07 12:36:08 +01:00
95f4d1d682 fix blockdevices being unwritable 2022-02-07 12:21:51 +01:00
4dfdda9e78 fix issue with size warning 2022-01-31 22:34:30 +01:00
36abb0e312 show possible target devices 2022-01-26 23:40:45 +01:00
5 changed files with 167 additions and 110 deletions

7
.github/workflows/lint.yml vendored Normal file
View File

@ -0,0 +1,7 @@
name: ci
on: [push]
jobs:
test:
uses: byReqz/workflows/.github/workflows/golint_with_codeql.yml@main

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.

28
go.mod
View File

@ -1,23 +1,23 @@
module github.com/byReqz/go-etcher module github.com/byReqz/go-etcher
go 1.17 go 1.21
toolchain go1.22.2
require ( require (
github.com/briandowns/spinner v1.18.0 github.com/JoaoDanielRufino/go-input-autocomplete v1.1.0
github.com/fatih/color v1.13.0 github.com/briandowns/spinner v1.23.0
github.com/schollz/progressbar/v3 v3.8.5 github.com/fatih/color v1.16.0
github.com/schollz/progressbar/v3 v3.14.2
github.com/spf13/pflag v1.0.5
) )
require ( require (
github.com/JoaoDanielRufino/go-input-autocomplete v1.0.4 // indirect github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 // indirect
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/rivo/uniseg v0.2.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect golang.org/x/sys v0.19.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect golang.org/x/term v0.19.0 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
) )

65
go.sum
View File

@ -1,53 +1,38 @@
github.com/JoaoDanielRufino/go-input-autocomplete v1.0.4 h1:ZsgP08f77C1Hizi5pfB9xNBG+d605s0z+PWuI5KLEEk= github.com/JoaoDanielRufino/go-input-autocomplete v1.1.0 h1:FNqKY5QPEIsqD1L8MFWh+mcU/vIZT45n5/XIYuDUOQc=
github.com/JoaoDanielRufino/go-input-autocomplete v1.0.4/go.mod h1:tB42IGOd4W3hzlAcw8JWf0mMnu94q4tPsjhT24MbcB4= github.com/JoaoDanielRufino/go-input-autocomplete v1.1.0/go.mod h1:jbEOh6l6PzFysFEbqoooNDwTQwb9Oz8hBwMJh2CTVlI=
github.com/briandowns/spinner v1.18.0 h1:SJs0maNOs4FqhBwiJ3Gr7Z1D39/rukIVGQvpNZVHVcM= github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
github.com/briandowns/spinner v1.18.0/go.mod h1:QOuQk7x+EaDASo80FEXwlwiA+j/PPIcX3FScO+3/ZPQ= github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 h1:jdjd5e68T4R/j4PWxfZqcKY8KtT9oo8IPNVuV4bSXDQ= github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg=
github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807/go.mod h1:Xoiu5VdKMvbRgHuY7+z64lhu/7lvax/22nzASF6GrO8= github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw= github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw= github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/schollz/progressbar/v3 v3.8.5 h1:VcmmNRO+eFN3B0m5dta6FXYXY+MEJmXdWoIS+jjssQM= github.com/schollz/progressbar/v3 v3.14.2 h1:EducH6uNLIWsr560zSV1KrTeUb/wZGAHqyMFIEa99ks=
github.com/schollz/progressbar/v3 v3.8.5/go.mod h1:ewO25kD7ZlaJFTvMeOItkOZa8kXu1UvFs379htE8HMQ= github.com/schollz/progressbar/v3 v3.14.2/go.mod h1:aQAZQnhF4JGFtRJiw/eobaXpsqpVQAftEQ+hLGXaRc4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 h1:0es+/5331RGQPcXlMfP+WrnIIS6dNnNRe0WB02W0F4M= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/sys v0.0.0-20200806125547-5acd03effb82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

176
main.go
View File

@ -1,26 +1,32 @@
package main package main
import ( import (
"crypto/sha256"
"fmt" "fmt"
"os"
"io"
"time"
"log"
"strings"
"github.com/schollz/progressbar/v3"
"github.com/fatih/color"
"github.com/briandowns/spinner"
flag "github.com/spf13/pflag"
ac "github.com/JoaoDanielRufino/go-input-autocomplete" ac "github.com/JoaoDanielRufino/go-input-autocomplete"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/schollz/progressbar/v3"
flag "github.com/spf13/pflag"
"io"
"log"
"os"
"runtime"
"strconv"
"strings"
"time"
) )
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()
} }
@ -45,6 +51,7 @@ func GetPath() string {
} }
func GetDest() string { func GetDest() string {
PrintAvail()
dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ") dest, err := ac.Read("[ " + color.YellowString("i") + " ] Please input destination: ")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -85,24 +92,42 @@ 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 { func PrintAvail() {
err := image.Sync() if runtime.GOOS == "linux" {
if err != nil { block, _ := os.ReadDir("/sys/block")
return err if len(block) == 0 {
return
}
var targets []string
for _, device := range block {
if strings.HasPrefix(device.Name(), "sd") {
targets = append(targets, device.Name())
}
if strings.HasPrefix(device.Name(), "nvme") {
targets = append(targets, device.Name())
}
if strings.HasPrefix(device.Name(), "vd") {
targets = append(targets, device.Name())
}
}
fmt.Println("Available devices:")
for _, target := range targets {
sizefile, _ := os.Open("/sys/block/" + target + "/size")
sizeread, _ := io.ReadAll(sizefile)
_ = sizefile.Close()
sizestring := strings.ReplaceAll(string(sizeread), "\n", "")
size, _ := strconv.Atoi(sizestring)
size = size * 512
size = size / 1024 / 1024 / 1024
fmt.Print(" * ", "/dev/"+target)
if size > 0 {
fmt.Print(" [", size, "GB]\n")
} else {
fmt.Println("")
}
}
} }
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 main() { func main() {
@ -122,7 +147,7 @@ func main() {
} else if len(flag.Args()) > 0 { } else if len(flag.Args()) > 0 {
if input == flag.Args()[0] && len(flag.Args()) > 1 { if input == flag.Args()[0] && len(flag.Args()) > 1 {
device = flag.Args()[1] device = flag.Args()[1]
} else if input != flag.Args()[0] && len(flag.Args()) > 0 { } else if input != flag.Args()[0] && len(flag.Args()) > 0 {
device = flag.Args()[0] device = flag.Args()[0]
} }
} }
@ -142,18 +167,11 @@ func main() {
s.Stop() s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Getting file details ") fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
log.Fatal(err) log.Fatal(err)
} else {
s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Getting file details ")
} }
s.Prefix = "[ "
s.Suffix = " ] Opening files"
s.Start()
image, err := os.Open(input) image, err := os.Open(input)
if err != nil { if err != nil {
s.Stop() s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Opening files ") fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
log.Fatal(err) log.Fatal(err)
} }
var inputsize int64 var inputsize int64
@ -162,13 +180,14 @@ func main() {
inputsize = statinput.Size() inputsize = statinput.Size()
inputisblock = false inputisblock = false
} else { } else {
inputsize, err = image.Seek(0, io.SeekEnd) inputsize, _ = image.Seek(0, io.SeekEnd)
inputisblock = true inputisblock = true
_, _ = image.Seek(0, 0)
} }
target, err := os.OpenFile(device, os.O_RDWR, 0660) target, err := os.OpenFile(device, os.O_RDWR, 0660)
if err != nil { if err != nil {
s.Stop() s.Stop()
fmt.Println("\r[", color.RedString("✘"), "] Opening files ") fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
log.Fatal(err) log.Fatal(err)
} }
var targetsize int64 var targetsize int64
@ -179,43 +198,57 @@ func main() {
} else { } else {
targetsize, err = target.Seek(0, io.SeekEnd) targetsize, err = target.Seek(0, io.SeekEnd)
targetisblock = true targetisblock = true
_, _ = 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("✘"), "] Opening files ") fmt.Println("\r[", color.RedString("✘"), "] Getting file details ")
log.Fatal(err) log.Fatal(err)
} else { } else {
s.Stop() s.Stop()
fmt.Println("\r[", color.GreenString("✓"), "] Opening files ") fmt.Println("\r[", color.GreenString("✓"), "] Getting file details ")
} }
inputmb := fmt.Sprint("[", inputsize / 1024 / 1024, "MB]") inputmb := fmt.Sprint("[", inputsize/1024/1024, "MB]")
devicemb := fmt.Sprint("[", targetsize / 1024 / 1024, "MB]") devicemb := fmt.Sprint("[", targetsize/1024/1024, "MB]")
var inputblock string var inputblock string
var targetblock string var targetblock string
if inputisblock == true { if inputisblock {
inputblock = "[Blockdevice]" inputblock = "[Blockdevice]"
} else { } else {
inputblock = "[File]" inputblock = "[File]"
} }
if targetisblock == true { if targetisblock {
targetblock = "[Blockdevice]" targetblock = "[Blockdevice]"
} else { } else {
targetblock = "[File]" targetblock = "[File]"
} }
fmt.Println("[", color.BlueString("i"), "] Input device/file: " + input, inputmb, inputblock) fmt.Println("[", color.BlueString("i"), "] Input device/file: "+input, inputmb, inputblock)
fmt.Println("[", color.BlueString("i"), "] Output device/file: " + device, devicemb, targetblock) fmt.Println("[", color.BlueString("i"), "] Output device/file: "+device, devicemb, targetblock)
if statinput.Size() > statdevice.Size() { if !force {
fmt.Println("[", color.RedString("w"), "]", color.RedString(" Warning:"), "Input file seems to be bigger than the destination!") if inputsize > targetsize {
fmt.Println("[", color.RedString("w"), "]", color.RedString(" Warning:"), "Input file seems to be bigger than the destination!")
}
fmt.Print(color.HiWhiteString("Do you want to continue? [y/N]: "))
var yesno string
_, _ = fmt.Scanln(&yesno)
yesno = strings.TrimSpace(yesno)
if !(yesno == "y" || yesno == "Y") {
log.Fatal("aborted")
}
} }
fmt.Print(color.HiWhiteString("Do you want to continue? [y/N]: "))
var yesno string
_, _ = fmt.Scanln(&yesno)
yesno = strings.TrimSpace(yesno)
if ! (yesno == "y" || yesno == "Y") {
log.Fatal("aborted")
}
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)
@ -226,7 +259,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 ")
@ -235,4 +274,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)
}
} }