4 * Copyright (C) 2012 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
17 #include <sys/types.h>
27 void do_zap_block(int argc, char *argv[])
29 unsigned long pattern = 0;
40 if (check_fs_open(argv[0]))
42 if (check_fs_read_write(argv[0]))
46 while ((c = getopt (argc, argv, "b:f:l:o:p:")) != EOF) {
52 bit = parse_ulong(optarg, argv[0],
56 if (bit >= (int) current_fs->blocksize * 8) {
57 com_err(argv[0], 0, "The bit to flip "
58 "must be within a %d block\n",
59 current_fs->blocksize);
64 pattern = parse_ulong(optarg, argv[0],
69 com_err(argv[0], 0, "The fill pattern must "
70 "be an 8-bit value\n");
75 offset = parse_ulong(optarg, argv[0],
79 if (offset >= (int) current_fs->blocksize) {
80 com_err(argv[0], 0, "The offset must be "
81 "within a %d block\n",
82 current_fs->blocksize);
89 length = parse_ulong(optarg, argv[0],
99 if (bit > 0 && offset > 0) {
100 com_err(argv[0], 0, "The -o and -b options can not be mixed.");
107 length = current_fs->blocksize - offset;
108 if ((offset + length) > (int) current_fs->blocksize) {
109 com_err(argv[0], 0, "The specified length is too bug\n");
113 if (argc != optind+1) {
115 com_err(0, 0, "Usage:\tzap_block [-f file] [-o offset] "
116 "[-l length] [-p pattern] block_num");
117 com_err(0, 0, "\tzap_block [-f file] [-b bit] "
122 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
127 inode = string_to_inode(file);
130 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
133 com_err(argv[0], errcode,
134 "while mapping logical block %llu\n", block);
139 buf = malloc(current_fs->blocksize);
141 com_err(argv[0], 0, "Couldn't allocate block buffer");
145 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
147 com_err(argv[0], errcode,
148 "while reading block %llu\n", block);
153 buf[bit >> 3] ^= 1 << (bit & 7);
155 memset(buf+offset, pattern, length);
157 errcode = io_channel_write_blk64(current_fs->io, block, 1, buf);
159 com_err(argv[0], errcode,
160 "while write block %llu\n", block);
169 void do_block_dump(int argc, char *argv[])
180 if (check_fs_open(argv[0]))
184 while ((c = getopt (argc, argv, "f:")) != EOF) {
195 if (argc != optind + 1) {
197 com_err(0, 0, "Usage: block_dump [-f inode] block_num");
201 block = parse_ulonglong(argv[optind], argv[0], "block", &err);
206 inode = string_to_inode(file);
209 errcode = ext2fs_bmap2(current_fs, inode, 0, 0, 0,
212 com_err(argv[0], errcode,
213 "while mapping logical block %llu\n", block);
218 buf = malloc(current_fs->blocksize);
220 com_err(argv[0], 0, "Couldn't allocate block buffer");
224 errcode = io_channel_read_blk64(current_fs->io, block, 1, buf);
226 com_err(argv[0], errcode,
227 "while reading block %llu\n", block);
231 for (i=0; i < current_fs->blocksize; i += 16) {
233 if (i && memcmp(buf + i, buf + i - 16, 16) == 0) {
239 if (memcmp(buf + i, buf + suppress, 16) == 0)
244 for (j = 0; j < 16; j++) {
245 printf("%02x", buf[i+j]);
250 for (j = 0; j < 16; j++)
251 printf("%c", isprint(buf[i+j]) ? buf[i+j] : '.');