1/*
2 * unused.c --- quick and dirty unused space dumper
3 *
4 * Copyright (C) 1997 Theodore Ts'o.  This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8#include "config.h"
9#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <ctype.h>
13#include <string.h>
14#include <time.h>
15#ifdef HAVE_ERRNO_H
16#include <errno.h>
17#endif
18#include <sys/types.h>
19#ifdef HAVE_GETOPT_H
20#include <getopt.h>
21#else
22extern int optind;
23extern char *optarg;
24#endif
25
26#include "debugfs.h"
27
28void do_dump_unused(int argc EXT2FS_ATTR((unused)), char **argv)
29{
30	blk64_t		blk;
31	unsigned char	buf[EXT2_MAX_BLOCK_SIZE];
32	unsigned int	i;
33	errcode_t	retval;
34
35	if (common_args_process(argc, argv, 1, 1,
36				"dump_unused", "", 0))
37		return;
38
39	for (blk=current_fs->super->s_first_data_block;
40	     blk < ext2fs_blocks_count(current_fs->super); blk++) {
41		if (ext2fs_test_block_bitmap2(current_fs->block_map,blk))
42			continue;
43		retval = io_channel_read_blk64(current_fs->io, blk, 1, buf);
44		if (retval) {
45			com_err(argv[0], retval, "While reading block\n");
46			return;
47		}
48		for (i=0; i < current_fs->blocksize; i++)
49			if (buf[i])
50				break;
51		if (i >= current_fs->blocksize)
52			continue;
53		printf("\nUnused block %llu contains non-zero data:\n\n",
54		       blk);
55		for (i=0; i < current_fs->blocksize; i++)
56			fputc(buf[i], stdout);
57	}
58}
59