Commit 5434834a authored by Krzysztof Karas's avatar Krzysztof Karas Committed by Tomasz Zawadzki
Browse files

util: use spdk_posix_file_load_from_name



Use new function accross SPDK, instead of reading file contents
locally, so users calling this function will be freed from handling
opening and closing the files.

Change-Id: I7a7a9aa1151500f6abd67e894c1ba2b6891cc966
Signed-off-by: default avatarKrzysztof Karas <krzysztof.karas@intel.com>
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/21829


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Community-CI: Mellanox Build Bot
Reviewed-by: default avatarTomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: default avatarJim Harris <jim.harris@samsung.com>
parent 2284b56b
Loading
Loading
Loading
Loading
+1 −17
Original line number Diff line number Diff line
@@ -499,22 +499,6 @@ app_json_config_load_subsystem(void *_ctx)
	app_json_config_load_subsystem_config_entry(ctx);
}

static void *
read_file(const char *filename, size_t *size)
{
	FILE *file = fopen(filename, "r");
	void *data;

	if (file == NULL) {
		/* errno is set by fopen */
		return NULL;
	}

	data = spdk_posix_file_load(file, size);
	fclose(file);
	return data;
}

static int
parse_json(void *json, ssize_t json_size, struct load_json_config_ctx *ctx)
{
@@ -650,7 +634,7 @@ spdk_subsystem_init_from_json_config(const char *json_config_file, const char *r

	assert(cb_fn);

	json = read_file(json_config_file, &json_size);
	json = spdk_posix_file_load_from_name(json_config_file, &json_size);
	if (!json) {
		SPDK_ERRLOG("Could not read JSON config file\n");
		cb_fn(-EINVAL, cb_arg);
+1 −10
Original line number Diff line number Diff line
@@ -2225,7 +2225,6 @@ static int
nvmf_ns_reservation_load_json(const struct spdk_nvmf_ns *ns,
			      struct spdk_nvmf_reservation_info *info)
{
	FILE *fd;
	size_t json_size;
	ssize_t values_cnt, rc;
	void *json = NULL, *end;
@@ -2234,16 +2233,8 @@ nvmf_ns_reservation_load_json(const struct spdk_nvmf_ns *ns,
	const char *file = ns->ptpl_file;
	uint32_t i;

	fd = fopen(file, "r");
	/* It's not an error if the file does not exist */
	if (!fd) {
		SPDK_NOTICELOG("File %s does not exist\n", file);
		return 0;
	}

	/* Load all persist file contents into a local buffer */
	json = spdk_posix_file_load(fd, &json_size);
	fclose(fd);
	json = spdk_posix_file_load_from_name(file, &json_size);
	if (!json) {
		SPDK_ERRLOG("Load persit file %s failed\n", file);
		return -ENOMEM;
+1 −10
Original line number Diff line number Diff line
@@ -102,23 +102,14 @@ fuzz_get_base_64_buffer_value(void *item, size_t len, char *buf, size_t buf_len)
static ssize_t
read_json_into_buffer(const char *filename, struct spdk_json_val **values, void **file_data)
{
	FILE *file = fopen(filename, "r");
	size_t file_data_size;
	ssize_t num_json_values = 0, rc;

	if (file == NULL) {
		/* errno is set by fopen */
		return 0;
	}

	*file_data = spdk_posix_file_load(file, &file_data_size);
	*file_data = spdk_posix_file_load_from_name(filename, &file_data_size);
	if (*file_data == NULL) {
		fclose(file);
		return 0;
	}

	fclose(file);

	num_json_values = spdk_json_parse(*file_data, file_data_size, NULL, 0, NULL,
					  SPDK_JSON_PARSE_FLAG_ALLOW_COMMENTS);

+1 −7
Original line number Diff line number Diff line
@@ -910,7 +910,6 @@ nvme_fuzz_parse(int ch, char *arg)
{
	long long tmp;
	int rc;
	FILE *repro_file;

	switch (ch) {
	case 'D':
@@ -929,12 +928,7 @@ nvme_fuzz_parse(int ch, char *arg)
		}
		break;
	case 'N':
		repro_file = fopen(optarg, "r");
		if (repro_file == NULL) {
			fprintf(stderr, "could not open %s: %s\n", optarg, spdk_strerror(errno));
			return -1;
		}
		g_repro_data = spdk_posix_file_load(repro_file, &g_repro_size);
		g_repro_data = spdk_posix_file_load_from_name(optarg, &g_repro_size);
		if (g_repro_data == NULL) {
			fprintf(stderr, "could not load data for file %s\n", optarg);
			return -1;
+1 −7
Original line number Diff line number Diff line
@@ -566,7 +566,6 @@ static int
vfio_fuzz_parse(int ch, char *arg)
{
	long long tmp = 0;
	FILE *repro_file = NULL;

	switch (ch) {
	case 'D':
@@ -584,12 +583,7 @@ vfio_fuzz_parse(int ch, char *arg)
		}
		break;
	case 'N':
		repro_file = fopen(optarg, "r");
		if (repro_file == NULL) {
			fprintf(stderr, "could not open %s: %s\n", optarg, spdk_strerror(errno));
			return -1;
		}
		g_repro_data = spdk_posix_file_load(repro_file, &g_repro_size);
		g_repro_data = spdk_posix_file_load_from_name(optarg, &g_repro_size);
		if (g_repro_data == NULL) {
			fprintf(stderr, "could not load data for file %s\n", optarg);
			return -1;