Compare commits

..

No commits in common. "ab43512e425ba3789253c2b7d077ba7c2ac5b178" and "5277b78fd31d1e89c4143624551e48dba1fdfc15" have entirely different histories.

3 changed files with 33 additions and 78 deletions

View file

@ -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

View file

@ -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)

View file

@ -11,11 +11,6 @@
#include <sys/stat.h>
#include <unistd.h>
#if __linux__
#include <linux/fs.h> /* IOTCL flush number */
#include <sys/ioctl.h> /* 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))
@ -61,7 +56,6 @@ const char help[] =
" -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"
"\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);
break; /* Finished */
}
@ -131,7 +120,6 @@ 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);
@ -139,7 +127,6 @@ int perform_write(write_job_t *job) {
exit(EXIT_FAILURE);
}
print_progress(b_written += written_bytes, job->total_bytes);
} /* Else maybe give some helpful insights? */
}
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);
if (ask_permission && !wjob.verify_only) {
printf("Is this okay? (y/n): ");
fflush(stdout);
if ('y' != getchar()) {
printf("Aborting...\n");
exit(EXIT_SUCCESS);
}
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;
}