Whamcloud - gitweb
debugfs: teach logdump the -n <num_trans> option
[tools/e2fsprogs.git] / debugfs / util.c
index bd5de79..9e88054 100644 (file)
@@ -119,7 +119,7 @@ ext2_ino_t string_to_inode(char *str)
         */
        if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
                ino = strtoul(str+1, &end, 0);
-               if (*end=='>')
+               if (*end=='>' && (ino <= current_fs->super->s_inodes_count))
                        return ino;
        }
 
@@ -128,6 +128,11 @@ ext2_ino_t string_to_inode(char *str)
                com_err(str, retval, 0);
                return 0;
        }
+       if (ino > current_fs->super->s_inodes_count) {
+               com_err(str, 0, "resolves to an illegal inode number: %u\n",
+                       ino);
+               return 0;
+       }
        return ino;
 }
 
@@ -205,7 +210,7 @@ char *time_to_string(__s64 cl)
        const char      *tz;
 
        if (do_gmt == -1) {
-               /* The diet libc doesn't respect the TZ environemnt variable */
+               /* The diet libc doesn't respect the TZ environment variable */
                tz = ss_safe_getenv("TZ");
                if (!tz)
                        tz = "";
@@ -415,12 +420,12 @@ int common_block_args_process(int argc, char *argv[],
        return 0;
 }
 
-int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
-                       const char *cmd, int bufsize)
+int debugfs_read_inode2(ext2_ino_t ino, struct ext2_inode * inode,
+                       const char *cmd, int bufsize, int flags)
 {
        int retval;
 
-       retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
+       retval = ext2fs_read_inode2(current_fs, ino, inode, bufsize, flags);
        if (retval) {
                com_err(cmd, retval, "while reading inode %u", ino);
                return 1;
@@ -441,15 +446,14 @@ int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
        return 0;
 }
 
-int debugfs_write_inode_full(ext2_ino_t ino,
-                            struct ext2_inode *inode,
-                            const char *cmd,
-                            int bufsize)
+int debugfs_write_inode2(ext2_ino_t ino,
+                        struct ext2_inode *inode,
+                        const char *cmd,
+                        int bufsize, int flags)
 {
        int retval;
 
-       retval = ext2fs_write_inode_full(current_fs, ino,
-                                        inode, bufsize);
+       retval = ext2fs_write_inode2(current_fs, ino, inode, bufsize, flags);
        if (retval) {
                com_err(cmd, retval, "while writing inode %u", ino);
                return 1;
@@ -517,7 +521,7 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
        blk64_t *lst = *list;
        size_t ln = *len;
        char *tok, *p = str;
-       errcode_t retval;
+       errcode_t retval = 0;
 
        while ((tok = strtok(p, ","))) {
                blk64_t *l;
@@ -526,24 +530,28 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
                errno = 0;
                y = x = strtoull(tok, &e, 0);
-               if (errno)
-                       return errno;
+               if (errno) {
+                       retval = errno;
+                       break;
+               }
                if (*e == '-') {
                        y = strtoull(e + 1, NULL, 0);
-                       if (errno)
-                               return errno;
+                       if (errno) {
+                               retval = errno;
+                               break;
+                       }
                } else if (*e != 0) {
                        retval = EINVAL;
-                       goto err;
+                       break;
                }
                if (y < x) {
                        retval = EINVAL;
-                       goto err;
+                       break;
                }
                l = realloc(lst, sizeof(blk64_t) * (ln + y - x + 1));
                if (l == NULL) {
                        retval = ENOMEM;
-                       goto err;
+                       break;
                }
                lst = l;
                for (; x <= y; x++)
@@ -553,8 +561,40 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
        *list = lst;
        *len = ln;
-       return 0;
-err:
-       free(lst);
        return retval;
 }
+
+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 - max, max) == 0) {
+                               suppress = i;
+                               fprintf(fp, "*\n");
+                               continue;
+                       }
+               } else {
+                       if (memcmp(buf + i, buf + suppress, max) == 0)
+                               continue;
+                       suppress = -1;
+               }
+               fprintf(fp, "%04o  ", (unsigned int)i);
+               for (j = 0; j < 16; j++) {
+                       if (j < max)
+                               fprintf(fp, "%02x", buf[i+j]);
+                       else
+                               fprintf(fp, "  ");
+                       if ((j % 2) == 1)
+                               fprintf(fp, " ");
+               }
+               fprintf(fp, " ");
+               for (j = 0; j < max; j++)
+                       fprintf(fp, "%c", isprint(buf[i+j]) ? buf[i+j] : '.');
+               fprintf(fp, "\n");
+       }
+       fprintf(fp, "\n");
+}