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 2/6] fs: implement O_DIRECTORY
Date: Thu, 20 Mar 2025 06:20:15 +0100	[thread overview]
Message-ID: <20250320052019.1726331-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250320052019.1726331-1-a.fatoum@pengutronix.de>

We already had O_DIRECTORY defined, but only used it as one of the
bits for O_TMPFILE.

Fix the O_TMPFILE branch, so it checks both bits and implement
O_DIRECTORY, so it causes an error when opening a file that's not
a directory.

This is useful for use with openat.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/stat.c   |  2 +-
 fs/fs.c           | 32 ++++++++++++++++++++++----------
 test/self/dirfd.c |  6 +++---
 3 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/commands/stat.c b/commands/stat.c
index 10662005cdda..8a96bf77b0f1 100644
--- a/commands/stat.c
+++ b/commands/stat.c
@@ -26,7 +26,7 @@ static int do_stat(int argc, char *argv[])
 			extra_flags |= O_CHROOT;
 			fallthrough;
 		case 'c':
-			dirfd = open(optarg, O_PATH | extra_flags);
+			dirfd = open(optarg, O_PATH | O_DIRECTORY | extra_flags);
 			if (dirfd < 0)
 				return dirfd;
 			break;
diff --git a/fs/fs.c b/fs/fs.c
index 1766719c567a..fd5818c36c20 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -337,16 +337,23 @@ static void put_file(struct file *f)
 
 static struct file *fd_to_file(int fd, bool o_path_ok)
 {
-	if (fd < 0 || fd >= MAX_FILES || !files[fd].fsdev) {
-		errno = EBADF;
-		return ERR_PTR(-errno);
-	}
-	if (!o_path_ok && (files[fd].f_flags & O_PATH)) {
-		errno = EINVAL;
-		return ERR_PTR(-errno);
+	int err = -EBADF;
+	unsigned flimits;
+
+	if (fd < 0 || fd >= MAX_FILES || !files[fd].fsdev)
+		goto err;
+
+	flimits = files[fd].f_flags & (O_PATH | O_DIRECTORY);
+	if (!o_path_ok && flimits) {
+		if (flimits & O_DIRECTORY)
+			err = -EBADF;
+		goto err;
 	}
 
 	return &files[fd];
+err:
+	errno = -err;
+	return ERR_PTR(err);
 }
 
 static int create(struct dentry *dir, struct dentry *dentry)
@@ -2545,7 +2552,7 @@ int openat(int dirfd, const char *pathname, int flags)
 	struct dentry *dentry = NULL;
 	struct path path;
 
-	if (flags & O_TMPFILE) {
+	if ((flags & O_TMPFILE) == O_TMPFILE) {
 		fsdev = get_fsdevice_by_path(dirfd, pathname);
 		if (!fsdev) {
 			errno = ENOENT;
@@ -2596,11 +2603,16 @@ int openat(int dirfd, const char *pathname, int flags)
 			error = -ENOENT;
 			goto out1;
 		}
-	} else if (!(flags & O_PATH)) {
-		if (d_is_dir(dentry) && !dentry_is_tftp(dentry)) {
+	} else if (d_is_dir(dentry)) {
+		if (!(flags & (O_PATH | O_DIRECTORY)) && !dentry_is_tftp(dentry)) {
 			error = -EISDIR;
 			goto out1;
 		}
+
+		flags |= O_DIRECTORY;
+	} else if (flags & O_DIRECTORY) {
+		error = -ENOTDIR;
+		goto out1;
 	}
 
 	inode = d_inode(dentry);
diff --git a/test/self/dirfd.c b/test/self/dirfd.c
index 20b54258715a..1f9b375e84ff 100644
--- a/test/self/dirfd.c
+++ b/test/self/dirfd.c
@@ -99,8 +99,8 @@ static void test_dirfd(void)
 {
 	int fd;
 
-	fd = open("/", O_PATH);
-	if (expect(fd < 0, false, "open(/, O_PATH) = %d", fd))
+	fd = open("/", O_PATH | O_DIRECTORY);
+	if (expect(fd < 0, false, "open(/, O_PATH | O_DIRECTORY) = %d", fd))
 		close(fd);
 
 #define B(dot, dotdot, zero, dev) 0b##dev##zero##dotdot##dot
@@ -119,7 +119,7 @@ static void test_dirfd(void)
 	do_test_dirfd("AT_FDCWD", AT_FDCWD,
 		      B(110,110,000,111), B(111,110,111,000),
 		      B(110,110,000,111), B(110,110,000,111));
-	do_test_dirfd("/dev", open("/dev", O_PATH),
+	do_test_dirfd("/dev", open("/dev", O_PATH | O_DIRECTORY),
 		      B(111,110,111,000), B(111,110,111,000),
 		      B(110,110,000,111), B(110,110,000,111));
 	do_test_dirfd("/dev O_CHROOT", open("/dev", O_PATH | O_CHROOT),
-- 
2.39.5




  parent reply	other threads:[~2025-03-20  5:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-20  5:20 [PATCH 0/6] fs: implement tree and truncate Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 1/6] fs: use filename_create/filename_lookup instead of open-coding Ahmad Fatoum
2025-03-20  5:20 ` Ahmad Fatoum [this message]
2025-03-20  5:20 ` [PATCH 3/6] fs: implement opendir in terms of fdopendir Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 4/6] commands: implement tree command Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 5/6] commands: add new truncate command Ahmad Fatoum
2025-03-20  5:20 ` [PATCH 6/6] Documentation: devel: add short section on file systems Ahmad Fatoum

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=20250320052019.1726331-3-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