[git commit] shred: implement -s SIZE

Denys Vlasenko vda.linux at googlemail.com
Sun Jun 20 11:08:50 UTC 2021


commit: https://git.busybox.net/busybox/commit/?id=ec3170ac9578d4a42423e5a966a8ba668df68337
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
shred_main                                           337     391     +54
.rodata                                           103393  103395      +2
packed_usage                                       33666   33656     -10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 56/-10)             Total: 46 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 coreutils/shred.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/coreutils/shred.c b/coreutils/shred.c
index 69fb3e8cc..4b95d197c 100644
--- a/coreutils/shred.c
+++ b/coreutils/shred.c
@@ -15,14 +15,15 @@
 //kbuild:lib-$(CONFIG_SHRED) += shred.o
 
 //usage:#define shred_trivial_usage
-//usage:       "[-fuz] [-n N] FILE..."
+//usage:       "[-fuz] [-n N] [-s SIZE] FILE..."
 //usage:#define shred_full_usage "\n\n"
 //usage:       "Overwrite/delete FILEs\n"
 //usage:     "\n	-f	Chmod to ensure writability"
+//usage:     "\n	-s SIZE	Size to write"
 //usage:     "\n	-n N	Overwrite N times (default 3)"
 //usage:     "\n	-z	Final overwrite with zeros"
 //usage:     "\n	-u	Remove file"
-//-x and -v are accepted but have no effect
+//-x (exact: don't round up to 4k) and -v (verbose) are accepted but have no effect
 
 /* shred (GNU coreutils) 8.25:
 -f, --force		change permissions to allow writing if necessary
@@ -41,6 +42,7 @@
 int shred_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int shred_main(int argc UNUSED_PARAM, char **argv)
 {
+	char *opt_s;
 	int rand_fd = rand_fd; /* for compiler */
 	int zero_fd;
 	unsigned num_iter = 3;
@@ -52,9 +54,10 @@ int shred_main(int argc UNUSED_PARAM, char **argv)
 		OPT_n = (1 << 3),
 		OPT_v = (1 << 4),
 		OPT_x = (1 << 5),
+		OPT_s = (1 << 6),
 	};
 
-	opt = getopt32(argv, "^" "fuzn:+vx" "\0" "-1"/*min 1 arg*/, &num_iter);
+	opt = getopt32(argv, "^" "fuzn:+vxs:" "\0" "-1"/*min 1 arg*/, &num_iter, &opt_s);
 	argv += optind;
 
 	zero_fd = xopen("/dev/zero", O_RDONLY);
@@ -82,6 +85,11 @@ int shred_main(int argc UNUSED_PARAM, char **argv)
 		if (fstat(fd, &sb) == 0 && sb.st_size > 0) {
 			off_t size = sb.st_size;
 
+			if (opt & OPT_s) {
+				size = BB_STRTOOFF(opt_s, NULL, 0); /* accepts oct/hex */
+				if (errno || size < 0) bb_show_usage();
+			}
+
 			for (i = 0; i < num_iter; i++) {
 				bb_copyfd_size(rand_fd, fd, size);
 				fdatasync(fd);


More information about the busybox-cvs mailing list