#include #include #include "block.h" enum { BlockSize = 4096, }; int fd; static void blockinit(void) { long i; uchar buf[BlockSize]; for(i = 0; i < 255; i++){ memset(buf, i, sizeof buf); if(pwrite(fd, buf, sizeof buf, i*BlockSize) != BlockSize) sysfatal("pwrite: %r"); } } static void blockcheck(void) { long i; uchar buf[BlockSize*4]; uchar cmp[BlockSize]; if(blockread(fd, buf, BlockSize, 4, 0) != 4) sysfatal("blockread: %r"); for(i = 0; i < 4; i++){ memset(cmp, i, sizeof cmp); if(memcmp(buf+(i*BlockSize), cmp, BlockSize) != 0) sysfatal("block %ld mismatch", i); } } void main(void) { char *tmp; tmp = mktemp("/tmp/libblock-img.XXXX"); fd = create(tmp, ORDWR|ORCLOSE, 0600); if(fd < 0) sysfatal("create %s: %r", tmp); blockinit(); blockcheck(); close(fd); exits(nil); }