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.
This commit is contained in:
Lennart Poettering
2023-05-03 17:55:55 +02:00
parent 8aefedce14
commit 9a22b098d2
+13 -25
View 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;