fs: new helper vfs_empty_path()
authorChristian Brauner <brauner@kernel.org>
Tue, 30 Apr 2024 11:54:46 +0000 (13:54 +0200)
committerChristian Brauner <brauner@kernel.org>
Thu, 27 Jun 2024 16:31:19 +0000 (18:31 +0200)
Make it possible to quickly check whether AT_EMPTY_PATH is valid.
Note, after some discussion we decided to also allow NULL to be passed
instead of requiring the empty string.

Signed-off-by: Christian Brauner <brauner@kernel.org>
include/linux/fs.h

index 942cb11dba961543e9697321173de150f9917bed..5ff362277834cdafd6b0a1310fc8e7e043deac4c 100644 (file)
@@ -3631,4 +3631,21 @@ extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
 extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
                           int advice);
 
+static inline bool vfs_empty_path(int dfd, const char __user *path)
+{
+       char c;
+
+       if (dfd < 0)
+               return false;
+
+       /* We now allow NULL to be used for empty path. */
+       if (!path)
+               return true;
+
+       if (unlikely(get_user(c, path)))
+               return false;
+
+       return !c;
+}
+
 #endif /* _LINUX_FS_H */