]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: teach xopenat_full() to pick automatically if given as MODE_INVALID
authorLennart Poettering <lennart@poettering.net>
Thu, 14 Nov 2024 11:20:07 +0000 (12:20 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 3 Jan 2025 11:58:43 +0000 (12:58 +0100)
src/basic/chase.c
src/basic/fs-util.c

index 8eac356665562e8b9120deeef499048ffe8b92a5..43fad0d93ff5a1c389265ff6ed59cad6d71e5ff4 100644 (file)
@@ -744,10 +744,15 @@ int chase_extract_filename(const char *path, const char *root, char **ret) {
         return strdup_to(ret, ".");
 }
 
-int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, int open_flags, char **ret_path) {
+int chase_and_open(
+                const char *path,
+                const char *root,
+                ChaseFlags chase_flags,
+                int open_flags,
+                char **ret_path) {
+
         _cleanup_close_ int path_fd = -EBADF;
         _cleanup_free_ char *p = NULL, *fname = NULL;
-        mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644;
         int r;
 
         assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
@@ -758,7 +763,7 @@ int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, i
                 return xopenat_full(AT_FDCWD, path,
                                     open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
                                     /* xopen_flags = */ 0,
-                                    mode);
+                                    MODE_INVALID);
 
         r = chase(path, root, CHASE_PARENT|chase_flags, &p, &path_fd);
         if (r < 0)
@@ -772,7 +777,7 @@ int chase_and_open(const char *path, const char *root, ChaseFlags chase_flags, i
                         return r;
         }
 
-        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags = */ 0, mode);
+        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags = */ 0, MODE_INVALID);
         if (r < 0)
                 return r;
 
@@ -948,10 +953,15 @@ int chase_and_open_parent(const char *path, const char *root, ChaseFlags chase_f
         return pfd;
 }
 
-int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int open_flags, char **ret_path) {
+int chase_and_openat(
+                int dir_fd,
+                const char *path,
+                ChaseFlags chase_flags,
+                int open_flags,
+                char **ret_path) {
+
         _cleanup_close_ int path_fd = -EBADF;
         _cleanup_free_ char *p = NULL, *fname = NULL;
-        mode_t mode = open_flags & O_DIRECTORY ? 0755 : 0644;
         int r;
 
         assert(!(chase_flags & (CHASE_NONEXISTENT|CHASE_STEP)));
@@ -962,7 +972,7 @@ int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int o
                 return xopenat_full(dir_fd, path,
                                     open_flags | (FLAGS_SET(chase_flags, CHASE_NOFOLLOW) ? O_NOFOLLOW : 0),
                                     /* xopen_flags = */ 0,
-                                    mode);
+                                    MODE_INVALID);
 
         r = chaseat(dir_fd, path, chase_flags|CHASE_PARENT, &p, &path_fd);
         if (r < 0)
@@ -974,7 +984,7 @@ int chase_and_openat(int dir_fd, const char *path, ChaseFlags chase_flags, int o
                         return r;
         }
 
-        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags = */ 0, mode);
+        r = xopenat_full(path_fd, strempty(fname), open_flags|O_NOFOLLOW, /* xopen_flags= */ 0, MODE_INVALID);
         if (r < 0)
                 return r;
 
index 4ede324c34c5965169d36c59ff0547b59182d90a..982c3bd5c3e33d85cd7d5fc1dbfd73b9741c2a22 100644 (file)
@@ -1133,8 +1133,13 @@ int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_
          *   • If the path is specified NULL or empty, behaves like fd_reopen().
          *
          *   • If XO_NOCOW is specified will turn on the NOCOW btrfs flag on the file, if available.
+         *
+         *   • If mode is specified as MODE_INVALID, we'll use 0755 for dirs, and 0644 for regular files.
          */
 
+        if (mode == MODE_INVALID)
+                mode = (open_flags & O_DIRECTORY) ? 0755 : 0644;
+
         if (isempty(path)) {
                 assert(!FLAGS_SET(open_flags, O_CREAT|O_EXCL));
                 return fd_reopen(dir_fd, open_flags & ~O_NOFOLLOW);