Skip to content

Commit

Permalink
util: fix uses of spdk_posix_file_load_from_name
Browse files Browse the repository at this point in the history
Changes for https://review.spdk.io/gerrit/c/spdk/spdk/+/21829 introduced a bug
in nvmf_ns_reservation_load_json(): non-existing file for ptpl was no longer
ignored.

spdk_posix_file_load_from_name changed to return an error code, in order to be
able to distinguish between different error conditions, and to prevent to rely
on global errno.

Signed-off-by: Dmitry Savitskiy <[email protected]>
  • Loading branch information
dsavitskiy committed May 30, 2024
1 parent 975f663 commit 5b2ac33
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 17 deletions.
6 changes: 4 additions & 2 deletions include/spdk/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ void *spdk_posix_file_load(FILE *file, size_t *size);
*
* \param file_name File name.
* \param size Size of bytes read from the file.
* \param file_data Pointer to write a pointer to the data containing the content on success, or
* NULL is written on failure.
*
* \return data containing the content on success, NULL on failure.
* \return 0 on success, negative errno error code on failure.
*/
void *spdk_posix_file_load_from_name(const char *file_name, size_t *size);
int spdk_posix_file_load_from_name(const char *file_name, size_t *size, void **file_data);

#ifdef __cplusplus
}
Expand Down
5 changes: 3 additions & 2 deletions lib/event/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,9 @@ spdk_app_start(struct spdk_app_opts *opts_user, spdk_msg_fn start_fn,
return 1;
}

g_spdk_app.json_data = spdk_posix_file_load_from_name(opts->json_config_file,
&g_spdk_app.json_data_size);
spdk_posix_file_load_from_name(opts->json_config_file,
&g_spdk_app.json_data_size,
&g_spdk_app.json_data);
if (!g_spdk_app.json_data) {
SPDK_ERRLOG("Read JSON configuration file %s failed: %s\n",
opts->json_config_file, spdk_strerror(errno));
Expand Down
2 changes: 1 addition & 1 deletion lib/init/json_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ spdk_subsystem_init_from_json_config(const char *json_config_file, const char *r

assert(cb_fn);

json = spdk_posix_file_load_from_name(json_config_file, &json_size);
spdk_posix_file_load_from_name(json_config_file, &json_size, (void **)&json);
if (!json) {
SPDK_ERRLOG("Could not read JSON config file\n");
cb_fn(-EINVAL, cb_arg);
Expand Down
14 changes: 11 additions & 3 deletions lib/nvmf/subsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -2538,6 +2538,7 @@ static int
nvmf_ns_reservation_load_json(const struct spdk_nvmf_ns *ns,
struct spdk_nvmf_reservation_info *info)
{
int err;
size_t json_size;
ssize_t values_cnt, rc;
void *json = NULL, *end;
Expand All @@ -2547,10 +2548,17 @@ nvmf_ns_reservation_load_json(const struct spdk_nvmf_ns *ns,
uint32_t i;

/* Load all persist file contents into a local buffer */
json = spdk_posix_file_load_from_name(file, &json_size);
err = spdk_posix_file_load_from_name(file, &json_size, &json);

/* It's not an error if the file does not exist */
if (err == -ENOENT) {
SPDK_NOTICELOG("Persist file %s does not exist\n", file);
return 0;
}

if (!json) {
SPDK_ERRLOG("Load persit file %s failed\n", file);
return -ENOMEM;
SPDK_ERRLOG("Load persist file %s failed: %s\n", file, strerror(-err));
return err;
}

rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0);
Expand Down
18 changes: 12 additions & 6 deletions lib/util/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,24 @@ spdk_posix_file_load(FILE *file, size_t *size)
return NULL;
}

void *
spdk_posix_file_load_from_name(const char *file_name, size_t *size)
int
spdk_posix_file_load_from_name(const char *file_name, size_t *size, void **file_data)
{
FILE *file = fopen(file_name, "r");
void *data;

assert(file_data);

if (file == NULL) {
return NULL;
*file_data = NULL;
return -errno;
}

data = spdk_posix_file_load(file, size);
*file_data = spdk_posix_file_load(file, size);
fclose(file);

return data;
if (*file_data) {
return 0;
} else {
return -ENOMEM;
}
}
2 changes: 1 addition & 1 deletion test/app/fuzz/common/fuzz_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ read_json_into_buffer(const char *filename, struct spdk_json_val **values, void
size_t file_data_size;
ssize_t num_json_values = 0, rc;

*file_data = spdk_posix_file_load_from_name(filename, &file_data_size);
spdk_posix_file_load_from_name(filename, &file_data_size, file_data);
if (*file_data == NULL) {
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion test/app/fuzz/llvm_nvme_fuzz/llvm_nvme_fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ nvme_fuzz_parse(int ch, char *arg)
}
break;
case 'N':
g_repro_data = spdk_posix_file_load_from_name(optarg, &g_repro_size);
spdk_posix_file_load_from_name(optarg, &g_repro_size, &g_repro_data);
if (g_repro_data == NULL) {
fprintf(stderr, "could not load data for file %s\n", optarg);
return -1;
Expand Down
2 changes: 1 addition & 1 deletion test/app/fuzz/llvm_vfio_fuzz/llvm_vfio_fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ vfio_fuzz_parse(int ch, char *arg)
}
break;
case 'N':
g_repro_data = spdk_posix_file_load_from_name(optarg, &g_repro_size);
spdk_posix_file_load_from_name(optarg, &g_repro_size, &g_repro_data);
if (g_repro_data == NULL) {
fprintf(stderr, "could not load data for file %s\n", optarg);
return -1;
Expand Down

0 comments on commit 5b2ac33

Please sign in to comment.