[RFC i-g-t 2/5] lib/igt_dir_explorer: Add function to recursively read all files in a directory

Peter Senna Tschudin peter.senna at linux.intel.com
Wed May 14 17:51:34 UTC 2025


Introduces igt_dir_explorer_read_and_discard_all(), a function that
performs a recursive scan of all files within a directory. Each file is
read, and its content is discarded.

This functionality is utilized in the following tests:
- core_debugfs
- core_debugfs_display_on_off
- core_sysfs

This addition enhances the ability to efficiently handle directory
traversal and file processing in tests.

Cc: marcin.bernatowicz at intel.com
Cc: himanshu.girotra at intel.com
Cc: aditya.chauhan at intel.com
Cc: pravalika.gurram at intel.com
Cc: sai.gowtham.ch at intel.com
Cc: ramadevi.gandi at intel.com
Cc: lucas.demarchi at intel.com
Cc: rodrigo.vivi at intel.com
Cc: kamil.konieczny at linux.intel.com
Cc: katarzyna.piecielska at intel.com
Signed-off-by: Peter Senna Tschudin <peter.senna at linux.intel.com>
---
 lib/igt_dir_explorer.c | 72 ++++++++++++++++++++++++++++++++++++++++++
 lib/igt_dir_explorer.h | 10 ++++++
 lib/meson.build        |  1 +
 3 files changed, 83 insertions(+)
 create mode 100644 lib/igt_dir_explorer.c
 create mode 100644 lib/igt_dir_explorer.h

diff --git a/lib/igt_dir_explorer.c b/lib/igt_dir_explorer.c
new file mode 100644
index 000000000..d47e08535
--- /dev/null
+++ b/lib/igt_dir_explorer.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2025 Intel Corporation
+ */
+
+#include <dirent.h>
+#include <fcntl.h>
+
+#include "igt.h"
+#include "igt_dir_explorer.h"
+
+void igt_dir_explorer_read_and_discard_all(int path_fd, int indent)
+{
+	struct dirent *dirent;
+	DIR *dir;
+	char tabs[8];
+	int i;
+
+	igt_assert(indent < sizeof(tabs) - 1);
+
+	for (i = 0; i < indent; i++)
+		tabs[i] = '\t';
+	tabs[i] = '\0';
+
+	dir = fdopendir(path_fd);
+	if (!dir)
+		return;
+
+	while ((dirent = readdir(dir))) {
+		if (!strcmp(dirent->d_name, ".") ||
+		    !strcmp(dirent->d_name, ".."))
+			continue;
+
+		if (dirent->d_type == DT_DIR) {
+			int sub_fd;
+
+			sub_fd = openat(path_fd, dirent->d_name,
+					O_RDONLY | O_DIRECTORY);
+			if (sub_fd < 0)
+				continue;
+
+			igt_debug("%sEntering subdir %s\n", tabs, dirent->d_name);
+			igt_dir_explorer_read_and_discard_all(sub_fd, indent + 1);
+			close(sub_fd);
+		} else if (dirent->d_type == DT_REG) {
+			char buf[512];
+			int sub_fd;
+			ssize_t ret;
+
+			igt_kmsg(KMSG_DEBUG "Reading file \"%s\"\n", dirent->d_name);
+			igt_debug("%sReading file \"%s\"\n", tabs, dirent->d_name);
+
+			sub_fd = openat(path_fd, dirent->d_name, O_RDONLY | O_NONBLOCK);
+			if (sub_fd == -1) {
+				igt_debug("%sCould not open file \"%s\" with error: %m\n",
+					  tabs, dirent->d_name);
+				continue;
+			}
+
+			do {
+				ret = read(sub_fd, buf, sizeof(buf));
+			} while (ret == sizeof(buf));
+
+			if (ret == -1)
+				igt_debug("%sCould not read file \"%s\" with error: %m\n",
+					  tabs, dirent->d_name);
+
+			close(sub_fd);
+		}
+	}
+	closedir(dir);
+}
diff --git a/lib/igt_dir_explorer.h b/lib/igt_dir_explorer.h
new file mode 100644
index 000000000..b767928a5
--- /dev/null
+++ b/lib/igt_dir_explorer.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: MIT
+ * Copyright © 2024 Intel Corporation
+ */
+
+#ifndef IGT_DIR_EXPLORER_H
+#define IGT_DIR_EXPLORER_H
+
+void igt_dir_explorer_read_and_discard_all(int path_fd, int indent);
+
+#endif /* IGT_DIR_EXPLORER_H */
diff --git a/lib/meson.build b/lib/meson.build
index b58976a43..f98265100 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -90,6 +90,7 @@ lib_sources = [
 	'igt_kms.c',
 	'igt_fb.c',
 	'igt_core.c',
+	'igt_dir_explorer.c',
 	'igt_draw.c',
 	'igt_list.c',
 	'igt_map.c',
-- 
2.43.0



More information about the igt-dev mailing list