From: Lennart Poettering Date: Fri, 28 Apr 2023 18:47:10 +0000 (+0200) Subject: base-filesystem: unify common parts of base_filesystem_create_fd() branches X-Git-Tag: v254-rc1~564^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9a22b098d257d2ab50b39b390cdea07f94616294;p=thirdparty%2Fsystemd.git base-filesystem: unify common parts of base_filesystem_create_fd() branches 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. --- diff --git a/src/shared/base-filesystem.c b/src/shared/base-filesystem.c index 37293cdb6d2..8a50cc6ebb5 100644 --- a/src/shared/base-filesystem.c +++ b/src/shared/base-filesystem.c @@ -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;