Module Name:    src
Committed By:   martin
Date:           Sat Feb  3 12:09:07 UTC 2024

Modified Files:
        src/sbin/blkdiscard [netbsd-10]: blkdiscard.8 blkdiscard.c

Log Message:
Pull up following revision(s) (requested by mrg in ticket #565):

        sbin/blkdiscard/blkdiscard.c: revision 1.2
        sbin/blkdiscard/blkdiscard.c: revision 1.3
        sbin/blkdiscard/blkdiscard.8: revision 1.3

determine the tty width instead off writing 100 chars before a new line.

blkdiscard: avoid asserting when passed a bsd disklabel raw device

PR#57856 shows when using blkdiscard on eg, /dev/ld0 it asserts because
'0' is not between 'a' and 'p'.  switch this to using DISKPART() on the
returned st_rdev, so it works on 'ld0c' or 'ld0' (rawpart=2.)


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.2.2.1 src/sbin/blkdiscard/blkdiscard.8
cvs rdiff -u -r1.1 -r1.1.2.1 src/sbin/blkdiscard/blkdiscard.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/blkdiscard/blkdiscard.8
diff -u src/sbin/blkdiscard/blkdiscard.8:1.2 src/sbin/blkdiscard/blkdiscard.8:1.2.2.1
--- src/sbin/blkdiscard/blkdiscard.8:1.2	Sat Oct 15 21:53:21 2022
+++ src/sbin/blkdiscard/blkdiscard.8	Sat Feb  3 12:09:06 2024
@@ -1,4 +1,4 @@
-.\"	$NetBSD: blkdiscard.8,v 1.2 2022/10/15 21:53:21 andvar Exp $
+.\"	$NetBSD: blkdiscard.8,v 1.2.2.1 2024/02/03 12:09:06 martin Exp $
 .\"
 .\" Copyright (c) 2022 Matthew R. Green
 .\" All rights reserved.
@@ -24,7 +24,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd February 6, 2022
+.Dd January 13, 2024
 .Dt BLKDISCARD 8
 .Os
 .Sh NAME
@@ -156,7 +156,7 @@ The
 .Nm
 command was written by
 .An Matthew R. Green
-.Aq m...@eterna.com.au .
+.Aq m...@eterna23.net .
 .Sh BUGS
 The secure erase functionality of the
 .Fl s

Index: src/sbin/blkdiscard/blkdiscard.c
diff -u src/sbin/blkdiscard/blkdiscard.c:1.1 src/sbin/blkdiscard/blkdiscard.c:1.1.2.1
--- src/sbin/blkdiscard/blkdiscard.c:1.1	Mon Feb  7 09:33:26 2022
+++ src/sbin/blkdiscard/blkdiscard.c	Sat Feb  3 12:09:06 2024
@@ -1,7 +1,7 @@
-/*	$NetBSD: blkdiscard.c,v 1.1 2022/02/07 09:33:26 mrg Exp $	*/
+/*	$NetBSD: blkdiscard.c,v 1.1.2.1 2024/02/03 12:09:06 martin Exp $	*/
 
 /*
- * Copyright (c) 2019, 2020, 2022 Matthew R. Green
+ * Copyright (c) 2019, 2020, 2022, 2024 Matthew R. Green
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -45,14 +45,14 @@
 #include <unistd.h>
 #include <err.h>
 #include <errno.h>
-#include <assert.h>
 #include <stdbool.h>
 
 static bool secure = false;
 static bool zeroout = false;
 static char *zeros = NULL;
+static unsigned ttywidth = 80;
 
-#define FDISCARD_VERSION	20220206u
+#define FDISCARD_VERSION	20240113u
 
 static void __dead
 usage(const char* msg)
@@ -104,6 +104,7 @@ main(int argc, char *argv[])
 	int64_t val;
 	int i;
 	bool verbose = false;
+	struct stat sb;
 
 	/* Default /sbin/blkdiscard to be "run" */
 	bool norun = strcmp(getprogname(), "blkdiscard") != 0;
@@ -175,6 +176,7 @@ main(int argc, char *argv[])
 
 	if (size == 0) {
 		struct dkwedge_info dkw;
+
 		if (ioctl(fd, DIOCGWEDGEINFO, &dkw) == 0) {
 			size = dkw.dkw_size * DEV_BSIZE;
 			if (verbose)
@@ -183,24 +185,20 @@ main(int argc, char *argv[])
 		}
 	}
 
-	if (size == 0) {
+	if (size == 0 && fstat(fd, &sb) != -1) {
 		struct disklabel dl;
+
 		if (ioctl(fd, DIOCGDINFO, &dl) != -1) {
-			char partchar = name[strlen(name)-1];
-			assert(partchar >= 'a' && partchar <= 'p');
-			int part = partchar - 'a';
+			unsigned part = DISKPART(sb.st_rdev);
 
 			size = (uint64_t)dl.d_partitions[part].p_size *
 			    dl.d_secsize;
 			if (verbose)
-				printf("%s: disklabel size is %lld\n", name,
-				    (long long)size);
+				printf("%s: partition %u disklabel size is"
+				       " %lld\n", name, part, (long long)size);
 		}
-	}
 
-	if (size == 0) {
-		struct stat sb;
-		if (fstat(fd, &sb) != -1) {
+		if (size == 0) {
 			size = sb.st_size;
 			if (verbose)
 				printf("%s: stat size is %lld\n", name,
@@ -224,7 +222,9 @@ main(int argc, char *argv[])
 		size = end_offset;
 	}
 
-	if (verbose)
+	if (verbose) {
+		struct winsize winsize;
+
 		printf("%sgoing to %s on %s from byte %lld for "
 		       "%lld bytes, %lld at a time\n",
 		       norun ? "not " : "",
@@ -233,7 +233,12 @@ main(int argc, char *argv[])
 		       name, (long long)first_byte, (long long)size,
 		       (long long)max_per_call);
 
-	int loop = 0;
+		if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1 &&
+		    winsize.ws_col > 1)
+			ttywidth = winsize.ws_col - 1;
+	}
+
+	unsigned loop = 0;
 	while (size > 0) {
 		if (size > max_per_call)
 			discard_size = max_per_call;
@@ -248,7 +253,7 @@ main(int argc, char *argv[])
 		if (verbose) {
 			printf(".");
 			fflush(stdout);
-			if (loop++ > 100) {
+			if (++loop >= ttywidth) {
 				loop = 0;
 				printf("\n");
 			}

Reply via email to