blob: b78d70b8095d2357c2b2c318e7ab89c3c98cfedd [file] [log] [blame]
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -04001/*
2 * unused.c --- quick and dirty unused space dumper
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -04004 * Copyright (C) 1997 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8#include <stdio.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <ctype.h>
12#include <string.h>
13#include <time.h>
14#ifdef HAVE_ERRNO_H
15#include <errno.h>
16#endif
17#include <sys/types.h>
18#ifdef HAVE_GETOPT_H
19#include <getopt.h>
Theodore Ts'oefc6f622008-08-27 23:07:54 -040020#else
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -040021extern int optind;
22extern char *optarg;
23#endif
24
25#include "debugfs.h"
26
Theodore Ts'o54434922003-12-07 01:28:50 -050027void do_dump_unused(int argc EXT2FS_ATTR((unused)), char **argv)
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -040028{
JP Abgralle0ed7402014-03-19 19:08:39 -070029 blk64_t blk;
30 unsigned char buf[EXT2_MAX_BLOCK_SIZE];
Theodore Ts'o54434922003-12-07 01:28:50 -050031 unsigned int i;
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -040032 errcode_t retval;
33
Matthias Koenig97fa31b2007-01-28 12:40:28 -050034 if (common_args_process(argc, argv, 1, 1,
35 "dump_unused", "", 0))
36 return;
37
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -040038 for (blk=current_fs->super->s_first_data_block;
JP Abgralle0ed7402014-03-19 19:08:39 -070039 blk < ext2fs_blocks_count(current_fs->super); blk++) {
40 if (ext2fs_test_block_bitmap2(current_fs->block_map,blk))
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -040041 continue;
JP Abgralle0ed7402014-03-19 19:08:39 -070042 retval = io_channel_read_blk64(current_fs->io, blk, 1, buf);
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -040043 if (retval) {
44 com_err(argv[0], retval, "While reading block\n");
45 return;
46 }
47 for (i=0; i < current_fs->blocksize; i++)
48 if (buf[i])
49 break;
50 if (i >= current_fs->blocksize)
51 continue;
JP Abgralle0ed7402014-03-19 19:08:39 -070052 printf("\nUnused block %llu contains non-zero data:\n\n",
Theodore Ts'oa5fdcd52003-05-21 17:58:36 -040053 blk);
54 for (i=0; i < current_fs->blocksize; i++)
55 fputc(buf[i], stdout);
56 }
57}