summaryrefslogtreecommitdiff
path: root/src/tmpfiles
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-06-09 13:30:16 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-06-09 13:30:19 +0900
commit21e43a7c51ff5bc560e67e95f24fc1589d4e02a2 (patch)
treee0f87d1fcec32e285ba9163ac81813fd0ca62818 /src/tmpfiles
parent045d7232b5a551b9b65b458281bc920cde157f6f (diff)
downloadsystemd-21e43a7c51ff5bc560e67e95f24fc1589d4e02a2.tar.gz
tmpfile: several minor coding style fixes
This makes the followings: - reduces scope of variables, - drop unnecessary 'else' - use CLOSE_AND_REPLACE() macro - use strnull() for possible NULL string
Diffstat (limited to 'src/tmpfiles')
-rw-r--r--src/tmpfiles/tmpfiles.c38
1 files changed, 20 insertions, 18 deletions
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 197bb5d223..527ff5d49c 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -2033,7 +2033,7 @@ static int glob_item_recursively(Item *i, fdaction_t action) {
static int rm_if_wrong_type_safe(
mode_t mode,
int parent_fd,
- const struct stat *parent_st /* Only used if follow is true. */,
+ const struct stat *parent_st, /* Only used if follow_links below is true. */
const char *name,
int flags) {
_cleanup_free_ char *parent_name = NULL;
@@ -2110,12 +2110,10 @@ static int rm_if_wrong_type_safe(
/* If child_mode is non-zero, rm_if_wrong_type_safe will be executed for the last path component. */
static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) {
- _cleanup_close_ int parent_fd = -1, next_fd = -1;
- _cleanup_free_ char *parent_name = NULL;
+ _cleanup_close_ int parent_fd = -1;
struct stat parent_st;
- const char *s, *e;
- int r;
size_t path_len;
+ int r;
assert(path);
assert((child_mode & ~S_IFMT) == 0);
@@ -2141,9 +2139,10 @@ static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) {
return log_error_errno(errno, "Failed to stat root: %m");
/* Check every parent directory in the path, except the last component */
- e = path;
- for (;;) {
+ for (const char *e = path;;) {
+ _cleanup_close_ int next_fd = -1;
char t[path_len + 1];
+ const char *s;
/* Find the start of the next path component. */
s = e + strspn(e, "/");
@@ -2156,19 +2155,19 @@ static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) {
/* Is this the last component? If so, then check the type */
if (*e == 0)
return child_mode != 0 ? rm_if_wrong_type_safe(child_mode, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW) : 0;
- else {
- r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, 0);
- /* Remove dangling symlinks. */
- if (r == -ENOENT)
- r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW);
- }
+ r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, 0);
+ /* Remove dangling symlinks. */
+ if (r == -ENOENT)
+ r = rm_if_wrong_type_safe(S_IFDIR, parent_fd, &parent_st, t, AT_SYMLINK_NOFOLLOW);
if (r == -ENOENT) {
RUN_WITH_UMASK(0000)
r = mkdirat_label(parent_fd, t, 0755);
if (r < 0) {
+ _cleanup_free_ char *parent_name = NULL;
+
(void) fd_get_path(parent_fd, &parent_name);
- return log_error_errno(r, "Failed to mkdir \"%s\" at \"%s\": %m", t, parent_name);
+ return log_error_errno(r, "Failed to mkdir \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
} else if (r < 0)
/* rm_if_wrong_type_safe already logs errors. */
@@ -2176,18 +2175,21 @@ static int mkdir_parents_rm_if_wrong_type(mode_t child_mode, const char *path) {
next_fd = openat(parent_fd, t, O_NOCTTY | O_CLOEXEC | O_DIRECTORY);
if (next_fd < 0) {
+ _cleanup_free_ char *parent_name = NULL;
+
r = -errno;
(void) fd_get_path(parent_fd, &parent_name);
- return log_error_errno(r, "Failed to open \"%s\" at \"%s\": %m", t, parent_name);
+ return log_error_errno(r, "Failed to open \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
if (fstat(next_fd, &parent_st) < 0) {
+ _cleanup_free_ char *parent_name = NULL;
+
r = -errno;
(void) fd_get_path(parent_fd, &parent_name);
- return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, parent_name);
+ return log_error_errno(r, "Failed to stat \"%s\" at \"%s\": %m", t, strnull(parent_name));
}
- safe_close(parent_fd);
- parent_fd = TAKE_FD(next_fd);
+ CLOSE_AND_REPLACE(parent_fd, next_fd);
}
}