Fix next_nth sorting bug

This commit is contained in:
Garrett Mills 2025-02-24 22:34:02 -05:00
parent 27c2d38fe1
commit 0359de37c8
2 changed files with 17 additions and 12 deletions

View File

@ -455,7 +455,9 @@ async fn transfer_directly(
let storage = &svc.config.pve_storage_pool;
let name_offset = storage.len() + 1; // 1 for the colon (:)
let disk_name = mount[name_offset..].split(",").next().unwrap();
debug!("transfer_directly: mount {mount} | name_offset {name_offset} | disk_name {disk_name}");
debug!("transfer_directly: mount {mount} | name_offset {name_offset} | disk_name {disk_name}");
// Persist the volume
let mut vol = vol.into_active_model();
vol.pve_node_id = Set(to_node.pve_id);

View File

@ -96,6 +96,7 @@ pub async fn try_lock_first_available<'a>(
/** The PVE config file for an LXC container (i.e. /etc/pve/lxc/XYZ.conf) */
#[derive(Debug)]
pub struct PveConfig {
lines: Vec<String>,
}
@ -167,19 +168,21 @@ impl PveConfig {
* Using that example, next_nth("volume") would return 2.
*/
pub fn next_nth(&self, prefix: &str) -> u32 {
let res = self.lines
.iter()
.filter(|line| line.starts_with(prefix))
let mut res = self.lines // list of lines
.iter() // as an iterator
.filter(|line| line.starts_with(prefix)) // only lines starting with "mp"
.map(|line|
line[prefix.len()..]
.split(":")
.next()
.unwrap()
.parse::<u32>())
.filter_map(|idx| idx.ok())
.next();
line[prefix.len()..] // string after the prefix ("1: <fubar>")
.split(":") // ["1", " <fubar>"]
.next() // "1"
.unwrap() // "1"
.parse::<u32>()) // 1
.filter_map(|idx| idx.ok()) // [1, 2]
.collect::<Vec<u32>>(); // [1, 2]
if let Some(idx) = res {
res.sort_by(|a, z| z.cmp(a)); // [2, 1]
if let Some(idx) = res.first() {
return idx + 1;
}