Skip to content

Commit 74b30d2

Browse files
committed
json: update nvme_dump_config to a file descriptor
This function can be made a bit more flexible where the output is dumped to by passing in a file descriptor where to dump to. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 1e95590 commit 74b30d2

8 files changed

Lines changed: 28 additions & 28 deletions

File tree

fabrics.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ int fabrics_discovery(const char *desc, int argc, char **argv, bool connect)
575575

576576
out_free:
577577
if (dump_config)
578-
nvme_dump_config(ctx, NULL);
578+
nvme_dump_config(ctx, STDOUT_FILENO);
579579

580580
return ret;
581581
}
@@ -678,7 +678,7 @@ int fabrics_connect(const char *desc, int argc, char **argv)
678678
}
679679

680680
if (dump_config)
681-
nvme_dump_config(ctx, NULL);
681+
nvme_dump_config(ctx, STDERR_FILENO);
682682

683683
return 0;
684684
}
@@ -951,11 +951,16 @@ int fabrics_config(const char *desc, int argc, char **argv)
951951
}
952952
}
953953

954-
if (update_config)
955-
nvme_dump_config(ctx, config_file);
954+
if (update_config) {
955+
_cleanup_fd_ int fd = -1;
956+
957+
fd = open(config_file, O_RDONLY, 0);
958+
if (fd != -1)
959+
nvme_dump_config(ctx, fd);
960+
}
956961

957962
if (dump_config)
958-
nvme_dump_config(ctx, NULL);
963+
nvme_dump_config(ctx, STDOUT_FILENO);
959964

960965
return 0;
961966
}

libnvme/libnvme/nvme.i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ struct nvme_ns {
529529
nvme_refresh_topology($self);
530530
}
531531
void dump_config() {
532-
nvme_dump_config($self, NULL);
532+
nvme_dump_config($self, STDERR_FILENO);
533533
}
534534
}
535535

libnvme/src/nvme/json.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ static void json_update_subsys(struct json_object *subsys_array,
386386
}
387387
}
388388

389-
int json_update_config(struct nvme_global_ctx *ctx, const char *config_file)
389+
int json_update_config(struct nvme_global_ctx *ctx, int fd)
390390
{
391391
nvme_host_t h;
392392
struct json_object *json_root, *host_obj;
@@ -432,18 +432,12 @@ int json_update_config(struct nvme_global_ctx *ctx, const char *config_file)
432432
json_object_put(host_obj);
433433
}
434434
}
435-
if (!config_file) {
436-
ret = json_object_to_fd(1, json_root,
437-
JSON_C_TO_STRING_PRETTY |
438-
JSON_C_TO_STRING_NOSLASHESCAPE);
439-
printf("\n");
440-
} else
441-
ret = json_object_to_file_ext(config_file, json_root,
442-
JSON_C_TO_STRING_PRETTY |
443-
JSON_C_TO_STRING_NOSLASHESCAPE);
435+
ret = json_object_to_fd(fd, json_root,
436+
JSON_C_TO_STRING_PRETTY |
437+
JSON_C_TO_STRING_NOSLASHESCAPE);
438+
write(fd, "\n", 1);
444439
if (ret < 0) {
445-
nvme_msg(ctx, LOG_ERR, "Failed to write to %s, %s\n",
446-
config_file ? "stdout" : config_file,
440+
nvme_msg(ctx, LOG_ERR, "Failed to write JSON config file: %s\n",
447441
json_util_get_last_err());
448442
ret = -EIO;
449443
}

libnvme/src/nvme/private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ int nvme_set_attr(const char *dir, const char *attr, const char *value);
391391

392392
int json_read_config(struct nvme_global_ctx *ctx, const char *config_file);
393393

394-
int json_update_config(struct nvme_global_ctx *ctx, const char *config_file);
394+
int json_update_config(struct nvme_global_ctx *ctx, int fd);
395395

396396
int json_dump_tree(struct nvme_global_ctx *ctx);
397397

libnvme/src/nvme/tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,9 @@ int nvme_scan(const char *config_file, struct nvme_global_ctx **ctxp)
406406
return ret;
407407
}
408408

409-
int nvme_dump_config(struct nvme_global_ctx *ctx, const char *config_file)
409+
int nvme_dump_config(struct nvme_global_ctx *ctx, int fd)
410410
{
411-
return json_update_config(ctx, config_file);
411+
return json_update_config(ctx, fd);
412412
}
413413

414414
int nvme_dump_tree(struct nvme_global_ctx *ctx)

libnvme/src/nvme/tree.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,15 +1398,14 @@ void nvme_refresh_topology(struct nvme_global_ctx *ctx);
13981398
/**
13991399
* nvme_dump_config() - Print the JSON configuration
14001400
* @ctx: &struct nvme_global_ctx object
1401-
* @config_file: JSON configuration file.
1401+
* @fd: File descriptor to write the JSON configuration.
14021402
*
1403-
* Prints the current contents of the JSON configuration
1404-
* file to the config_file. If connfig_file is not provided it prints
1405-
* to stdout.
1403+
* Writes the current contents of the JSON configuration
1404+
* to the file descriptor fd.
14061405
*
14071406
* Return: 0 on success, or negative error code otherwise.
14081407
*/
1409-
int nvme_dump_config(struct nvme_global_ctx *ctx, const char *config_file);
1408+
int nvme_dump_config(struct nvme_global_ctx *ctx, int fd);
14101409

14111410
/**
14121411
* nvme_dump_tree() - Dump internal object tree

libnvme/test/config/config-dump.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string.h>
88
#include <stdbool.h>
99
#include <stdlib.h>
10+
#include <unistd.h>
1011
#include <errno.h>
1112

1213
#include <libnvme.h>
@@ -29,7 +30,7 @@ static bool config_dump(const char *file)
2930
if (err)
3031
goto out;
3132

32-
err = nvme_dump_config(ctx, NULL);
33+
err = nvme_dump_config(ctx, STDOUT_FILENO);
3334
if (err)
3435
goto out;
3536

libnvme/test/config/psk-json.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string.h>
1010
#include <stdbool.h>
1111
#include <stdlib.h>
12+
#include <unistd.h>
1213
#include <errno.h>
1314

1415
#include <libnvme.h>
@@ -69,7 +70,7 @@ static bool psk_json_test(char *file)
6970
if (!import_export_key(ctx, c))
7071
goto out;
7172

72-
err = nvme_dump_config(ctx, NULL);
73+
err = nvme_dump_config(ctx, STDOUT_FILENO);
7374
if (err)
7475
goto out;
7576

0 commit comments

Comments
 (0)