Whamcloud - gitweb
debugfs: fix printing of xattrs with ea_in_inode values
[tools/e2fsprogs.git] / debugfs / zap.c
index 8109209..c7996b2 100644 (file)
@@ -25,7 +25,8 @@ extern char *optarg;
 
 #include "debugfs.h"
 
-void do_zap_block(int argc, char *argv[])
+void do_zap_block(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
+                   void *infop EXT2FS_ATTR((unused)))
 {
        unsigned long   pattern = 0;
        unsigned char   *buf;
@@ -167,27 +168,29 @@ errout:
        return;
 }
 
-void do_block_dump(int argc, char *argv[])
+void do_block_dump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
+                   void *infop EXT2FS_ATTR((unused)))
 {
        unsigned char   *buf;
        ext2_ino_t      inode;
        errcode_t       errcode;
        blk64_t         block;
        char            *file = NULL;
-       unsigned int    i, j;
+       int             xattr_dump = 0;
        int             c, err;
-       int             suppress = -1;
 
        if (check_fs_open(argv[0]))
                return;
 
        reset_getopt();
-       while ((c = getopt (argc, argv, "f:")) != EOF) {
+       while ((c = getopt (argc, argv, "f:x")) != EOF) {
                switch (c) {
                case 'f':
                        file = optarg;
                        break;
-
+               case 'x':
+                       xattr_dump = 1;
+                       break;
                default:
                        goto print_usage;
                }
@@ -195,7 +198,7 @@ void do_block_dump(int argc, char *argv[])
 
        if (argc != optind + 1) {
        print_usage:
-               com_err(0, 0, "Usage: block_dump [-f inode] block_num");
+               com_err(0, 0, "Usage: block_dump [-x] [-f inode] block_num");
                return;
        }
 
@@ -229,32 +232,45 @@ void do_block_dump(int argc, char *argv[])
                goto errout;
        }
 
-       for (i=0; i < current_fs->blocksize; i += 16) {
+       if (xattr_dump)
+               block_xattr_dump(stdout, buf, current_fs->blocksize);
+       else
+               do_byte_hexdump(stdout, buf, current_fs->blocksize);
+errout:
+       free(buf);
+}
+
+void do_byte_hexdump(FILE *fp, unsigned char *buf, size_t bufsize)
+{
+       size_t          i, j, max;
+       int             suppress = -1;
+
+       for (i = 0; i < bufsize; i += 16) {
+               max = (bufsize - i > 16) ? 16 : bufsize - i;
                if (suppress < 0) {
-                       if (i && memcmp(buf + i, buf + i - 16, 16) == 0) {
+                       if (i && memcmp(buf + i, buf + i - max, max) == 0) {
                                suppress = i;
-                               printf("*\n");
+                               fprintf(fp, "*\n");
                                continue;
                        }
                } else {
-                       if (memcmp(buf + i, buf + suppress, 16) == 0)
+                       if (memcmp(buf + i, buf + suppress, max) == 0)
                                continue;
                        suppress = -1;
                }
-               printf("%04o  ", i);
+               fprintf(fp, "%04o  ", (unsigned int)i);
                for (j = 0; j < 16; j++) {
-                       printf("%02x", buf[i+j]);
+                       if (j < max)
+                               fprintf(fp, "%02x", buf[i+j]);
+                       else
+                               fprintf(fp, "  ");
                        if ((j % 2) == 1)
-                               putchar(' ');
+                               fprintf(fp, " ");
                }
-               putchar(' ');
-               for (j = 0; j < 16; j++)
-                       printf("%c", isprint(buf[i+j]) ? buf[i+j] : '.');
-               putchar('\n');
+               fprintf(fp, " ");
+               for (j = 0; j < max; j++)
+                       fprintf(fp, "%c", isprint(buf[i+j]) ? buf[i+j] : '.');
+               fprintf(fp, "\n");
        }
-       putchar('\n');
-
-errout:
-       free(buf);
-       return;
+       fprintf(fp, "\n");
 }