I want to read a block device file, block by block until last byte even if the block is full of zeroes.
My code is this. size
is the no. of bytes I want it to read in one go - could be anything 4K, 8K etc.
for (int i = 1; i <= num; i++){
read_bytes = read(fd, buf, size);
if (read_bytes < 0) {
perror("read failed");
return 1;
}
lseek_offset = lseek(fd, size, SEEK_CUR);
if (lseek_offset < 0){
perror("lseek failed.\n");
return 1;
}
}
When a block is filled with zero bytes (not the length of block but the data in block), lseek fails with EINV.
And I can see from df -h
that that disk is half full and rest is zero bytes since it was formatted with ext4 before using it.