From f325aaf3417590a29d1c362a118532ea11fb020d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 8 Jan 2019 18:46:47 +0100 Subject: [PATCH] json: add json_parse_file_at() helper This is an "at" function, similar to json_parse_file(). --- src/shared/json.c | 4 ++-- src/shared/json.h | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/shared/json.c b/src/shared/json.c index b9a4fe4e23e..377a2c455a2 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -2833,7 +2833,7 @@ int json_parse_continue(const char **p, JsonVariant **ret, unsigned *ret_line, u return json_parse_internal(p, NULL, ret, ret_line, ret_column, true); } -int json_parse_file(FILE *f, const char *path, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { +int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { _cleanup_(json_source_unrefp) JsonSource *source = NULL; _cleanup_free_ char *text = NULL; const char *p; @@ -2842,7 +2842,7 @@ int json_parse_file(FILE *f, const char *path, JsonVariant **ret, unsigned *ret_ if (f) r = read_full_stream(f, &text, NULL); else if (path) - r = read_full_file(path, &text, NULL); + r = read_full_file_full(dir_fd, path, 0, &text, NULL); else return -EINVAL; if (r < 0) diff --git a/src/shared/json.h b/src/shared/json.h index 8e589a0b842..dd1aff450ab 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once +#include #include #include #include @@ -175,7 +176,11 @@ int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *valu int json_parse(const char *string, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column); int json_parse_continue(const char **p, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column); -int json_parse_file(FILE *f, const char *path, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column); +int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column); + +static inline int json_parse_file(FILE *f, const char *path, JsonVariant **ret, unsigned *ret_line, unsigned *ret_column) { + return json_parse_file_at(f, AT_FDCWD, path, ret, ret_line, ret_column); +} enum { _JSON_BUILD_STRING,