Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is_dir/is_file small improvements #1241

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/unix/linux/component.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,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 @@ -659,26 +666,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_str()?).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
Original file line number Diff line number Diff line change
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
Loading