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,26 +1219,25 @@ 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) => entry, // OK
Some(nix::dir::Type::Directory) => {} // OK _ => continue,
_ => continue,
}
if BACKUP_DATE_REGEX.is_match(name) {
let backup_time = match proxmox_time::parse_rfc3339(&name) {
Ok(time) => time,
Err(err) => return Some(Err(err)),
};
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)), Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if BACKUP_DATE_REGEX.is_match(name) {
let backup_time = match proxmox_time::parse_rfc3339(&name) {
Ok(time) => time,
Err(err) => return Some(Err(err)),
};
return Some(BackupDir::with_group(self.group.clone(), backup_time));
}
} }
} }
} }
@ -1274,47 +1273,46 @@ 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) => entry, // OK
Some(nix::dir::Type::Directory) => {} // OK _ => continue,
_ => continue,
}
if BACKUP_ID_REGEX.is_match(name) {
return Some(Ok(BackupGroup::new(
Arc::clone(&self.store),
(group_type, name.to_owned()).into(),
)));
}
} }
continue; // file did not match regex or isn't valid utf-8
} }
Err(err) => return Some(Err(err)), Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if BACKUP_ID_REGEX.is_match(name) {
return Some(Ok(BackupGroup::new(
Arc::clone(&self.store),
(group_type, name.to_owned()).into(),
)));
}
} }
} 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) => entry, // OK
Some(nix::dir::Type::Directory) => {} // OK _ => continue,
_ => continue,
}
if let Ok(group_type) = BackupType::from_str(name) {
// found a backup group type, descend into it to scan all IDs in it
// by switching to the id-state branch
let base_fd = entry.parent_fd();
let id_dirfd = match proxmox_sys::fs::read_subdir(base_fd, name) {
Ok(dirfd) => dirfd,
Err(err) => return Some(Err(err.into())),
};
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)), Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if let Ok(group_type) = BackupType::from_str(name) {
// found a backup group type, descend into it to scan all IDs in it
// by switching to the id-state branch
let base_fd = entry.parent_fd();
let id_dirfd = match proxmox_sys::fs::read_subdir(base_fd, name) {
Ok(dirfd) => dirfd,
Err(err) => return Some(Err(err.into())),
};
self.id_state = Some((group_type, id_dirfd));
}
} }
} }
} }