From b2bb430b3989a172bd17dd402e0510a67708c73d Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Fri, 13 Oct 2023 13:09:34 +0545 Subject: [PATCH] ddnuke: make block size & count cli options --- ddnuke.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/ddnuke.c b/ddnuke.c index 388a144..02c6c21 100644 --- a/ddnuke.c +++ b/ddnuke.c @@ -65,9 +65,9 @@ static off_t get_size(int fd) err(EX_DATAERR, "unknown type of file"); } -static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern) +static void nuke(int fd, off_t block_size, off_t offset, off_t max_size, off_t flush_count, unsigned char pattern) { - static char mem[1024 * 1024]; + char mem[block_size]; memset(mem, pattern, sizeof(mem)); if (pattern) printf("Writing 0x%X to the output\n", pattern); @@ -87,7 +87,7 @@ static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern) /* This will round up to sizeof(mem) ... */ ret = write(fd, mem, sizeof(mem)); pos += ret; - if (ret != sizeof(mem)) { + if (ret != block_size) { if (pos != max_size) printf("\nread() returned %zi (wanted %zu)\n%'llu MB/s over entire run\n", ret, sizeof(mem), mbps(&itime, &etime, pos)); @@ -95,11 +95,11 @@ static void nuke(int fd, off_t offset, off_t max_size, unsigned char pattern) } unsigned long long eta = speed ? (max_size - pos) / (speed * 1000 * 1000) : 0; - printf("\r\e[2K%'llu bytes %u%% (%'llu MB/s) (ETA ~%llum%llus)", + printf("\r%'llu bytes %u%% (%'llu MB/s) (ETA ~%llum%llus)\e[K", (unsigned long long)pos, (unsigned)((pos * 100) / max_size), speed, eta / 60, eta % 60); - if ((++fsync_pos % 32) == 0) { + if ((++fsync_pos % flush_count) == 0) { speed = mbps(&stime, &etime, pos - last_pos); last_pos = pos; fflush(stdout); @@ -119,6 +119,8 @@ static void usage(int status) "Options:\n" " -q Run quietly\n" " -y Assume yes to all prompts\n" + " -b How many bytes to write at once. (default: 1Mib)\n" + " -f How many blocks to write before flush (stdout & disk). (default: 32)\n" " -o Position to start writing\n" " -r Write three times: 0x00, then 0xaa, then 0x55\n" ); @@ -127,6 +129,8 @@ static void usage(int status) int main(int argc, char *argv[]) { + off_t block_size = 1024 * 1024; + off_t flush_count = 32; off_t offset = 0; const char *file; bool quiet = false; @@ -136,8 +140,14 @@ int main(int argc, char *argv[]) setlocale(LC_NUMERIC, "en_US"); - while ((o = getopt(argc, argv, "ho:qry")) != -1) { + while ((o = getopt(argc, argv, "hb:f:o:qry")) != -1) { switch (o) { + case 'b': + block_size = atoll(optarg); + break; + case 'f': + flush_count = atoll(optarg); + break; case 'o': offset = atoll(optarg); break; @@ -186,10 +196,10 @@ int main(int argc, char *argv[]) off_t max_size = get_size(fd); - nuke(fd, offset, max_size, 0x00); + nuke(fd, block_size, offset, max_size, flush_count, 0x00); if (random) { - nuke(fd, offset, max_size, 0xaa); - nuke(fd, offset, max_size, 0x55); + nuke(fd, block_size, offset, max_size, flush_count, 0xaa); + nuke(fd, block_size, offset, max_size, flush_count, 0x55); } return EX_OK; -- 2.39.2