Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat(gui): Support for verifying written data #118

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gtk/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl App {
self.connect_image_drag_and_drop();
self.connect_hash();
self.connect_view_ready();
self.connect_check();

self
}
Expand Down
7 changes: 7 additions & 0 deletions gtk/src/app/signals/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ impl App {
}
});
}

pub fn connect_check(&self) {
let state = self.state.clone();
self.ui.content.image_view.check_button.connect_property_active_notify(move |button| {
state.check.set(button.get_active());
});
}
}

fn set_hash_widget(state: &State, ui: &GtkUi) {
Expand Down
30 changes: 23 additions & 7 deletions gtk/src/app/signals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ impl App {
let finished = Arc::new(
(0..ndestinations).map(|_| Atomic::new(false)).collect::<Vec<_>>(),
);
let verifying = Arc::new(
(0..ndestinations).map(|_| Atomic::new(false)).collect::<Vec<_>>(),
);

let _ =
state.back_event_tx.send(BackgroundEvent::Flash(FlashRequest::new(
Expand All @@ -182,12 +185,15 @@ impl App {
flash_status.clone(),
progress.clone(),
finished.clone(),
verifying.clone(),
state.check.get(),
)));

tasks = Some(FlashTask {
previous: Arc::new(Mutex::new(vec![[0; 7]; ndestinations])),
progress,
finished,
verifying,
});
}
// When the flashing view is active, and thus an image is flashing.
Expand All @@ -209,6 +215,7 @@ impl App {
let prev_values = &mut previous[id];
let progress = &tasks.progress[id];
let finished = &tasks.finished[id];
let verifying = &tasks.verifying[id];

let raw_value = progress.load(Ordering::SeqCst);
let task_is_finished = finished.load(Ordering::SeqCst);
Expand All @@ -224,17 +231,26 @@ impl App {
if task_is_finished {
label.set_label("Complete");
} else {
prev_values[1] = prev_values[2];
prev_values[2] = prev_values[3];
prev_values[3] = prev_values[4];
prev_values[4] = prev_values[5];
prev_values[5] = prev_values[6];
prev_values[6] = raw_value - prev_values[0];
if raw_value < prev_values[0] {
*prev_values = [0; 7];
} else {
prev_values[1] = prev_values[2];
prev_values[2] = prev_values[3];
prev_values[3] = prev_values[4];
prev_values[4] = prev_values[5];
prev_values[5] = prev_values[6];
prev_values[6] = raw_value - prev_values[0];
}
prev_values[0] = raw_value;

let sum: u64 = prev_values.iter().skip(1).sum();
let per_second = sum / 3;
label.set_label(&format!("{}/s", bytesize::to_string(per_second, true)));
let size = bytesize::to_string(per_second, true);
if verifying.load(Ordering::SeqCst) {
label.set_label(&format!("Verifying {}/s", size));
} else {
label.set_label(&format!("{}/s", size));
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions gtk/src/app/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct State {

pub available_devices: RefCell<Box<[Arc<DiskDevice>]>>,
pub selected_devices: RefCell<Vec<Arc<DiskDevice>>>,

pub check: Cell<bool>,
}

impl State {
Expand Down Expand Up @@ -61,6 +63,7 @@ impl State {
image_size: Arc::new(Atomic::new(0u64)),
available_devices: RefCell::new(Box::new([])),
selected_devices: RefCell::new(Vec::new()),
check: Cell::new(false),
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion gtk/src/app/views/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::path::Path;
pub struct ImageView {
pub view: View,
pub check: Button,
pub check_button: CheckButton,
pub chooser_container: Stack,
pub chooser: Button,
pub image_path: Label,
Expand Down Expand Up @@ -110,6 +111,11 @@ impl ImageView {
..set_margin_bottom(24);
};

let check_button = cascade! {
gtk::CheckButton::with_label("Verify written data");
..set_margin_top(6);
};

let view = View::new(
"application-x-cd-image",
"Choose an Image",
Expand All @@ -118,10 +124,11 @@ impl ImageView {
|right_panel| {
right_panel.pack_start(&chooser_container, true, false, 0);
right_panel.pack_start(&hash_container, false, false, 0);
right_panel.pack_start(&check_button, false, false, 0);
},
);

ImageView { view, check, chooser_container, chooser, image_path, hash, hash_label }
ImageView { view, check, check_button, chooser_container, chooser, image_path, hash, hash_label }
}

pub fn set_hash_sensitive(&self, sensitive: bool) {
Expand Down
24 changes: 17 additions & 7 deletions gtk/src/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ pub struct FlashRequest {
status: Arc<Atomic<FlashStatus>>,
progress: Arc<Vec<Atomic<u64>>>,
finished: Arc<Vec<Atomic<bool>>>,
verifying: Arc<Vec<Atomic<bool>>>,
check: bool,
}

pub struct FlashTask {
pub progress: Arc<Vec<Atomic<u64>>>,
pub previous: Arc<Mutex<Vec<[u64; 7]>>>,
pub finished: Arc<Vec<Atomic<bool>>>,
pub verifying: Arc<Vec<Atomic<bool>>>,
}

struct FlashProgress<'a> {
request: &'a FlashRequest,
id: usize,
errors: &'a [Cell<Result<(), FlashError>>],
verifying: &'a [Atomic<bool>],
}

#[derive(Clone, Debug)]
Expand All @@ -62,10 +66,13 @@ impl<'a> Progress for FlashProgress<'a> {
type Device = ();

fn message(&mut self, _device: &(), kind: &str, message: &str) {
self.errors[self.id].set(Err(FlashError {
kind: kind.to_string(),
message: message.to_string(),
}));
self.verifying[self.id].store(kind == "V", Ordering::SeqCst);
if !message.is_empty() {
self.errors[self.id].set(Err(FlashError {
kind: kind.to_string(),
message: message.to_string(),
}));
}
}

fn finish(&mut self) {
Expand All @@ -84,8 +91,10 @@ impl FlashRequest {
status: Arc<Atomic<FlashStatus>>,
progress: Arc<Vec<Atomic<u64>>>,
finished: Arc<Vec<Atomic<bool>>>,
verifying: Arc<Vec<Atomic<bool>>>,
check: bool,
) -> FlashRequest {
FlashRequest { source: Some(source), destinations, status, progress, finished }
FlashRequest { source: Some(source), destinations, status, progress, finished, verifying, check }
}

pub fn write(mut self) -> anyhow::Result<(anyhow::Result<()>, Vec<Result<(), FlashError>>)> {
Expand Down Expand Up @@ -125,9 +134,10 @@ impl FlashRequest {
// How many bytes to write at a given time.
let mut bucket = [0u8; 64 * 1024];

let mut task = Task::new(source.into(), false);
let mut task = Task::new(source.into(), self.check);
for (i, file) in files.into_iter().enumerate() {
let progress = FlashProgress {request: &self, errors: errors_cells, id: i};
let verifying = &self.verifying;
let progress = FlashProgress {request: &self, errors: errors_cells, verifying, id: i};
task.subscribe(file.into(), (), progress);
}

Expand Down