diff --git a/Makefile b/Makefile index 5f38eca..416e4c1 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ GITREV ?= $(shell git describe --dirty --always) BLDDATE ?= $(shell date -I) CR_YEAR ?= $(shell date +%Y) -VERSION ?= "$(shell git describe --tags --always --abbrev=0 2>/dev/null || git rev-parse --short HEAD)" +VERSION ?= "v0.2.1" CFLAGS ?= -Wall -Wextra -Wpedantic -O2 -std=gnu99 CFLAGS += -DGITREV='"$(GITREV)"' @@ -10,6 +10,10 @@ CFLAGS += -DCR_YEAR='"$(CR_YEAR)"' CFLAGS += -DVERSION='$(VERSION)' CFLAGS += $(EXTRA_CFLAGS) +# Soon... +# CFLAGS += $(shell pkg-config --cflags libudev) +# LIBS += $(shell pkg-config --libs libudev) + PREFIX ?= /usr/local DESTDIR ?= diff --git a/test.sh b/test.sh deleted file mode 100644 index 70cfae9..0000000 --- a/test.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/env/bin bash - -set -e -# set -x # For debugging - -# If you ever mess this up: -# $ sudo mknod /dev/loop-control c 10 237 -# $ sudo chmod 600 /dev/loop-control -# $ sudo chown root:root /dev/loop-control -# -# For cleanup: -# $ sudo find /dev -maxdepth 1 -type b -name 'loop[0-9]*' -exec rm -f {} \; - - -DISKFILE="/tmp/disk.img" -BINFILE="/tmp/file.bin" -LOOPNUM=$((RANDOM % 156 + 100)) -LOOPDEV="/dev/loop${LOOPNUM}" - -echo "Using device: ${LOOPDEV}" - -cleanup() { - echo "Cleaning up..." - set +e -x - sudo losetup -d ${LOOPDEV} - sudo rm ${LOOPDEV} - sudo rm ${BINFILE} ${DISKFILE} -} - -trap cleanup EXIT INT TERM - -if losetup ${LOOPDEV} >/dev/null 2>&1; then - echo "${LOOPDEV} already in use" >&2 - cleanup - exit 1 -fi - -if [ ! -f ${DISKFILE} ]; then - dd if=/dev/zero of=${DISKFILE} bs=1M count=256 -fi - -if [ ! -f ${BINFILE} ]; then - dd if=/dev/urandom of=${BINFILE} bs=1M count=64 -fi - -if [ ! -e ${LOOPDEV} ]; then - sudo losetup ${LOOPDEV} ${DISKFILE} -fi - -sudo ./writeimg -nd ${LOOPDEV} ${BINFILE} -sudo ./writeimg -vnd ${LOOPDEV} ${BINFILE} - -sudo ./writeimg -nd ${LOOPDEV} ./writeimg -sudo ./writeimg -vnd ${LOOPDEV} ./writeimg - -sudo ./writeimg -nd ${LOOPDEV} ./LICENSE -sudo ./writeimg -vnd ${LOOPDEV} ./LICENSE - -# Redirect this to avoid confusion -! sudo ./writeimg -vnd ${LOOPDEV} ./crc32.h 2>/dev/null - -GREEN="\e[32m" -RESET="\e[0m" - -echo -e "\n\n${GREEN}Looks good!${RESET}" diff --git a/writeimg.c b/writeimg.c index 38cf3d0..7bcd467 100644 --- a/writeimg.c +++ b/writeimg.c @@ -32,10 +32,6 @@ #define BLOCKSIZE (1024 * 1024) #endif -#define WI_VERIFY (1 << 0) -#define WI_WRITE (1 << 1) -#define WI_ASK (1 << 2) - #define BYTES_TO_MIB(bts) ((double)bts / (1024 * 1024)) #define BAR_WIDTH 50 @@ -73,14 +69,14 @@ const char help[] = const char copyright[] = "Copyright (C) %s Imbus, BSD-2-Clause\n"; struct write_job { - char *iname; - char *oname; + char *filename; + char *dev_name; char *buffer; char *buffer2; /* For memcmp integrity checks */ size_t bufsize; size_t block_size; size_t total_bytes; - char flags; + char verify_only; } wjob = {0}; typedef struct write_job write_job_t; @@ -98,8 +94,8 @@ void int_handler(int signum) { } int perform_write(write_job_t *job) { - int block_fd = open(job->oname, O_RDWR); - int file_fd = open(job->iname, O_RDONLY); + int block_fd = open(job->dev_name, O_RDWR); + int file_fd = open(job->filename, O_RDONLY); assert(block_fd >= 0); assert(file_fd >= 0); @@ -118,7 +114,7 @@ int perform_write(write_job_t *job) { if (read_bytes == 0) { crc = crc32_finalize(crc); - if (job->flags & WI_WRITE) { + if (!job->verify_only) { printf("\nWriting done...\n"); assert(job->total_bytes == b_written); } else @@ -128,17 +124,17 @@ int perform_write(write_job_t *job) { } if (read_bytes < 0) { - fprintf(stderr, "%s: Read error\n", job->iname); + fprintf(stderr, "%s: Read error\n", job->filename); perror("Read"); exit(EXIT_FAILURE); } crc = crc32_update(crc, job->buffer, read_bytes); - if (job->flags & WI_WRITE) { + 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->oname); + fprintf(stderr, "%s: Write error\n", job->dev_name); perror("Write"); exit(EXIT_FAILURE); } @@ -199,7 +195,7 @@ static const struct option longopts[] = { }; int main(int argc, char *argv[]) { - printf("%s %s, Rev. %s\n", "WriteIMG", VERSION, GITREV); + printf("%s %s, Rev. %s\n", basename(argv[0]), VERSION, GITREV); /* Line buffering, system allocated */ setvbuf(stdout, NULL, _IOLBF, 0); @@ -210,18 +206,14 @@ int main(int argc, char *argv[]) { signal(SIGHUP, int_handler); signal(SIGTERM, int_handler); - wjob.flags = WI_VERIFY | WI_WRITE | WI_ASK; - + int ask_permission = 1; int c = {0}; while ((c = getopt_long(argc, argv, "vd:hnV", longopts, 0)) != -1) { switch (c) { - case 'v': - wjob.flags |= WI_VERIFY; - wjob.flags &= ~WI_WRITE; - continue; - case 'd': wjob.oname = optarg; continue; + case 'v': ++wjob.verify_only; continue; + case 'd': wjob.dev_name = optarg; continue; case 'h': break; - case 'n': wjob.flags &= ~WI_ASK; continue; + case 'n': --ask_permission; continue; case 'V': exit(EXIT_SUCCESS); } printf("In honor of SwePwnage - the OG disk destroyer\n"); @@ -244,27 +236,27 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - wjob.iname = argv[0]; + wjob.filename = argv[0]; struct stat file_stat = {0}; - if (0 != stat(wjob.iname, &file_stat)) { + if (0 != stat(wjob.filename, &file_stat)) { printf("File does not exist...\n"); exit(EXIT_FAILURE); } - if (NULL == wjob.oname) { + if (NULL == wjob.dev_name) { printf("You need to specify a device.\n"); exit(EXIT_FAILURE); } - if (0 != strncmp(wjob.oname, "/dev/", 4)) { - printf("\"%s\" does not appear to be a block device...\n", wjob.oname); + if (0 != strncmp(wjob.dev_name, "/dev/", 4)) { + printf("\"%s\" does not appear to be a block device...\n", wjob.dev_name); exit(EXIT_FAILURE); } /* Seems to be the cleanest way to check for write perm on a blockdev */ - int fd = open(wjob.oname, O_WRONLY); + int fd = open(wjob.dev_name, O_WRONLY); if (fd < 0) { - printf("Cannot write to \"%s\", do you have write permissions?\n", wjob.oname); + printf("Cannot write to \"%s\", do you have write permissions?\n", wjob.dev_name); exit(1); } close(fd); @@ -272,11 +264,13 @@ int main(int argc, char *argv[]) { wjob.total_bytes = file_stat.st_size; assert(file_stat.st_size >= 0); - if (wjob.flags & WI_WRITE) - printf( - "Writing \"%s\" (%.1f MiB) to \"%s\"\n", basename(wjob.iname), BYTES_TO_MIB(wjob.total_bytes), wjob.oname); + if (!wjob.verify_only) + printf("Writing \"%s\" (%.1f MiB) to \"%s\"\n", + basename(wjob.filename), + BYTES_TO_MIB(wjob.total_bytes), + wjob.dev_name); - if ((wjob.flags & WI_ASK) && (wjob.flags & WI_WRITE)) { + if (ask_permission && !wjob.verify_only) { printf("Is this okay? (y/N): "); fflush(stdout); if ('y' != getchar()) {