]> git.wh0rd.org - home.git/commitdiff
ddnuke: make block size & count cli options
authorMike Frysinger <vapier@gentoo.org>
Fri, 13 Oct 2023 07:24:34 +0000 (13:09 +0545)
committerMike Frysinger <vapier@gentoo.org>
Fri, 13 Oct 2023 07:24:34 +0000 (13:09 +0545)
ddnuke.c

index 388a1441e224010992c8758f16ecfce155b8a935..02c6c215fa56762074a9675c8c9f3a6d6bcce0b3 100644 (file)
--- 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 <size>     How many bytes to write at once. (default: 1Mib)\n"
+               " -f <count>    How many blocks to write before flush (stdout & disk). (default: 32)\n"
                " -o <offset>   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;