]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: refactor S_ISCHR/S_ISBLK with helpers
authorMinwoo Im <minwoo.im.dev@gmail.com>
Thu, 7 Jan 2021 07:03:13 +0000 (16:03 +0900)
committerKeith Busch <kbusch@kernel.org>
Tue, 12 Jan 2021 22:13:43 +0000 (15:13 -0700)
This patch refactors S_ISCHR/S_ISBLK macros to helper functions for
readability.  The global variable nvme_stat does no longer need to be in
subcommand handlers.

Signed-off-by: Minwoo Im <minwoo.im.dev@gmail.com>
nvme.c

diff --git a/nvme.c b/nvme.c
index 03ba13706ba6a539fabe0dc55cbb80ff06758e48..a3bde5a8ab2bca5330a24e1f3e7f263260510c32 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -136,6 +136,16 @@ void *nvme_alloc(size_t len, bool *huge)
 }
 #endif
 
+static bool is_chardev(void)
+{
+       return S_ISCHR(nvme_stat.st_mode);
+}
+
+static bool is_blkdev(void)
+{
+       return S_ISBLK(nvme_stat.st_mode);
+}
+
 static int open_dev(char *dev)
 {
        int err, fd;
@@ -151,7 +161,7 @@ static int open_dev(char *dev)
                close(fd);
                goto perror;
        }
-       if (!S_ISCHR(nvme_stat.st_mode) && !S_ISBLK(nvme_stat.st_mode)) {
+       if (!is_chardev() && !is_blkdev()) {
                fprintf(stderr, "%s is not a block or character device\n", dev);
                close(fd);
                return -ENODEV;
@@ -3133,7 +3143,7 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
        else {
                printf("Success formatting namespace:%x\n", cfg.namespace_id);
                if (cfg.lbaf != prev_lbaf){
-                       if (S_ISCHR(nvme_stat.st_mode)) {
+                       if (is_chardev()) {
                                if(ioctl(fd, NVME_IOCTL_RESCAN) < 0){
                                        fprintf(stderr, "failed to rescan namespaces\n");
                                        err = -errno;
@@ -3163,7 +3173,7 @@ static int format(int argc, char **argv, struct command *cmd, struct plugin *plu
                                }
                        }
                }
-               if (cfg.reset && S_ISCHR(nvme_stat.st_mode))
+               if (cfg.reset && is_chardev())
                        nvme_reset_controller(fd);
        }