mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/4] fs: implement unreaddir
Date: Mon, 10 Oct 2022 08:14:37 +0200	[thread overview]
Message-ID: <20221010061437.2085412-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20221010061437.2085412-1-a.fatoum@pengutronix.de>

When iterating over a directory, it can be useful to put back the just
read directory entry, so it can be retried at a later time. This will be
needed for the EFI loader variable support.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/fs.c           | 15 +++++++++++++++
 include/dirent.h  |  1 +
 test/self/ramfs.c | 17 ++++++++++++++++-
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/fs/fs.c b/fs/fs.c
index 620cd6597a63..6583e0a6e9b3 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -890,6 +890,21 @@ static int fillonedir(struct dir_context *ctx, const char *name, int namlen,
 	return 0;
 }
 
+int unreaddir(DIR *dir, const struct dirent *d)
+{
+	struct readdir_entry *entry;
+
+	if (d != &dir->d)
+		return -EINVAL;
+
+	entry = xzalloc(sizeof(*entry));
+	entry->d = *d;
+	list_add_tail(&entry->list, &dir->entries);
+
+	return 0;
+}
+EXPORT_SYMBOL(unreaddir);
+
 struct dirent *readdir(DIR *dir)
 {
 	struct readdir_entry *entry;
diff --git a/include/dirent.h b/include/dirent.h
index 61a76c5b59b9..285074e9f7de 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -18,6 +18,7 @@ typedef struct dir {
 
 DIR *opendir(const char *pathname);
 struct dirent *readdir(DIR *dir);
+int unreaddir(DIR *dir, const struct dirent *d);
 int closedir(DIR *dir);
 
 #endif /* __DIRENT_H */
diff --git a/test/self/ramfs.c b/test/self/ramfs.c
index 927b035e1e8c..f8e1d60a61b5 100644
--- a/test/self/ramfs.c
+++ b/test/self/ramfs.c
@@ -44,6 +44,7 @@ static inline int get_file_count(int i)
 
 static void test_ramfs(void)
 {
+	int files[] = { 1, 3, 5, 7, 11, 13, 17 };
 	char fname[128];
 	char *content = NULL;
 	char *oldpwd = NULL;
@@ -127,6 +128,7 @@ static void test_ramfs(void)
 		goto out;
 
 	i = 1;
+	j = 0;
 
 	while ((d = readdir(dir))) {
 		size_t size = 0;
@@ -150,7 +152,20 @@ static void test_ramfs(void)
 
 		free(buf);
 
-		i++;
+		/*
+		 * For select files, we test unreaddir once and check if
+		 * we read back same element on repeated readdir
+		 */
+		for (j = 0; j < ARRAY_SIZE(files); j++) {
+			if (size == files[j]) {
+				ret = unreaddir(dir, d);
+				expect_success(ret, "unreaddir");
+				files[j] = -1;
+				break;
+			}
+		}
+		if (j == ARRAY_SIZE(files))
+			i++;
 	}
 
 	expect_success(i == 90 ? 0 : -EINVAL,
-- 
2.30.2




  parent reply	other threads:[~2022-10-10  6:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-10  6:14 [PATCH 1/4] test: self: change CONFIG_CMD_SELFTEST default to y Ahmad Fatoum
2022-10-10  6:14 ` [PATCH 2/4] test: self: always build envvar test when SELFTEST_ENABLE_ALL Ahmad Fatoum
2022-10-10  6:14 ` [PATCH 3/4] test: self: add ramfs test Ahmad Fatoum
2022-10-10  6:14 ` Ahmad Fatoum [this message]
2022-10-11 15:02 ` [PATCH 1/4] test: self: change CONFIG_CMD_SELFTEST default to y Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221010061437.2085412-4-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox