backend.rs (4081B)
1 //! What the installer needs from the system, behind one trait so the screens can be developed and 2 //! tested without a disc, drives or root: the drives, the install, and restart. `Mock` stands in 3 //! for all of it; the real one is in system.rs. 4 5 use std::sync::Mutex; 6 use std::sync::mpsc::Sender; 7 use std::time::Duration; 8 9 #[derive(Debug, Clone, PartialEq)] 10 pub struct Drive { 11 /// `/dev/nvme0n1`: what `patina-install` takes. 12 pub path: String, 13 pub model: String, 14 pub serial: String, 15 pub bytes: u64, 16 /// What is on it, as the person would know it: "Windows (ntfs)", "Fedora", "Empty". 17 pub contents: Vec<String>, 18 /// Why it cannot be chosen (the live disc's own drive, something mounted, too small), if so. 19 pub unusable: Option<String>, 20 } 21 22 /// What an install reports while it runs. 23 #[derive(Debug, Clone, PartialEq)] 24 pub enum Event { 25 /// What is happening, one line. 26 Step(String), 27 /// How far along, 0 to 1, when it can be told. 28 Progress(f32), 29 Done, 30 Failed(String), 31 } 32 33 pub trait Backend: Send + Sync { 34 /// The whole drives that are there, usable or not, in the order to show them. 35 fn drives(&self) -> Vec<Drive>; 36 /// Wipes the drive and installs onto it. Returns at once; the events come over `tx` from a 37 /// thread of the backend's, the last one `Done` or `Failed`. 38 fn install(&self, drive: &Drive, tx: Sender<Event>); 39 fn restart(&self) -> Result<(), String>; 40 } 41 42 pub fn gigabytes(bytes: u64) -> String { 43 let gb = bytes as f64 / 1e9; 44 if gb >= 1000.0 { 45 format!("{:.1} TB", gb / 1000.0) 46 } else { 47 format!("{gb:.0} GB") 48 } 49 } 50 51 /// Three fake drives (one with Windows, one the live disc's, one too small) and an install that 52 /// takes `Mock::SECONDS`, or ends in an error when the mock is told to fail. 53 pub struct Mock { 54 pub fail: bool, 55 pub restarted: Mutex<bool>, 56 } 57 58 impl Mock { 59 pub const SECONDS: u64 = 16; 60 61 pub fn new(fail: bool) -> Self { 62 Self { 63 fail, 64 restarted: Mutex::new(false), 65 } 66 } 67 } 68 69 impl Backend for Mock { 70 fn drives(&self) -> Vec<Drive> { 71 vec![ 72 Drive { 73 path: "/dev/nvme0n1".into(), 74 model: "Samsung SSD 980 500GB".into(), 75 serial: "S64DNX0T123456".into(), 76 bytes: 500_107_862_016, 77 contents: vec![ 78 "EFI System".into(), 79 "Windows (ntfs)".into(), 80 "Recovery".into(), 81 ], 82 unusable: None, 83 }, 84 Drive { 85 path: "/dev/sda".into(), 86 model: "SanDisk Cruzer".into(), 87 serial: "4C530001".into(), 88 bytes: 32_015_679_488, 89 contents: vec!["Sector F install disc".into()], 90 unusable: Some("The install disc is on this drive".into()), 91 }, 92 Drive { 93 path: "/dev/sdb".into(), 94 model: "Generic SD Reader".into(), 95 serial: "0000000000".into(), 96 bytes: 8_000_000_000, 97 contents: vec!["Empty".into()], 98 unusable: Some("Smaller than 12 GB".into()), 99 }, 100 ] 101 } 102 103 fn install(&self, _drive: &Drive, tx: Sender<Event>) { 104 let fail = self.fail; 105 std::thread::spawn(move || { 106 let _ = tx.send(Event::Step("Writing the system to the drive".into())); 107 for i in 0..=100u64 { 108 std::thread::sleep(Duration::from_millis(Mock::SECONDS * 10)); 109 let _ = tx.send(Event::Progress(i as f32 / 100.0)); 110 if fail && i == 60 { 111 let _ = tx.send(Event::Failed( 112 "bootc: writing the bootloader: No space left on device".into(), 113 )); 114 return; 115 } 116 } 117 let _ = tx.send(Event::Done); 118 }); 119 } 120 121 fn restart(&self) -> Result<(), String> { 122 *self.restarted.lock().unwrap() = true; 123 Ok(()) 124 } 125 }