From: Karel Zak Date: Tue, 30 Jun 2026 14:53:56 +0000 (+0200) Subject: vfs: move mode2flags() to include/vfs.h as ul_mode_to_flags() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=dc1ef208076b2838f8a4161c31211d565c10be0f;p=thirdparty%2Futil-linux.git vfs: move mode2flags() to include/vfs.h as ul_mode_to_flags() Move the fopen-style mode string to open(2) flags parser from lib/path.c to include/vfs.h for reuse across the project, especially in the upcoming ul_vfs_fopen() helper. Addresses: https://github.com/util-linux/util-linux/issues/4308 Signed-off-by: Karel Zak --- diff --git a/include/vfs.h b/include/vfs.h index 97479ec0a..50d8c27d5 100644 --- a/include/vfs.h +++ b/include/vfs.h @@ -97,4 +97,33 @@ static inline int ul_vfs_fsync(const struct ul_vfs_ops *vfs, int fd) return fsync(fd); } +static inline int ul_mode_to_flags(const char *mode) +{ + int flags = 0; + const char *p; + + for (p = mode; p && *p; p++) { + if (*p == 'r' && *(p + 1) == '+') + flags |= O_RDWR; + else if (*p == 'r') + flags |= O_RDONLY; + + else if (*p == 'w' && *(p + 1) == '+') + flags |= O_RDWR | O_TRUNC; + else if (*p == 'w') + flags |= O_WRONLY | O_TRUNC; + + else if (*p == 'a' && *(p + 1) == '+') + flags |= O_RDWR | O_APPEND; + else if (*p == 'a') + flags |= O_WRONLY | O_APPEND; +#ifdef O_CLOEXEC + else if (*p == 'e') + flags |= O_CLOEXEC; +#endif + } + + return flags; +} + #endif /* UTIL_LINUX_VFS_H */ diff --git a/lib/path.c b/lib/path.c index 2a30bf9e7..65a7b5648 100644 --- a/lib/path.c +++ b/lib/path.c @@ -24,6 +24,7 @@ #include "c.h" #include "fileutils.h" #include "all-io.h" +#include "vfs.h" #include "path.h" #include "debug.h" #include "strutils.h" @@ -502,38 +503,9 @@ int ul_path_openf(struct path_cxt *pc, int flags, const char *path, ...) /* * Maybe stupid, but good enough ;-) */ -static int mode2flags(const char *mode) -{ - int flags = 0; - const char *p; - - for (p = mode; p && *p; p++) { - if (*p == 'r' && *(p + 1) == '+') - flags |= O_RDWR; - else if (*p == 'r') - flags |= O_RDONLY; - - else if (*p == 'w' && *(p + 1) == '+') - flags |= O_RDWR | O_TRUNC; - else if (*p == 'w') - flags |= O_WRONLY | O_TRUNC; - - else if (*p == 'a' && *(p + 1) == '+') - flags |= O_RDWR | O_APPEND; - else if (*p == 'a') - flags |= O_WRONLY | O_APPEND; -#ifdef O_CLOEXEC - else if (*p == *UL_CLOEXECSTR) - flags |= O_CLOEXEC; -#endif - } - - return flags; -} - FILE *ul_path_fopen(struct path_cxt *pc, const char *mode, const char *path) { - int flags = mode2flags(mode); + int flags = ul_mode_to_flags(mode); int fd = ul_path_open(pc, flags, path); if (fd < 0)