diff options
Diffstat (limited to 'sys-utils/blkdiscard.c')
-rw-r--r-- | sys-utils/blkdiscard.c | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/sys-utils/blkdiscard.c b/sys-utils/blkdiscard.c index 0e15c01b0..af9ed6630 100644 --- a/sys-utils/blkdiscard.c +++ b/sys-utils/blkdiscard.c @@ -130,18 +130,17 @@ int main(int argc, char **argv) usage(stderr); } - if (stat(path, &sb) == -1) - err(EXIT_FAILURE, _("stat failed %s"), path); - if (!S_ISBLK(sb.st_mode)) - errx(EXIT_FAILURE, _("%s: not a block device"), path); - fd = open(path, O_WRONLY); if (fd < 0) err(EXIT_FAILURE, _("cannot open %s"), path); + if (fstat(fd, &sb) == -1) + err(EXIT_FAILURE, _("stat failed %s"), path); + if (!S_ISBLK(sb.st_mode)) + errx(EXIT_FAILURE, _("%s: not a block device"), path); + if (ioctl(fd, BLKGETSIZE64, &blksize)) err(EXIT_FAILURE, _("%s: BLKGETSIZE64 ioctl failed"), path); - if (ioctl(fd, BLKSSZGET, &secsize)) err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path); |