From: Keith Busch Date: Fri, 18 Mar 2016 14:52:06 +0000 (-0600) Subject: Code reuse: character device check X-Git-Tag: v0.6~9 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=d0f1bd0b01af02047295a766931733e93fa87cb1;p=users%2Fsagi%2Fnvme-cli.git Code reuse: character device check Moving character device sanity check to common function to reduce duplication. Signed-off-by: Keith Busch --- diff --git a/nvme-ioctl.c b/nvme-ioctl.c index 950f579c..6acfb85a 100644 --- a/nvme-ioctl.c +++ b/nvme-ioctl.c @@ -21,33 +21,31 @@ #include "nvme-ioctl.h" -int nvme_subsystem_reset(int fd) +void nvme_verify_chr(int fd) { static struct stat nvme_stat; int err = fstat(fd, &nvme_stat); - if (err < 0) - return err; + if (err < 0) { + perror("fstat"); + exit(err); + } if (!S_ISCHR(nvme_stat.st_mode)) { fprintf(stderr, - "Error: requesting subsystem reset on non-controller handle\n"); + "Error: requesting reset on non-controller handle\n"); exit(ENOTBLK); } +} + +int nvme_subsystem_reset(int fd) +{ + nvme_verify_chr(fd); return ioctl(fd, NVME_IOCTL_SUBSYS_RESET); } int nvme_reset_controller(int fd) { - static struct stat nvme_stat; - int err = fstat(fd, &nvme_stat); - - if (err < 0) - return err; - if (!S_ISCHR(nvme_stat.st_mode)) { - fprintf(stderr, - "Error: requesting reset on non-controller handle\n"); - exit(ENOTBLK); - } + nvme_verify_chr(fd); return ioctl(fd, NVME_IOCTL_RESET); }