4 * Copyright (C) 2012 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
18 #include <sys/types.h>
28 void do_zap_block(int argc, char *argv[])
30 unsigned long pattern = 0;
41 if (check_fs_open(argv[0]))
43 if (check_fs_read_write(argv[0]))
47 while ((c = getopt (argc, argv, "b:f:l:o:p:")) != EOF) {
53 bit = parse_ulong(optarg, argv[0],
57 if (bit >= (int) current_fs->blocksize * 8) {
58 com_err(argv[0], 0, "The bit to flip "
59 "must be within a %d block\n",
60 current_fs->blocksize);
65 pattern = parse_ulong(optarg, argv[0],
70 com_err(argv[0], 0, "The fill pattern must "
71 "be an 8-bit value\n");
76 offset = parse_ulong(optarg, argv[0],
80 if (offset >= (int) current_fs->blocksize) {
81 com_err(argv[0], 0, "The offset must be "
82 "within a %d block\n",
83 current_fs->blocksize);
90 length = parse_ulong(optarg, argv[0],
100 if (bit > 0 && offset > 0) {
101 com_err(argv[0], 0, "The -o and -b options can not be mixed.");
108 length = current_fs->blocksize - offset;
109 if ((offset + length) > (int) current_fs->blocksize) {
110 com_err(argv[0], 0, "The specified length is too bug\n");
114 if (argc != optind+1) {
116 com_err(0, 0, "Usage:\tzap_block [-f file] [-o offset] "
117 "[-l length] [-p pattern] block_num");
118 com_err(0, 0, "\tzap_block [-f file] [-b bit] "
123 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
128 inode = string_to_inode(file);
131 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
134 com_err(argv[0], errcode,
135 "while mapping logical block %llu\n", block);
140 buf = malloc(current_fs->blocksize);
142 com_err(argv[0], 0, "Couldn't allocate block buffer");
146 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
148 com_err(argv[0], errcode,
149 "while reading block %llu\n", block);
154 buf[bit >> 3] ^= 1 << (bit & 7);
156 memset(buf+offset, pattern, length);
158 errcode = io_channel_write_blk64(current_fs->io, block, 1, buf);
160 com_err(argv[0], errcode,
161 "while write block %llu\n", block);
170 void do_block_dump(int argc, char *argv[])
181 if (check_fs_open(argv[0]))
185 while ((c = getopt (argc, argv, "f:")) != EOF) {
196 if (argc != optind + 1) {
198 com_err(0, 0, "Usage: block_dump [-f inode] block_num");
202 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
207 inode = string_to_inode(file);
210 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
213 com_err(argv[0], errcode,
214 "while mapping logical block %llu\n", block);
219 buf = malloc(current_fs->blocksize);
221 com_err(argv[0], 0, "Couldn't allocate block buffer");
225 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
227 com_err(argv[0], errcode,
228 "while reading block %llu\n", block);
232 for (i=0; i < current_fs->blocksize; i += 16) {
234 if (i && memcmp(buf + i, buf + i - 16, 16) == 0) {
240 if (memcmp(buf + i, buf + suppress, 16) == 0)
245 for (j = 0; j < 16; j++) {
246 printf("%02x", buf[i+j]);
251 for (j = 0; j < 16; j++)
252 printf("%c", isprint(buf[i+j]) ? buf[i+j] : '.');