Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arnavbhatt288 committed Nov 3, 2023
1 parent 6896ea9 commit 9b2de97
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 129 deletions.
File renamed without changes.
File renamed without changes
226 changes: 110 additions & 116 deletions src/common.go → common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func fmtDuration(d time.Duration) string {
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}

func StartTimer(start time.Time, widgets GUI) chan bool {
ch := make(chan bool)
func StartTimer(start time.Time, widgets GUI) chan struct{} {
chQuit := make(chan struct{})
go func() {
for range time.Tick(time.Second) {
select {
case <-ch:
case <-chQuit:
return
default:
elapsed := time.Since(start)
Expand All @@ -53,13 +53,15 @@ func StartTimer(start time.Time, widgets GUI) chan bool {
}
}
}()
return ch
return chQuit
}

func cleanUp(data *MainData, gui GUI, handles Handles) {
data.bQuitTimer <- true
data.bQuitTimer <- struct{}{}
close(data.bQuitTask)
close(data.bQuitTimer)
CloseRequiredHandles(handles)
disableCancelButton(gui, *data)
DisableCancelButton(gui, *data)
}

func StartMainTask(data *MainData, gui GUI) {
Expand Down Expand Up @@ -124,63 +126,60 @@ func ReadDisk(data *MainData, gui GUI, handles Handles) {
lasti := int64(0)
updateTimer := time.Now()

data.bQuitTask = make(chan bool)
data.bQuitTask = make(chan struct{})
go func() {
defer cleanUp(data, gui, handles)
for i := int64(0); i < diskNumSectors; i += 1024 {
select {
case <-data.bQuitTask:
return
default:
{
set := determineOptimalSize(diskNumSectors, i)
set := determineOptimalSize(diskNumSectors, i)
sectorData, err := ReadSectorDataFromHandle(
handles.hDisk,
i,
set,
diskSector,
)

sectorData, err := ReadSectorDataFromHandle(
handles.hDisk,
i,
set,
diskSector,
if err != nil {
HandleError(
gui,
data,
errors.Join(
errors.New("ReadDisk(): ReadSectorDataFromHandle failed"),
err,
),
)
if err != nil {
cleanUp(data, gui, handles)
HandleError(
gui,
data,
errors.Join(
errors.New("ReadDisk(): ReadSectorDataFromHandle failed"),
err,
),
)
return
}
return
}

err = WriteSectorDataFromHandle(handles.hImage, sectorData, i, diskSector)
if err != nil {
cleanUp(data, gui, handles)
HandleError(
gui,
data,
errors.Join(
errors.New("ReadDisk(): WriteSectorDataFromHandle failed"),
err,
),
)
return
}
err = WriteSectorDataFromHandle(handles.hImage, sectorData, i, diskSector)
if err != nil {
HandleError(
gui,
data,
errors.Join(
errors.New("ReadDisk(): WriteSectorDataFromHandle failed"),
err,
),
)
return
}

gui.rwProgressBar.SetValue(float64(i))
if time.Since(updateTimer).Milliseconds() >= 1000 {
mbPerSec := float64(
(int64(diskSector) * (i - lasti)),
) * (1000 / float64(time.Since(updateTimer).Milliseconds())) / 1024.0 / 1024.0
setText := fmt.Sprintf("%f MB/s", mbPerSec)
gui.speedLabel.SetText(setText)
lasti = i
updateTimer = time.Now()
}
gui.rwProgressBar.SetValue(float64(i))
if time.Since(updateTimer).Milliseconds() >= 1000 {
mbPerSec := float64(
(int64(diskSector) * (i - lasti)),
) * (1000 / float64(time.Since(updateTimer).Milliseconds())) / 1024.0 / 1024.0
setText := fmt.Sprintf("%f MB/s", mbPerSec)
gui.speedLabel.SetText(setText)
lasti = i
updateTimer = time.Now()
}
}
}
cleanUp(data, gui, handles)
gui.statusLabel.SetText("Success!")
}()
}

Expand Down Expand Up @@ -220,99 +219,94 @@ func WriteVerifyDisk(data *MainData, gui GUI, handles Handles) {
lasti := int64(0)
updateTimer := time.Now()

data.bQuitTask = make(chan bool)
data.bQuitTask = make(chan struct{})
go func() {
defer cleanUp(data, gui, handles)
for i := int64(0); i < imageNumSectors; i += 1024 {
select {
case <-data.bQuitTask:
return
default:
{
numSectors := determineOptimalSize(imageNumSectors, i)
numSectors := determineOptimalSize(imageNumSectors, i)
sectorData, err := ReadSectorDataFromHandle(
handles.hImage,
i,
numSectors,
diskSector,
)

sectorData, err := ReadSectorDataFromHandle(
handles.hImage,
i,
numSectors,
diskSector,
if err != nil {
HandleError(
gui,
data,
errors.Join(
errors.New("WriteVerifyDisk(): ReadSectorDataFromHandle failed"),
err,
),
)
if err != nil {
cleanUp(data, gui, handles)
HandleError(
gui,
data,
errors.Join(
errors.New("WriteVerifyDisk(): ReadSectorDataFromHandle failed"),
err,
),
)
return
}
return
}

if data.taskType == START_WRITE {
err = WriteSectorDataFromHandle(
handles.hDisk,
sectorData,
i,
diskSector,
)
if err != nil {
cleanUp(data, gui, handles)
HandleError(
gui,
data,
errors.Join(
errors.New(
"WriteVerifyDisk(): WriteSectorDataFromHandle failed",
),
err,
),
)
return
}
}
sectorData2, err := ReadSectorDataFromHandle(
if data.taskType == START_WRITE {
err = WriteSectorDataFromHandle(
handles.hDisk,
sectorData,
i,
numSectors,
diskSector,
)
if err != nil {
cleanUp(data, gui, handles)
HandleError(
gui,
data,
errors.Join(
errors.New("WriteVerifyDisk(): ReadSectorDataFromHandle failed"),
errors.New(
"WriteVerifyDisk(): WriteSectorDataFromHandle failed",
),
err,
),
)
return
}
}
sectorData2, err := ReadSectorDataFromHandle(
handles.hDisk,
i,
numSectors,
diskSector,
)
if err != nil {
HandleError(
gui,
data,
errors.Join(
errors.New("WriteVerifyDisk(): ReadSectorDataFromHandle failed"),
err,
),
)
return
}

if !bytes.Equal(sectorData, sectorData2) {
cleanUp(data, gui, handles)
strError := fmt.Sprintf(
"WriteVerifyDisk(): Verification failed at sector: %d\n",
i,
)
HandleError(gui, data, errors.New(strError))
return
}
if !bytes.Equal(sectorData, sectorData2) {
strError := fmt.Sprintf(
"WriteVerifyDisk(): Verification failed at sector: %d\n",
i,
)
HandleError(gui, data, errors.New(strError))
return
}

gui.rwProgressBar.SetValue(float64(i))
if time.Since(updateTimer).Milliseconds() >= 1000 {
mbPerSec := float64(
(int64(diskSector) * (i - lasti)),
) * (1000 / float64(time.Since(updateTimer).Milliseconds())) / 1024.0 / 1024.0
setText := fmt.Sprintf("%f MB/s", mbPerSec)
gui.speedLabel.SetText(setText)
lasti = i
updateTimer = time.Now()
}
gui.rwProgressBar.SetValue(float64(i))
if time.Since(updateTimer).Milliseconds() >= 1000 {
mbPerSec := float64(
(int64(diskSector) * (i - lasti)),
) * (1000 / float64(time.Since(updateTimer).Milliseconds())) / 1024.0 / 1024.0
setText := fmt.Sprintf("%f MB/s", mbPerSec)
gui.speedLabel.SetText(setText)
lasti = i
updateTimer = time.Now()
}
}
}
cleanUp(data, gui, handles)
gui.statusLabel.SetText("Success!")
}()
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 8 additions & 12 deletions src/gui.go → gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type MainData struct {
taskType TaskType
selectedDrive string
imagePath string
bQuitTimer chan bool
bQuitTask chan bool
bQuitTimer chan struct{}
bQuitTask chan struct{}
}

type GUI struct {
Expand All @@ -30,7 +30,7 @@ type GUI struct {
guiTabs *container.AppTabs
}

func disableCancelButton(widgets GUI, data MainData) {
func DisableCancelButton(widgets GUI, data MainData) {
if data.taskType == START_WRITE || data.taskType == START_VERIFY {
widgets.guiTabs.EnableIndex(1)
} else if data.taskType == START_READ {
Expand Down Expand Up @@ -77,7 +77,7 @@ func enableCancelButton(widgets GUI, data MainData) {
}

func FileOpenDialog(myApp fyne.App, gui GUI, data *MainData) {
window := myApp.NewWindow("Open File")
window := myApp.NewWindow("Utkirna")
window.CenterOnScreen()
window.SetFixedSize(true)

Expand All @@ -91,7 +91,6 @@ func FileOpenDialog(myApp fyne.App, gui GUI, data *MainData) {
gui.inputPath.SetText(data.imagePath)
}
}, window)
fd.SetFilter(storage.NewExtensionFileFilter([]string{".img"}))
fd.Show()
fd.SetOnClosed(func() {
window.Close()
Expand All @@ -105,7 +104,7 @@ func FileOpenDialog(myApp fyne.App, gui GUI, data *MainData) {
}

func FileSaveDialog(myApp fyne.App, gui GUI, data *MainData) {
window := myApp.NewWindow("Save File")
window := myApp.NewWindow("Utkirna")
window.CenterOnScreen()
window.SetFixedSize(true)

Expand Down Expand Up @@ -134,7 +133,7 @@ func FileSaveDialog(myApp fyne.App, gui GUI, data *MainData) {

func HandleError(gui GUI, data *MainData, err error) {
dialog.ShowError(err, gui.window)
disableCancelButton(gui, *data)
DisableCancelButton(gui, *data)
}

func HandleStartError() {
Expand Down Expand Up @@ -229,13 +228,11 @@ func StartGui() {
gui.cancelButton = widget.NewButton("Cancel", func() {
dialog.ShowConfirm(
"Cancellation",
"Cancelling the current operation may corrupt the destination drive. Are you sure to continue?",
"Cancelling the current operation may corrupt the destination drive.\nAre you sure to continue?",
func(b bool) {
if b {
data.bQuitTask <- struct{}{}
gui.statusLabel.SetText("Cancelled")
data.bQuitTask <- true
disableCancelButton(gui, data)
data.bQuitTimer <- true
}
},
gui.window,
Expand Down Expand Up @@ -335,7 +332,6 @@ func StartGui() {
container.NewTabItem("Write To Disk", writeTab),
container.NewTabItem("Read From Disk", readTab),
)

gui.guiTabs.SetTabLocation(container.TabLocationTop)

gui.window.SetContent(gui.guiTabs)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/util_linux.go → util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func isPermAvailable() bool {
if err != nil {
fmt.Printf("[isRoot] Unable to get current user: %s", err)
}
return currentUser.Username == "root"
return currentUser.Uid == "0"
}

func gatherSizeInBytes(fd int) (int64, error) {
Expand Down
File renamed without changes.

0 comments on commit 9b2de97

Please sign in to comment.