Whamcloud - gitweb
Fix gcc -Wall nits.
[tools/e2fsprogs.git] / debugfs / htree.c
index a6d4eef..7af3d1b 100644 (file)
@@ -21,9 +21,6 @@
 extern int optind;
 extern char *optarg;
 #endif
-#ifdef HAVE_OPTRESET
-extern int optreset;           /* defined by BSD, but not others */
-#endif
 
 #include "debugfs.h"
 
@@ -31,11 +28,13 @@ static FILE *pager;
 
 static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
                                 struct ext2_inode *inode,
+                                struct ext2_dx_root_info * rootnode,
                                 blk_t blk, char *buf)
 {
        errcode_t       errcode;
        struct ext2_dir_entry *dirent;
-       int             thislen, col = 0, offset = 0;
+       int             thislen, col = 0;
+       unsigned int    offset = 0;
        char            name[EXT2_NAME_LEN];
        char            tmp[EXT2_NAME_LEN + 16];
        blk_t           pblk;
@@ -48,7 +47,7 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
                return;
        }
 
-       errcode = io_channel_read_blk(current_fs->io, pblk, 1, buf);
+       errcode = ext2fs_read_dir_block2(current_fs, pblk, buf, 0);
        if (errcode) {
                com_err("htree_dump_leaf_node", errcode,
                        "while  reading block %d\n", blk);
@@ -68,7 +67,9 @@ 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(0, name, thislen, &hash);
+               errcode = ext2fs_dirhash(rootnode->hash_version, name,
+                                        thislen, fs->super->s_hash_seed,
+                                        &hash, 0);
                if (errcode)
                        com_err("htree_dump_leaf_node", errcode,
                                "while calculating hash");
@@ -89,37 +90,48 @@ static void htree_dump_leaf_node(ext2_filsys fs, ext2_ino_t ino,
 
 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
                                 struct ext2_inode *inode,
+                                struct ext2_dx_root_info * rootnode,
                                 blk_t blk, char *buf, int level);
 
 
 static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
                                struct ext2_inode *inode,
+                               struct ext2_dx_root_info * rootnode,
                                struct ext2_dx_entry *ent, 
                                char *buf, int level)
 {
-       struct          ext2_dx_countlimit *limit;
-       int             i;
+       struct ext2_dx_countlimit       limit;
+       struct ext2_dx_entry            e;
+       int                             hash, i;
+       
 
-       limit = (struct ext2_dx_countlimit *) ent;
+       limit = *((struct ext2_dx_countlimit *) ent);
+       limit.count = ext2fs_le16_to_cpu(limit.count);
+       limit.limit = ext2fs_le16_to_cpu(limit.limit);
 
-       fprintf(pager, "Number of entries (count): %d\n", limit->count);
-       fprintf(pager, "Number of entries (limit): %d\n", limit->limit);
+       fprintf(pager, "Number of entries (count): %d\n", limit.count);
+       fprintf(pager, "Number of entries (limit): %d\n", limit.limit);
 
-       for (i=0; i < limit->count; i++)
-               fprintf(pager, "Entry #%d: Hash 0x%08x, block %d\n", i,
-                      i ? ent[i].hash : 0, ent[i].block);
+       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,
+                       hash, (hash & 1) ? " (**)" : "",
+                       ext2fs_le32_to_cpu(ent[i].block));
+               }
 
        fprintf(pager, "\n");
 
-       for (i=0; i < limit->count; i++) {
+       for (i=0; i < limit.count; i++) {
+               e.hash = ext2fs_le32_to_cpu(ent[i].hash);
+               e.block = ext2fs_le32_to_cpu(ent[i].block);
                fprintf(pager, "Entry #%d: Hash 0x%08x, block %d\n", i,
-                      i ? ent[i].hash : 0, ent[i].block);
+                      i ? e.hash : 0, e.block);
                if (level)
-                       htree_dump_int_block(fs, ino, inode,
-                                            ent[i].block, buf, level-1);
+                       htree_dump_int_block(fs, ino, inode, rootnode,
+                                            e.block, buf, level-1);
                else
-                       htree_dump_leaf_node(fs, ino, inode,
-                                            ent[i].block, buf);
+                       htree_dump_leaf_node(fs, ino, inode, rootnode,
+                                            e.block, buf);
        }
 
        fprintf(pager, "---------------------\n");
