datastore: improve backup group/snapshot iters

move the check for directory before doing the OSString -> String
conversion, which should be a bit more efficient.

Also let the match return the entry in the non-skip/return case to
reduce indentation level for the inner "yield element" part, making
it slightly easier to follow.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-04-24 18:06:17 +02:00
parent 4b77d300a2
commit cc295e2c7a
1 changed files with 44 additions and 46 deletions

View File

@ -1219,14 +1219,17 @@ impl Iterator for ListSnapshots {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
loop { loop {
let item = self.fd.next()?; let item = self.fd.next()?; // either get a entry to check or return None if exhausted
match item { let entry = match item {
Ok(ref entry) => { Ok(ref entry) => {
if let Ok(name) = entry.file_name().to_str() {
match entry.file_type() { match entry.file_type() {
Some(nix::dir::Type::Directory) => {} // OK Some(nix::dir::Type::Directory) => entry, // OK
_ => continue, _ => continue,
} }
}
Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if BACKUP_DATE_REGEX.is_match(name) { if BACKUP_DATE_REGEX.is_match(name) {
let backup_time = match proxmox_time::parse_rfc3339(&name) { let backup_time = match proxmox_time::parse_rfc3339(&name) {
Ok(time) => time, Ok(time) => time,
@ -1236,10 +1239,6 @@ impl Iterator for ListSnapshots {
return Some(BackupDir::with_group(self.group.clone(), backup_time)); return Some(BackupDir::with_group(self.group.clone(), backup_time));
} }
} }
continue; // file did not match regex or isn't valid utf-8
}
Err(err) => return Some(Err(err)),
}
} }
} }
} }
@ -1274,13 +1273,16 @@ impl Iterator for ListGroups {
continue; // exhausted all IDs for the current group type, try others continue; // exhausted all IDs for the current group type, try others
} }
}; };
match item { let entry = match item {
Ok(ref entry) => { Ok(ref entry) => {
if let Ok(name) = entry.file_name().to_str() {
match entry.file_type() { match entry.file_type() {
Some(nix::dir::Type::Directory) => {} // OK Some(nix::dir::Type::Directory) => entry, // OK
_ => continue, _ => continue,
} }
}
Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if BACKUP_ID_REGEX.is_match(name) { if BACKUP_ID_REGEX.is_match(name) {
return Some(Ok(BackupGroup::new( return Some(Ok(BackupGroup::new(
Arc::clone(&self.store), Arc::clone(&self.store),
@ -1288,19 +1290,19 @@ impl Iterator for ListGroups {
))); )));
} }
} }
continue; // file did not match regex or isn't valid utf-8
}
Err(err) => return Some(Err(err)),
}
} else { } else {
let item = self.type_fd.next()?; let item = self.type_fd.next()?;
match item { let entry = match item {
// filter directories
Ok(ref entry) => { Ok(ref entry) => {
if let Ok(name) = entry.file_name().to_str() {
match entry.file_type() { match entry.file_type() {
Some(nix::dir::Type::Directory) => {} // OK Some(nix::dir::Type::Directory) => entry, // OK
_ => continue, _ => continue,
} }
}
Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if let Ok(group_type) = BackupType::from_str(name) { if let Ok(group_type) = BackupType::from_str(name) {
// found a backup group type, descend into it to scan all IDs in it // found a backup group type, descend into it to scan all IDs in it
// by switching to the id-state branch // by switching to the id-state branch
@ -1312,10 +1314,6 @@ impl Iterator for ListGroups {
self.id_state = Some((group_type, id_dirfd)); self.id_state = Some((group_type, id_dirfd));
} }
} }
continue; // file did not match regex or isn't valid utf-8
}
Err(err) => return Some(Err(err)),
}
} }
} }
} }