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