@@ -127,6 +139,7 @@ static void htree_dump_int_node(ext2_filsys fs, ext2_ino_t ino,
 
 static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
                                 struct ext2_inode *inode,
+                                struct ext2_dx_root_info * rootnode,
                                 blk_t blk, char *buf, int level)
 {
        char            *cbuf;
@@ -153,7 +166,8 @@ static void htree_dump_int_block(ext2_filsys fs, ext2_ino_t ino,
                return;
        }
 
-       htree_dump_int_node(fs, ino, inode, (struct ext2_dx_entry *) (buf+8),
+       htree_dump_int_node(fs, ino, inode, rootnode,
+                           (struct ext2_dx_entry *) (buf+8),
                            cbuf, level);
        free(cbuf);
 }
@@ -164,12 +178,10 @@ void do_htree_dump(int argc, char *argv[])
 {
        ext2_ino_t      ino;
        struct ext2_inode inode;
-       int             retval;
-       int             i, c;
-       int             flags;
+       int             c;
        int             long_opt;
-       void            *buf = NULL;
-       struct          ext2_dx_root_info  *root;
+       char            *buf = NULL;
+       struct          ext2_dx_root_info  *rootnode;
        struct          ext2_dx_entry *ent;
        struct          ext2_dx_countlimit *limit;
        errcode_t       errcode;
@@ -179,10 +191,7 @@ void do_htree_dump(int argc, char *argv[])
 
        pager = open_pager();
 
-       optind = 0;
-#ifdef HAVE_OPTRESET
-       optreset = 1;           /* Makes BSD getopt happy */
-#endif
+       reset_getopt();
        while ((c = getopt (argc, argv, "l")) != EOF) {
                switch (c) {
                case 'l':
@@ -193,7 +202,7 @@ void do_htree_dump(int argc, char *argv[])
 
        if (argc > optind+1) {
                com_err(0, 0, "Usage: htree_dump [-l] file");
-               return;
+               goto errout;
        }
 
        if (argc == optind)
@@ -201,25 +210,25 @@ void do_htree_dump(int argc, char *argv[])
        else
                ino = string_to_inode(argv[optind]);
        if (!ino)
-               return;
+               goto errout;
 
        if (debugfs_read_inode(ino, &inode, argv[1]))
-               return;
+               goto errout;
 
        if (!LINUX_S_ISDIR(inode.i_mode)) {
                com_err(argv[0], 0, "Not a directory");
-               return;
+               goto errout;
        }
        
        if ((inode.i_flags & EXT2_BTREE_FL) == 0) {
                com_err(argv[0], 0, "Not a hash-indexed directory");
-               return;
+               goto errout;
        }
 
        buf = malloc(2*current_fs->blocksize);
        if (!buf) {
                com_err(argv[0], 0, "Couldn't allocate htree buffer");
-               return;
+               goto errout;
        }
 
        errcode = io_channel_read_blk(current_fs->io, inode.i_block[0], 
@@ -229,21 +238,21 @@ void do_htree_dump(int argc, char *argv[])
                goto errout;
        }
 
-       root = (struct ext2_dx_root_info *) (buf + 24);
+       rootnode = (struct ext2_dx_root_info *) (buf + 24);
 
        fprintf(pager, "Root node dump:\n");
-       fprintf(pager, "\t Reserved zero: %d\n", root->reserved_zero);
-       fprintf(pager, "\t Hash Version: %d\n", root->hash_version);
-       fprintf(pager, "\t Info length: %d\n", root->info_length);
-       fprintf(pager, "\t Indirect levels: %d\n", root->indirect_levels);
-       fprintf(pager, "\t Flags: %d\n", root->unused_flags);
+       fprintf(pager, "\t Reserved zero: %d\n", rootnode->reserved_zero);
+       fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version);
+       fprintf(pager, "\t Info length: %d\n", rootnode->info_length);
+       fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels);
+       fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags);
 
-       ent = (struct ext2_dx_entry *) (buf + 24 + root->info_length);
+       ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length);
        limit = (struct ext2_dx_countlimit *) ent;
 
-       htree_dump_int_node(current_fs, ino, &inode, ent,
+       htree_dump_int_node(current_fs, ino, &inode, rootnode, ent,
                            buf + current_fs->blocksize,
-                           root->indirect_levels);
+                           rootnode->indirect_levels);
 
 errout:
        if (buf)
@@ -256,19 +265,34 @@ errout:
  */
 void do_dx_hash(int argc, char *argv[])
 {
-       ext2_dirhash_t hash;
+       ext2_dirhash_t hash, minor_hash;
        errcode_t       err;
+       int             c;
+       int             hash_version = 0;
+       __u32           hash_seed[4];
        
-       if (argc != 2) {
+       hash_seed[0] = hash_seed[1] = hash_seed[2] = hash_seed[3] = 0;
+
+       reset_getopt();
+       while ((c = getopt (argc, argv, "h:")) != EOF) {
+               switch (c) {
+               case 'h':
+                       hash_version = atoi(optarg);
+                       break;
+               }
+       }
+       if (optind != argc-1) {
                com_err(argv[0], 0, "usage: dx_hash filename");
                return;
        }
-       err = ext2fs_dirhash(0, argv[1], strlen(argv[1]), &hash);
+       err = ext2fs_dirhash(hash_version, argv[optind], strlen(argv[optind]),
+                            hash_seed, &hash, &minor_hash);
        if (err) {
                com_err(argv[0], err, "while caclulating hash");
                return;
        }
-       printf("Hash of %s is 0x%0x\n", argv[1], hash);
+       printf("Hash of %s is 0x%0x (minor 0x%0x)\n", argv[optind],
+              hash, minor_hash);
 }
 
 /*
@@ -289,9 +313,6 @@ static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
 void do_dirsearch(int argc, char *argv[])
 {
        ext2_ino_t      inode;
-       int             retval;
-       int             c;
-       int             flags;
        struct process_block_struct pb;
        
        if (check_fs_open(argv[0]))
@@ -321,13 +342,15 @@ void do_dirsearch(int argc, char *argv[])
 
 
 static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
-                           e2_blkcnt_t blockcnt, blk_t ref_blk, 
-                           int ref_offset, void *priv_data)
+                           e2_blkcnt_t blockcnt, 
+                           blk_t ref_blk EXT2FS_ATTR((unused)),
+                           int ref_offset EXT2FS_ATTR((unused)),
+                           void *priv_data)
 {
        struct process_block_struct *p;
        struct ext2_dir_entry *dirent;
        errcode_t               errcode;
-       int                     offset = 0;
+       unsigned int            offset = 0;
 
        if (blockcnt < 0)
                return 0;
@@ -337,7 +360,7 @@ static int search_dir_block(ext2_filsys fs, blk_t *blocknr,
        errcode = io_channel_read_blk(current_fs->io, *blocknr, 1, p->buf);
        if (errcode) {
                com_err("search_dir_block", errcode,
-                       "while reading block %lu", *blocknr);
+                       "while reading block %lu", (unsigned long) *blocknr);
                return BLOCK_ABORT;
        }