]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
vfs: move mode2flags() to include/vfs.h as ul_mode_to_flags()
authorKarel Zak <kzak@redhat.com>
Tue, 30 Jun 2026 14:53:56 +0000 (16:53 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 9 Jul 2026 10:18:04 +0000 (12:18 +0200)
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 <kzak@redhat.com>
include/vfs.h
lib/path.c

index 97479ec0aa7af15715e1af72d6216c0ac9654290..50d8c27d51234a8548fd40b4d02b7833b837b533 100644 (file)
@@ -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 */
index 2a30bf9e74bb833933469bfee75a985e454d65f3..65a7b5648de37228655aa0cc865c0193110e8942 100644 (file)
@@ -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)