Whamcloud - gitweb
libext2fs: Add stricter/earlier tests for blocksize in ext2fs_open()
[tools/e2fsprogs.git] / debugfs / htree.c
index 6dbe426..a46bae7 100644 (file)
@@ -35,10 +35,11 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
        struct ext2_dir_entry *dirent;
        int             thislen, col = 0;
        unsigned int    offset = 0;
-       char            name[EXT2_NAME_LEN];
+       char            name[EXT2_NAME_LEN + 1];
        char            tmp[EXT2_NAME_LEN + 16];
        blk_t           pblk;
        ext2_dirhash_t  hash;
+       int             hash_alg;
        
        errcode = ext2fs_bmap(fs, ino, inode, buf, 0, blk, &pblk);
        if (errcode) {
@@ -53,6 +54,10 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
                        "while  reading block %u\n", blk);
                return;
        }
+       hash_alg = rootnode->hash_version;
+       if ((hash_alg <= EXT2_HASH_TEA) &&
+           (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
+               hash_alg += 3;
 
        while (offset < fs->blocksize) {
                dirent = (struct ext2_dir_entry *) (buf + offset);
@@ -67,7 +72,7 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
                        (dirent->name_len & 0xFF) : EXT2_NAME_LEN;
                strncpy(name, dirent->name, thislen);
                name[thislen] = '\0';
-               errcode = ext2fs_dirhash(rootnode->hash_version, name,
+               errcode = ext2fs_dirhash(hash_alg, name,
                                         thislen, fs->super->s_hash_seed,
                                         &hash, 0);
                if (errcode)
@@ -114,7 +119,7 @@ static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
 
        for (i=0; i < limit.count; i++) {
                hash = i ? ext2fs_le32_to_cpu(ent[i].hash) : 0;
-               fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %d\n", i,
+               fprintf(pager, "Entry #%d: Hash 0x%08x%s, block %u\n", i,
                        hash, (hash & 1) ? " (**)" : "",
                        ext2fs_le32_to_cpu(ent[i].block));
                }
@@ -156,19 +161,20 @@ static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
        if (errcode) {
                com_err("htree_dump_int_block", errcode,
                        "while mapping logical block %u\n", blk);
-               return;
+               goto errout;
        }
 
        errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
        if (errcode) {
                com_err("htree_dump_int_block", errcode,
                        "while  reading block %u\n", blk);
-               return;
+               goto errout;
        }
 
        htree_dump_int_node(fs, ino, inode, rootnode,
                            (struct ext2_dx_entry *) (buf+8),
                            cbuf, level);
+errout:
        free(cbuf);
 }
 
@@ -179,7 +185,7 @@ void do_htree_dump(int argc, char *argv[])
        ext2_ino_t      ino;
        struct ext2_inode inode;
        int             c;
-       int             long_opt;
+       int             long_opt = 0;
        char            *buf = NULL;
        struct          ext2_dx_root_info  *rootnode;
        struct          ext2_dx_entry *ent;
@@ -197,10 +203,13 @@ void do_htree_dump(int argc, char *argv[])
                case 'l':
                        long_opt++;
                        break;
+               default:
+                       goto print_usage;
                }
        }
 
        if (argc > optind+1) {
+       print_usage:
                com_err(0, 0, "Usage: htree_dump [-l] file");
                goto errout;
        }
@@ -279,9 +288,12 @@ void do_dx_hash(int argc, char *argv[])
                case 'h':
                        hash_version = atoi(optarg);
                        break;
+               default:
+                       goto print_usage;
                }
        }
        if (optind != argc-1) {
+       print_usage:
                com_err(argv[0], 0, "usage: dx_hash filename");
                return;
        }
@@ -335,7 +347,8 @@ void do_dirsearch(int argc, char *argv[])
        pb.search_name = argv[2];
        pb.len = strlen(pb.search_name);
        
-       ext2fs_block_iterate2(current_fs, inode, 0, 0, search_dir_block, &pb);
+       ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, 0,
+                             search_dir_block, &pb);
 
        free(pb.buf);
 }
@@ -372,7 +385,7 @@ static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
                    strncmp(p->search_name, dirent->name,
                            p->len) == 0) {
                        printf("Entry found at logical block %lld, "
-                              "phys %u, offset %u\n", blockcnt,
+                              "phys %u, offset %u\n", (long long)blockcnt,
                               *blocknr, offset);
                        printf("offset %u\n", offset);
                        return BLOCK_ABORT;