summaryrefslogtreecommitdiff
path: root/debugfs/unused.c
blob: 818fef13473fa420965403a07cdf9bdf5416a091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
 * unused.c --- quick and dirty unused space dumper
 *
 * Copyright (C) 1997 Theodore Ts'o.  This file may be redistributed
 * under the terms of the GNU Public License.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include <sys/types.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
extern int optind;
extern char *optarg;
#endif

#include "debugfs.h"

void do_dump_unused(int argc EXT2FS_ATTR((unused)), char **argv)
{
	unsigned long	blk;
	unsigned char buf[EXT2_MAX_BLOCK_SIZE];
	unsigned int	i;
	errcode_t	retval;

	if (common_args_process(argc, argv, 1, 1,
				"dump_unused", "", 0))
		return;

	for (blk=current_fs->super->s_first_data_block;
	     blk < current_fs->super->s_blocks_count; blk++) {
		if (ext2fs_test_block_bitmap(current_fs->block_map,blk))
			continue;
		retval = io_channel_read_blk(current_fs->io, blk, 1, buf);
		if (retval) {
			com_err(argv[0], retval, "While reading block\n");
			return;
		}
		for (i=0; i < current_fs->blocksize; i++)
			if (buf[i])
				break;
		if (i >= current_fs->blocksize)
			continue;
		printf("\nUnused block %lu contains non-zero data:\n\n",
		       blk);
		for (i=0; i < current_fs->blocksize; i++)
			fputc(buf[i], stdout);
	}
}