Skip to content

Instantly share code, notes, and snippets.

@mshroyer
Created March 19, 2022 00:49
Show Gist options
  • Save mshroyer/a20d4022430c19cffbbe9a71a473fb30 to your computer and use it in GitHub Desktop.
Save mshroyer/a20d4022430c19cffbbe9a71a473fb30 to your computer and use it in GitHub Desktop.
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define REPORT_BLOCKS 1024
#define BLOCK_SIZE 65536
#define SUB_BLOCK_SIZE 8192
char ba[BLOCK_SIZE];
char bb[BLOCK_SIZE];
int
open_block_device(const char* path)
{
int fd;
fd = open(path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Unable to open block device: %s\n", path);
exit(1);
}
return fd;
}
int
main(int argc, char* argv[])
{
size_t blocknum = 0;
size_t diff_blocks = 0;
size_t diff_sub_blocks = 0;
int fa, fb;
fa = open_block_device(argv[1]);
fb = open_block_device(argv[2]);
while (true) {
if (blocknum % REPORT_BLOCKS == 0) {
printf("\rblock number: %zu", blocknum);
}
if (read(fa, ba, BLOCK_SIZE) != BLOCK_SIZE) {
break;
}
if (read(fb, bb, BLOCK_SIZE) != BLOCK_SIZE) {
break;
}
bool block_differs = false;
bool sub_block_differs = false;
for (size_t i = 0; i < BLOCK_SIZE; ++i) {
if (i % SUB_BLOCK_SIZE == 0) {
sub_block_differs = false;
}
if (ba[i] != bb[i]) {
if (!block_differs) {
block_differs = true;
++diff_blocks;
}
if (!sub_block_differs) {
sub_block_differs = true;
++diff_sub_blocks;
}
}
}
++blocknum;
}
printf("\nTotal blocks counted: %zu\n", blocknum);
printf("Differing blocks: %zu\n", diff_blocks);
printf("Differing sub-blocks: %zu\n", diff_sub_blocks);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment