sparse-tools/src/fillblocks.c

60 lines
1.1 KiB
C

//
// Created by haraldwolff on 21.08.22.
//
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <getopt.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
int main(int argc, char *argv[]){
int ch;
char filename[1024] = "";
while ((ch = getopt(argc, argv, "f:"))!=-1){
switch (ch){
case 'f':
strncpy(filename, optarg, 1024);
break;
}
}
if (!filename[0]){
printf("need filename: blockfill -f <filename>\n");
exit(EXIT_FAILURE);
}
int f = open(filename, O_RDWR);
if (f < 0)
{
printf("could not open %s (%s)\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
long size = lseek(f, 0, SEEK_END);
void *mm = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
if (mm == MAP_FAILED){
printf("could not mmap file.");
} else {
long offset = 0;
u_int64_t *t = (u_int64_t *)mm;
while (offset < size) {
for (int n = 0; n < 512; n++) {
t[ (offset >> 3) + n ] = offset;
}
offset += 4096;
printf("\roffset: 0x%llx %d%%", offset, (100 * offset / size));
}
printf("\n");
munmap(mm, size);
}
close(f);
}