Skip to content

Commit

Permalink
is_dir/is_file small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin authored and GuillaumeGomez committed Apr 8, 2024
1 parent adb5fc2 commit 663a429
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/unix/linux/component.rs
Expand Up @@ -240,9 +240,16 @@ impl ComponentInner {
let dir = read_dir(folder).ok()?;
let mut matchings: HashMap<u32, Component> = HashMap::with_capacity(10);
for entry in dir.flatten() {
let Ok(file_type) = entry.file_type() else {
continue;
};
if file_type.is_dir() {
continue;
}

let entry = entry.path();
let filename = entry.file_name().and_then(|x| x.to_str()).unwrap_or("");
if entry.is_dir() || !filename.starts_with("temp") {
if !filename.starts_with("temp") {
continue;
}

Expand Down Expand Up @@ -363,8 +370,11 @@ impl ComponentsInner {
self.components.clear();
if let Ok(dir) = read_dir(Path::new("/sys/class/hwmon/")) {
for entry in dir.flatten() {
let Ok(file_type) = entry.file_type() else {
continue;
};
let entry = entry.path();
if !entry.is_dir()
if !file_type.is_dir()
|| !entry
.file_name()
.and_then(|x| x.to_str())
Expand Down
26 changes: 15 additions & 11 deletions src/unix/linux/process.rs
Expand Up @@ -657,6 +657,13 @@ fn get_all_pid_entries(
entry: DirEntry,
data: &mut Vec<ProcAndTasks>,
) -> Option<Pid> {
let Ok(file_type) = entry.file_type() else {
return None;
};
if !file_type.is_dir() {
return None;
}

let entry = entry.path();
let name = entry.file_name();

Expand All @@ -665,26 +672,23 @@ fn get_all_pid_entries(
return None;
}
let name = name?;
if !entry.is_dir() {
return None;
}
let pid = Pid::from(usize::from_str(&name.to_string_lossy()).ok()?);

let tasks_dir = Path::join(&entry, "task");
let tasks = if tasks_dir.is_dir() {

let tasks = if let Ok(entries) = fs::read_dir(tasks_dir) {
let mut tasks = HashSet::new();
if let Ok(entries) = fs::read_dir(tasks_dir) {
for task in entries
.into_iter()
.filter_map(|entry| get_all_pid_entries(Some(name), Some(pid), entry.ok()?, data))
{
tasks.insert(task);
}
for task in entries
.into_iter()
.filter_map(|entry| get_all_pid_entries(Some(name), Some(pid), entry.ok()?, data))
{
tasks.insert(task);
}
Some(tasks)
} else {
None
};

data.push(ProcAndTasks {
pid,
parent_pid,
Expand Down
3 changes: 2 additions & 1 deletion tests/code_checkers/utils.rs
Expand Up @@ -25,8 +25,9 @@ pub fn read_dirs<P: AsRef<Path>, F: FnMut(&Path, &str)>(dirs: &[P], callback: &m
fn read_dir<P: AsRef<Path>, F: FnMut(&Path, &str)>(dir: P, callback: &mut F) {
for entry in fs::read_dir(dir).expect("read_dir failed") {
let entry = entry.expect("entry failed");
let file_type = entry.file_type().expect("file_type failed");
let path = entry.path();
if path.is_dir() {
if file_type.is_dir() {
read_dir(path, callback);
} else if path
.extension()
Expand Down

0 comments on commit 663a429

Please sign in to comment.