Skip to content

Commit a164327

Browse files
committed
dbus: rauc: add Channels type to use instead of Vec<Channel>
This allows us to add methods like "get the primary channel", that operates on all update channels at once, to this `Channels` type later on. Signed-off-by: Leonard Göhrs <[email protected]>
1 parent 77aafa2 commit a164327

File tree

5 files changed

+61
-42
lines changed

5 files changed

+61
-42
lines changed

src/dbus/rauc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::broker::{BrokerBuilder, Topic};
3232
use crate::watched_tasks::WatchedTasksBuilder;
3333

3434
mod update_channels;
35-
pub use update_channels::Channel;
35+
pub use update_channels::{Channel, Channels};
3636

3737
#[cfg(feature = "demo_mode")]
3838
mod demo_mode;
@@ -124,7 +124,7 @@ pub struct Rauc {
124124
pub primary: Arc<Topic<String>>,
125125
pub last_error: Arc<Topic<String>>,
126126
pub install: Arc<Topic<String>>,
127-
pub channels: Arc<Topic<Vec<Channel>>>,
127+
pub channels: Arc<Topic<Channels>>,
128128
pub reload: Arc<Topic<bool>>,
129129
pub should_reboot: Arc<Topic<bool>>,
130130
pub enable_polling: Arc<Topic<bool>>,
@@ -203,7 +203,7 @@ fn would_reboot_into_other_slot(slot_status: &SlotStatus, primary: Option<String
203203
async fn channel_polling_task(
204204
conn: Arc<Connection>,
205205
enable_polling: Arc<Topic<bool>>,
206-
channels: Arc<Topic<Vec<Channel>>>,
206+
channels: Arc<Topic<Channels>>,
207207
slot_status: Arc<Topic<Arc<SlotStatus>>>,
208208
name: String,
209209
) {
@@ -213,7 +213,7 @@ async fn channel_polling_task(
213213

214214
while let Some(mut channel) = channels
215215
.try_get()
216-
.and_then(|chs| chs.into_iter().find(|ch| ch.name == name))
216+
.and_then(|chs| chs.into_vec().into_iter().find(|ch| ch.name == name))
217217
{
218218
// Make sure update polling is enabled before doing anything,
219219
// as contacting the update server requires user consent.
@@ -271,7 +271,7 @@ async fn channel_list_update_task(
271271
conn: Arc<Connection>,
272272
mut reload_stream: Receiver<bool>,
273273
enable_polling: Arc<Topic<bool>>,
274-
channels: Arc<Topic<Vec<Channel>>>,
274+
channels: Arc<Topic<Channels>>,
275275
slot_status: Arc<Topic<Arc<SlotStatus>>>,
276276
) -> Result<()> {
277277
let mut previous: Option<Instant> = None;
@@ -292,7 +292,7 @@ async fn channel_list_update_task(
292292
}
293293

294294
// Read the list of available update channels
295-
let new_channels = match Channel::from_directory(CHANNELS_DIR) {
295+
let new_channels = match Channels::from_directory(CHANNELS_DIR) {
296296
Ok(chs) => chs,
297297
Err(e) => {
298298
warn!("Failed to get list of update channels: {e}");

src/dbus/rauc/update_channels.rs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ pub struct Channel {
5454
pub bundle: Option<UpstreamBundle>,
5555
}
5656

57+
#[derive(Serialize, Deserialize, Clone, PartialEq)]
58+
pub struct Channels(Vec<Channel>);
59+
5760
#[derive(Deserialize)]
5861
pub struct ChannelFile {
5962
pub name: String,
@@ -140,38 +143,6 @@ impl Channel {
140143
Ok(ch)
141144
}
142145

143-
pub(super) fn from_directory(dir: &str) -> Result<Vec<Self>> {
144-
// Find all .yaml files in CHANNELS_DIR
145-
let mut dir_entries: Vec<DirEntry> = read_dir(dir)?
146-
.filter_map(|dir_entry| dir_entry.ok())
147-
.filter(|dir_entry| {
148-
dir_entry
149-
.file_name()
150-
.as_os_str()
151-
.as_bytes()
152-
.ends_with(b".yaml")
153-
})
154-
.collect();
155-
156-
// And sort them alphabetically, so that 01_stable.yaml takes precedence over
157-
// 05_testing.yaml.
158-
dir_entries.sort_by_key(|dir_entry| dir_entry.file_name());
159-
160-
let mut channels: Vec<Self> = Vec::new();
161-
162-
for dir_entry in dir_entries {
163-
let channel = Self::from_file(&dir_entry.path())?;
164-
165-
if channels.iter().any(|ch| ch.name == channel.name) {
166-
bail!("Encountered duplicate channel name \"{}\"", channel.name);
167-
}
168-
169-
channels.push(channel);
170-
}
171-
172-
Ok(channels)
173-
}
174-
175146
fn update_enabled(&mut self) {
176147
// Which channels are enabled is decided based on which RAUC certificates are enabled.
177148
let cert_file = self.name.clone() + ".cert.pem";
@@ -206,6 +177,52 @@ impl Channel {
206177
}
207178
}
208179

180+
impl Channels {
181+
pub(super) fn from_directory(dir: &str) -> Result<Self> {
182+
// Find all .yaml files in CHANNELS_DIR
183+
let mut dir_entries: Vec<DirEntry> = read_dir(dir)?
184+
.filter_map(|dir_entry| dir_entry.ok())
185+
.filter(|dir_entry| {
186+
dir_entry
187+
.file_name()
188+
.as_os_str()
189+
.as_bytes()
190+
.ends_with(b".yaml")
191+
})
192+
.collect();
193+
194+
// And sort them alphabetically, so that 01_stable.yaml takes precedence over
195+
// 05_testing.yaml.
196+
dir_entries.sort_by_key(|dir_entry| dir_entry.file_name());
197+
198+
let mut channels: Vec<Channel> = Vec::new();
199+
200+
for dir_entry in dir_entries {
201+
let channel = Channel::from_file(&dir_entry.path())?;
202+
203+
if channels.iter().any(|ch| ch.name == channel.name) {
204+
bail!("Encountered duplicate channel name \"{}\"", channel.name);
205+
}
206+
207+
channels.push(channel);
208+
}
209+
210+
Ok(Self(channels))
211+
}
212+
213+
pub fn into_vec(self) -> Vec<Channel> {
214+
self.0
215+
}
216+
217+
pub fn iter(&self) -> std::slice::Iter<Channel> {
218+
self.0.iter()
219+
}
220+
221+
pub fn iter_mut(&mut self) -> std::slice::IterMut<Channel> {
222+
self.0.iter_mut()
223+
}
224+
}
225+
209226
impl UpstreamBundle {
210227
fn new(compatible: String, version: String, slot_status: Option<&SlotStatus>) -> Self {
211228
let mut ub = Self {

src/motd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ pub fn run(
207207
},
208208
update = channels_events.recv().fuse() => {
209209
motd.rauc_update_urls = update?
210+
.into_vec()
210211
.into_iter()
211212
.filter_map(|ch| {
212213
ch.bundle

src/ui/screens/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn diagnostic_text(ui: &Ui) -> Result<String, std::fmt::Error> {
130130
if let Some(channels) = ui.res.rauc.channels.try_get() {
131131
write!(&mut text, "chs: ")?;
132132

133-
for ch in channels {
133+
for ch in channels.into_vec() {
134134
let en = if ch.enabled { "[x]" } else { "[ ]" };
135135
let name = ch.name;
136136

src/ui/screens/update_available.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use super::{
3030
InputEvent, Screen, Ui,
3131
};
3232
use crate::broker::Topic;
33-
use crate::dbus::rauc::Channel;
33+
use crate::dbus::rauc::{Channel, Channels};
3434
use crate::watched_tasks::WatchedTasksBuilder;
3535

3636
const SCREEN_TYPE: AlertScreen = AlertScreen::UpdateAvailable;
@@ -73,8 +73,9 @@ impl Selection {
7373
!self.channels.is_empty()
7474
}
7575

76-
fn update_channels(&self, channels: Vec<Channel>) -> Option<Self> {
76+
fn update_channels(&self, channels: Channels) -> Option<Self> {
7777
let channels: Vec<Channel> = channels
78+
.into_vec()
7879
.into_iter()
7980
.filter(|ch| {
8081
ch.bundle
@@ -143,7 +144,7 @@ impl UpdateAvailableScreen {
143144
pub fn new(
144145
wtb: &mut WatchedTasksBuilder,
145146
alerts: &Arc<Topic<AlertList>>,
146-
channels: &Arc<Topic<Vec<Channel>>>,
147+
channels: &Arc<Topic<Channels>>,
147148
) -> Result<Self> {
148149
let (mut channels_events, _) = channels.clone().subscribe_unbounded();
149150
let alerts = alerts.clone();

0 commit comments

Comments
 (0)