diff --git a/.clang-format b/.clang-format deleted file mode 100644 index f5a3605..0000000 --- a/.clang-format +++ /dev/null @@ -1,20 +0,0 @@ -BasedOnStyle: LLVM -IndentWidth: 4 -TabWidth: 4 -UseTab: Never -ColumnLimit: 120 -AllowShortLoopsOnASingleLine: true -AllowShortFunctionsOnASingleLine: false -AlwaysBreakTemplateDeclarations: true -BreakConstructorInitializers: BeforeComma -AlignConsecutiveDeclarations: - Enabled: true - AcrossEmptyLines: false - AcrossComments: false - AlignCompound: false - AlignFunctionPointers: false - PadOperators: false -AlignConsecutiveMacros: true -AllowShortCaseLabelsOnASingleLine: true -SeparateDefinitionBlocks: Always -BinPackArguments: false diff --git a/Makefile b/Makefile index 58199f1..2b92b8c 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,13 @@ GITREV ?= $(shell git describe --dirty --always) BLDDATE ?= $(shell date -I) CR_YEAR ?= $(shell date +%Y) -VERSION ?= "v0.2.0" +VERSION ?= "v0.1.2" CFLAGS ?= -Wall -Wextra -Wpedantic -O2 -std=gnu99 CFLAGS += -DGITREV='"$(GITREV)"' CFLAGS += -DBLDDATE='"$(BLDDATE)"' CFLAGS += -DCR_YEAR='"$(CR_YEAR)"' CFLAGS += -DVERSION='$(VERSION)' -CFLAGS += $(EXTRA_CFLAGS) # Soon... # CFLAGS += $(shell pkg-config --cflags libudev) diff --git a/writeimg.c b/writeimg.c index 3d6c04b..6533771 100644 --- a/writeimg.c +++ b/writeimg.c @@ -11,11 +11,6 @@ #include #include -#if __linux__ -#include /* IOTCL flush number */ -#include /* IOCTL */ -#endif - #ifndef GITREV #define GITREV "unknown" #endif @@ -29,7 +24,7 @@ #endif #ifndef BLOCKSIZE -#define BLOCKSIZE (1024 * 1024) +#define BLOCKSIZE (4 * 1024 * 1024) #endif #define BYTES_TO_MIB(bts) ((double)bts / (1024 * 1024)) @@ -57,12 +52,11 @@ const char help[] = " writeimg [-v] -d \n" "\n" "Args:\n" - " Binary image file\n" - " -v Verify only\n" - " -d device Target block device\n" - " -h, --help Print this help message\n" - " -n, --noconfirm Do not ask for premission\n" - " -V, --version Print version\n" + " Binary image file\n" + " -v Verify only\n" + " -d device Target block device\n" + " -h, --help Print this help message\n" + " -V, --version Print version\n" "\0"; // clang-format on @@ -106,20 +100,15 @@ int perform_write(write_job_t *job) { int crc = crc32_init(); size_t b_written = 0; - while (42) { - fsync(block_fd); + while (!job->verify_only) { ssize_t read_bytes = read(file_fd, job->buffer, job->block_size); assert(read_bytes >= 0); if (read_bytes == 0) { + fsync(block_fd); crc = crc32_finalize(crc); - - if (!job->verify_only) { - printf("\nWriting done...\n"); - assert(job->total_bytes == b_written); - } else - assert(0 == b_written); - + printf("\nWriting done...\n"); + assert(job->total_bytes == b_written); break; /* Finished */ } @@ -131,15 +120,13 @@ int perform_write(write_job_t *job) { crc = crc32_update(crc, job->buffer, read_bytes); - if (!job->verify_only) { - ssize_t written_bytes = write(block_fd, job->buffer, read_bytes); - if (written_bytes < 0) { - fprintf(stderr, "%s: Write error\n", job->dev_name); - perror("Write"); - exit(EXIT_FAILURE); - } - print_progress(b_written += written_bytes, job->total_bytes); - } /* Else maybe give some helpful insights? */ + ssize_t written_bytes = write(block_fd, job->buffer, read_bytes); + if (written_bytes < 0) { + fprintf(stderr, "%s: Write error\n", job->dev_name); + perror("Write"); + exit(EXIT_FAILURE); + } + print_progress(b_written += written_bytes, job->total_bytes); } lseek(block_fd, 0, SEEK_SET); @@ -148,9 +135,6 @@ int perform_write(write_job_t *job) { memset(job->buffer, 0, BLOCKSIZE); memset(job->buffer2, 0, BLOCKSIZE); - /* This is essentially $ blockdev --flushbufs */ - ioctl(block_fd, BLKFLSBUF); - int crc_back = crc32_init(); b_written = 0; @@ -189,12 +173,16 @@ static const struct option longopts[] = { {"help", 0, 0, 'h'}, {"version", 0, 0, 'V'}, {"device", 1, 0, 'd'}, - {"noconfirm", 0, 0, 'n'}, {NULL, 0, 0, 0}, }; int main(int argc, char *argv[]) { printf("%s %s, Rev. %s\n", basename(argv[0]), VERSION, GITREV); + fprintf(stdout, copyright, CR_YEAR); +#ifdef BLDDATE + printf("Build date: %s\n", BLDDATE); +#endif + printf("\n"); /* Line buffering, system allocated */ setvbuf(stdout, NULL, _IOLBF, 0); @@ -205,22 +193,14 @@ int main(int argc, char *argv[]) { signal(SIGHUP, int_handler); signal(SIGTERM, int_handler); - int ask_permission = 1; int c = {0}; - while ((c = getopt_long(argc, argv, "vd:hnV", longopts, 0)) != -1) { + while ((c = getopt_long(argc, argv, "vd:hV", longopts, 0)) != -1) { switch (c) { case 'v': ++wjob.verify_only; continue; case 'd': wjob.dev_name = optarg; continue; case 'h': break; - case 'n': --ask_permission; continue; case 'V': exit(EXIT_SUCCESS); } - printf("In honor of SwePwnage - the OG disk destroyer\n"); - fprintf(stdout, copyright, CR_YEAR); -#ifdef BLDDATE - printf("Build date: %s\n", BLDDATE); -#endif - printf("\n"); printf("%s\n", help); exit(EXIT_SUCCESS); } @@ -243,7 +223,7 @@ int main(int argc, char *argv[]) { } if (NULL == wjob.dev_name) { - printf("You need to specify a device.\n"); + printf("Device required...\n"); exit(EXIT_FAILURE); } @@ -260,16 +240,12 @@ int main(int argc, char *argv[]) { } close(fd); - if (!wjob.verify_only) - printf("Writing %s to %s\n", wjob.filename, wjob.dev_name); + printf("Writing %s to %s\n", wjob.filename, wjob.dev_name); - if (ask_permission && !wjob.verify_only) { - printf("Is this okay? (y/n): "); - fflush(stdout); - if ('y' != getchar()) { - printf("Aborting...\n"); - exit(EXIT_SUCCESS); - } + printf("Is this okay? (y/n): "); + fflush(stdout); + if ('y' != getchar()) { + exit(0); } wjob.buffer = malloc(BLOCKSIZE); @@ -290,7 +266,7 @@ int main(int argc, char *argv[]) { if (wjob.buffer2) free(wjob.buffer2); - printf("\n%.1f MiB's verified.\nAll good!\n", BYTES_TO_MIB(wjob.total_bytes)); - - exit(EXIT_SUCCESS); + printf("\n%.1f MiB's written and verified.\nAll good!\n", BYTES_TO_MIB(wjob.total_bytes)); + exit(0); + return 0; }