[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: attention: file corruption, diagnose program



On Thu, Jan 06, 2000 at 04:40:56AM +0100, Marcus Brinkmann wrote:
> The attached program holefinder.c detects such holes as well as holes at the
> end of a file which might be shorter than 4096 bytes.

Aiieeee!

Here it is.

Marcus

-- 
`Rhubarb is no Egyptian god.' Debian http://www.debian.org Check Key server 
Marcus Brinkmann              GNU    http://www.gnu.org    for public PGP Key 
Marcus.Brinkmann@ruhr-uni-bochum.de,     marcus@gnu.org    PGP Key ID 36E7CD09
http://homepage.ruhr-uni-bochum.de/Marcus.Brinkmann/       brinkmd@debian.org
/* holefinder.c -- find whole zero blocks */
/* Written by Marcus Brinkmann. This file is hereby placed
  in the public domain. */

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#define BLOCKSIZE 4096

int main (int argc, char *argv[])
{
int fd; /* Filedescriptor */
int byte_count;
int found_non_zero_byte;
int bytes_read;
int block_count = 0;
int first_zero_block = 1;
char buffer[BLOCKSIZE];

	if (argc != 2) {
		printf("Usage: %s FILE\n", argv[0]);
		return 0;
	}

	fd = open(argv[1], O_RDONLY);
	if (!fd) {
		printf ("could not open %s: %s\n", argv[1], strerror(errno));
		return 1;
	}

	while ((bytes_read = read(fd, buffer, BLOCKSIZE)) > 0) {
		found_non_zero_byte = 0;
		byte_count = bytes_read-1;
		while (byte_count >= 0 && !found_non_zero_byte)
			if (buffer[byte_count--] != 0)  
				found_non_zero_byte = 1;
		if (!found_non_zero_byte) {
			if (first_zero_block) {
				printf("%s: ", argv[1]);
				first_zero_block = 0;
			} else
				printf(", ");
			if (bytes_read == BLOCKSIZE)
				printf("%i", block_count);
			else
				printf("%i (%i)", block_count, bytes_read);
		}
		block_count++;
	}
	if (!first_zero_block) {
		if (bytes_read == -1)
		printf(" %s\n", strerror(errno));
	else
		printf("\n");
	}
	close(fd);
}

Reply to: