Skip to content

Commit

Permalink
utils: add vbt tool to dump VBT information
Browse files Browse the repository at this point in the history
  • Loading branch information
no92 committed Jun 17, 2023
1 parent 4818a35 commit b18f472
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ sources = files(
'src/vbt/vbt.c',
)

util_vbt_sources = files(
'utils/vbt/vbt.c',
)

include_directories = include_directories('include')
library = static_library('lil', sources, include_directories: include_directories, pic: false)
dependency = declare_dependency(link_with: library, include_directories: include_directories)

if get_option('build_utils')
util_vbt = executable('vbt', util_vbt_sources, dependencies: dependency, native: true)
endif
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option('build_utils', type : 'boolean', value : false)
83 changes: 83 additions & 0 deletions utils/vbt/vbt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <lil/vbt.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

static struct option longopts[] = {
{"blocks", no_argument, 0, 'b'},
{0, 0, 0, 0},
};

int main(int argc, char *argv[]) {
bool print_blocks = false;

while(1) {
int option_index = 0;
int c = getopt_long(argc, argv, "b", longopts, &option_index);

if(c == -1) {
break;
}

switch(c) {
case 'b': {
print_blocks = true;
break;
}
default: {
fprintf(stderr, "getopt_long returned %c\n", c);
break;
}
}
}

if((optind + 1) != argc) {
fprintf(stderr, "no file given\n");
exit(1);
}

const char *filepath = argv[optind];
int fd = open(filepath, O_RDONLY);
if(fd < 0) {
fprintf(stderr, "could not open file '%s': %s\n", argv[1], strerror(errno));
exit(1);
}

struct stat statbuf;
int err = fstat(fd, &statbuf);
assert(err >= 0);

void *vbt = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
assert(vbt != MAP_FAILED);
close(fd);

const struct vbt_header *hdr = vbt_get_header(vbt, statbuf.st_size);
assert(hdr);

printf("VBT header version %u.%u\n", hdr->version / 100, hdr->version % 100);

const struct bdb_header *bdb_hdr = vbt_get_bdb_header(hdr);
assert(bdb_hdr);

if(print_blocks) {
printf("Blocks:");
for(size_t i = 0; i < 256; i++) {
if(vbt_get_bdb_block(hdr, i)) {
printf(" %zu", i);
}
}
printf("\n");
}

munmap(vbt, statbuf.st_size);

return 0;
}

0 comments on commit b18f472

Please sign in to comment.