about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume.gomez@huawei.com>2024-08-12 14:19:29 +0200
committerGuillaume Gomez <guillaume.gomez@huawei.com>2024-08-12 14:20:36 +0200
commite2fd0c0f9affa65bf2291a83d48b16919d59fd7c (patch)
tree7e927a1ac2dad813ccf096f9a747436878f6c6a6
parent18d62008d4196630da20c91f3ee95e93816d613d (diff)
downloadrust-e2fd0c0f9affa65bf2291a83d48b16919d59fd7c.tar.gz
rust-e2fd0c0f9affa65bf2291a83d48b16919d59fd7c.zip
Add `read_dir_entries_recursive` in `run_make_support`
-rw-r--r--src/tools/run-make-support/src/path_helpers.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/tools/run-make-support/src/path_helpers.rs b/src/tools/run-make-support/src/path_helpers.rs
index b788bc6ef30..1e6e44c4584 100644
--- a/src/tools/run-make-support/src/path_helpers.rs
+++ b/src/tools/run-make-support/src/path_helpers.rs
@@ -84,3 +84,18 @@ pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool {
 pub fn filename_contains<P: AsRef<Path>>(path: P, needle: &str) -> bool {
     path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(needle))
 }
+
+/// Helper for reading entries in a given directory and its children.
+pub fn read_dir_entries_recursive<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, mut callback: F) {
+    fn read_dir_entries_recursive_inner<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, callback: &mut F) {
+        for entry in rfs::read_dir(dir) {
+            let path = entry.unwrap().path();
+            callback(&path);
+            if path.is_dir() {
+                read_dir_entries_recursive_inner(path, callback);
+            }
+        }
+    }
+
+    read_dir_entries_recursive_inner(dir, &mut callback);
+}