]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
base-filesystem: unify common parts of base_filesystem_create_fd() branches
authorLennart Poettering <lennart@poettering.net>
Fri, 28 Apr 2023 18:47:10 +0000 (20:47 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 3 May 2023 15:55:55 +0000 (17:55 +0200)
The error handling and fchmodat() invocation is pretty much the same in
the directory and symlink branches, hence make them the same.

No real change in behaviour. Just refactoring.

src/shared/base-filesystem.c

index 37293cdb6d2f43a2dc0e71af8c9cfba29b0b11b5..8a50cc6ebb5e43997fa181d98e483bcbbcf9aef9 100644 (file)
@@ -11,6 +11,7 @@
 #include "alloc-util.h"
 #include "architecture.h"
 #include "base-filesystem.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "log.h"
 #include "macro.h"
@@ -142,7 +143,7 @@ int base_filesystem_create_fd(int fd, const char *root, uid_t uid, gid_t gid) {
                 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
                         continue;
 
-                if (table[i].target) {
+                if (table[i].target) { /* Create as symlink? */
                         const char *target = NULL;
 
                         /* check if one of the targets exists */
@@ -169,38 +170,25 @@ int base_filesystem_create_fd(int fd, const char *root, uid_t uid, gid_t gid) {
                         if (!target)
                                 continue;
 
-                        if (symlinkat(target, fd, table[i].dir) < 0) {
-                                log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
-                                               "Failed to create symlink at %s/%s: %m", root, table[i].dir);
-
-                                if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
-                                        continue;
-
-                                return -errno;
-                        }
-
-                        if (uid_is_valid(uid) || gid_is_valid(gid))
-                                if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
-                                        return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
-
-                        continue;
+                        r = RET_NERRNO(symlinkat(target, fd, table[i].dir));
+                } else {
+                        /* Create as directory. */
+                        WITH_UMASK(0000)
+                                r = RET_NERRNO(mkdirat(fd, table[i].dir, table[i].mode));
                 }
-
-                WITH_UMASK(0000)
-                        r = mkdirat(fd, table[i].dir, table[i].mode);
                 if (r < 0) {
-                        log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
-                                       "Failed to create directory at %s/%s: %m", root, table[i].dir);
-
-                        if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
+                        bool ignore = IN_SET(r, -EEXIST, -EROFS) || table[i].ignore_failure;
+                        log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
+                                       "Failed to create %s/%s: %m", root, table[i].dir);
+                        if (ignore)
                                 continue;
 
-                        return -errno;
+                        return r;
                 }
 
                 if (uid_is_valid(uid) || gid_is_valid(gid))
                         if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
-                                return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
+                                return log_error_errno(errno, "Failed to chown %s/%s: %m", root, table[i].dir);
         }
 
         return 0;