]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: use stdout for exporting TLS keys
authorDaniel Wagner <dwagner@suse.de>
Mon, 1 Jul 2024 09:41:11 +0000 (11:41 +0200)
committerDaniel Wagner <wagi@monom.org>
Tue, 9 Jul 2024 09:29:47 +0000 (11:29 +0200)
The export command should use stdout as default output as documentation
claims. Furthermore, it should open the keyfile to write in write mode.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
nvme.c

diff --git a/nvme.c b/nvme.c
index 08553e5664b004d272cf8697cad3dadf29c6daef..28fac1cc06de03ca5f78e900862dec92af9a1d4e 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -9477,14 +9477,25 @@ static int tls_key(int argc, char **argv, struct command *command, struct plugin
                return err;
 
        if (cfg.keyfile) {
-               fd = fopen(cfg.keyfile, "r");
+               const char *mode;
+
+               if (cfg.import)
+                       mode = "r";
+               else
+                       mode = "w";
+
+               fd = fopen(cfg.keyfile, mode);
                if (!fd) {
                        nvme_show_error("Cannot open keyfile %s, error %d\n",
                                        cfg.keyfile, errno);
                        return -errno;
                }
-       } else
-               fd = stdin;
+       } else {
+               if (cfg.import)
+                       fd = freopen(NULL, "r", stdin);
+               else
+                       fd = freopen(NULL, "w", stdout);
+       }
 
        if (cfg.export && cfg.import) {
                nvme_show_error("Cannot specify both --import and --export");