sd-json: accept NULL path in sd_json_parse_file_at() too, port one manual fdopen() to it (#39538)

This commit is contained in:
Yu Watanabe
2025-11-05 01:40:27 +09:00
committed by GitHub
3 changed files with 7 additions and 17 deletions

View File

@@ -1011,7 +1011,7 @@ static int xfopenat_regular(int dir_fd, const char *path, const char *mode, int
assert(mode);
assert(ret);
if (dir_fd == AT_FDCWD && open_flags == 0 && path)
if (dir_fd == AT_FDCWD && path && open_flags == 0)
f = fopen(path, mode);
else {
_cleanup_close_ int fd = -EBADF;
@@ -1029,7 +1029,7 @@ static int xfopenat_regular(int dir_fd, const char *path, const char *mode, int
if (dir_fd == AT_FDCWD)
return -EBADF;
fd = fd_reopen(dir_fd, mode_flags | open_flags);
fd = fd_reopen(dir_fd, (mode_flags | open_flags) & ~O_NOFOLLOW);
if (fd < 0)
return fd;
}

View File

@@ -3453,10 +3453,8 @@ _public_ int sd_json_parse_file_at(
if (f)
r = read_full_stream(f, &text, NULL);
else if (path)
r = read_full_file_full(dir_fd, path, UINT64_MAX, SIZE_MAX, 0, NULL, &text, NULL);
else
return -EINVAL;
r = read_full_file_full(dir_fd, path, UINT64_MAX, SIZE_MAX, 0, NULL, &text, NULL);
if (r < 0)
return r;

View File

@@ -221,10 +221,8 @@ static int job_new(JobType type, Target *t, sd_bus_message *msg, JobComplete com
}
static int job_parse_child_output(int _fd, sd_json_variant **ret) {
_cleanup_close_ int fd = ASSERT_FD(_fd); /* Take ownership of the passed fd */
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
/* Take ownership of the passed fd */
_cleanup_close_ int fd = _fd;
_cleanup_fclose_ FILE *f = NULL;
struct stat st;
int r;
@@ -240,16 +238,10 @@ static int job_parse_child_output(int _fd, sd_json_variant **ret) {
return 0;
}
if (lseek(fd, SEEK_SET, 0) == (off_t) -1)
return log_debug_errno(errno, "Failed to seek to beginning of memfd: %m");
f = take_fdopen(&fd, "r");
if (!f)
return log_debug_errno(errno, "Failed to reopen memfd: %m");
r = sd_json_parse_file(f, "stdout", 0, &v, NULL, NULL);
r = sd_json_parse_file_at(/* f = */ NULL, fd, /* path = */ NULL, /* flags = */ 0,
&v, /* reterr_line = */ NULL, /* reterr_column = */ NULL);
if (r < 0)
return log_debug_errno(r, "Failed to parse JSON: %m");
return log_debug_errno(r, "Failed to parse child output as JSON: %m");
*ret = TAKE_PTR(v);
return 0;