Skip to content

Commit

Permalink
Refactor progress screen and flashing code
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Nov 11, 2024
1 parent 3b24815 commit b2f352b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 44 deletions.
63 changes: 27 additions & 36 deletions app/dd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"io"
"io/fs"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -44,52 +45,21 @@ func RunDd(iff string, of string) {
}
}

// FlashFileToBlockDevice is a re-implementation of dd
// in Golang to work cross-platform on Windows as well.
// FlashFileToBlockDevice is a re-implementation of dd to work cross-platform on Windows as well.
func FlashFileToBlockDevice(iff string, of string) {
// References to use:
// https://stackoverflow.com/questions/21032426/low-level-disk-i-o-in-golang
// https://stackoverflow.com/questions/56512227/how-to-read-and-write-low-level-raw-disk-in-windows-and-go
quit := handleStopInput(func() { os.Exit(0) })
filePath, err := filepath.Abs(iff)
if err != nil {
log.Fatalln("Unable to resolve path to file.")
}
destPath, err := filepath.Abs(of)
if err != nil {
log.Fatalln("Unable to resolve path to dest.")
}
file, err := os.Open(filePath)
if err != nil && os.IsNotExist(err) {
log.Fatalln("This file does not exist!")
} else if err != nil {
log.Fatalln("An error occurred while opening the file.", err)
}
defer file.Close()
fileStat, err := file.Stat()
if err != nil {
log.Fatalln("An error occurred while opening the file.", err)
} else if fileStat.Mode().IsDir() {
log.Fatalln("The specified source file is a folder!")
}
dest, err := os.OpenFile(destPath, os.O_WRONLY|os.O_EXCL, os.ModePerm)
if err != nil {
log.Fatalln("An error occurred while opening the dest.", err)
}
defer dest.Close()
destStat, err := dest.Stat()
if err != nil {
log.Fatalln("An error occurred while opening the file.", err)
} else if destStat.Mode().IsDir() {
log.Fatalln("The specified destination is a directory!")
}
src := openFile(iff, os.O_RDONLY, 0, "file")
dest := openFile(of, os.O_WRONLY|os.O_EXCL, os.ModePerm, "destination")
bs := 4 * 1024 * 1024 // TODO: Allow configurability?
timer := time.NewTimer(time.Second)
startTime := time.Now().UnixMilli()
var total int
buf := make([]byte, bs)
for {
n1, err := file.Read(buf)
n1, err := src.Read(buf)
if err != nil {
if io.EOF == err {
break
Expand All @@ -116,7 +86,7 @@ func FlashFileToBlockDevice(iff string, of string) {
}
}
// t, _ := io.CopyBuffer(dest, file, buf); total = int(t)
err = dest.Sync()
err := dest.Sync()
if err != nil {
log.Fatalln("Failed to sync writes to disk!", err)
} else {
Expand All @@ -129,6 +99,27 @@ func FlashFileToBlockDevice(iff string, of string) {
quit <- true
}

func openFile(filePath string, flag int, mode fs.FileMode, name string) *os.File {
path, err := filepath.Abs(filePath)
if err != nil {
log.Fatalln("Unable to resolve path to " + name + "!")
}
file, err := os.OpenFile(path, flag, mode)
if err != nil && os.IsNotExist(err) {
log.Fatalln("This " + name + " does not exist!")
} else if err != nil {
log.Fatalln("An error occurred while opening "+name+"!", err)
}
defer file.Close()
fileStat, err := file.Stat()
if err != nil {
log.Fatalln("An error occurred while opening "+name+"!", err)
} else if fileStat.Mode().IsDir() {
log.Fatalln("The specified " + name + " is a directory!")
}
return file
}

func handleStopInput(cancel func()) chan bool {
quit := make(chan bool, 1)
go (func() {
Expand Down
17 changes: 9 additions & 8 deletions renderer/screens/ProgressScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const ProgressScreen = ({

const isDone = progress === 'Done!'
const isError = typeof progress === 'string' && !isDone
const inProgress = typeof progress === 'object'
const progressPercent =
!isError && !isDone
? JSBI.divide(
Expand Down Expand Up @@ -85,29 +86,29 @@ const ProgressScreen = ({
</Modal>
<Typography gutterBottom level='h3' color={isError ? 'danger' : undefined}>
{isDone && 'Completed flashing ISO to disk!'}
{isError && 'Error during '}
{!isDone && 'Phase 1/1: Writing ISO to disk...'}
{isError && 'Error occurred while flashing ISO to disk!'}
{inProgress && 'Phase 1/1: Writing ISO to disk...'}
</Typography>
<LinearProgress
sx={{ mb: '0.8em' }}
color={isError ? 'danger' : undefined}
determinate={!isError}
value={isError ? undefined : isDone ? 100 : JSBI.toNumber(progressPercent)}
value={inProgress ? JSBI.toNumber(progressPercent) : isDone ? 100 : undefined}
/>
{!isDone && (
<Typography level='title-lg' gutterBottom color={isError ? 'danger' : undefined}>
{isError || isDone
? progress
: `${progressPercent.toString()}% \
(${bytesToString(progress.bytes)} / ${bytesToString(progress.total)}) — ${progress.speed}`}
{inProgress
? `${progressPercent.toString()}% \
(${bytesToString(progress.bytes)} / ${bytesToString(progress.total)}) — ${progress.speed}`
: progress}
</Typography>
)}
<Typography gutterBottom>
<strong>Source Image:</strong> {sourceImage}
<br />
<strong>Target Disk:</strong> {targetDisk}
</Typography>
{!isError && !isDone && (
{inProgress && (
<Typography gutterBottom color='warning'>
<strong>Note:</strong> Do not remove the external drive or shut down the computer during
the write process.
Expand Down

0 comments on commit b2f352b

Please sign in to comment.