errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino, struct problem_context *pctx) { ext2_filsys fs = ctx->fs; errcode_t retval; struct ext2_inode inode; char *dir_buf = 0; struct fill_dir_struct fd; struct out_dir outdir; outdir.max = outdir.num = 0; outdir.buf = 0; outdir.hashes = 0; e2fsck_read_inode(ctx, ino, &inode, "rehash_dir"); if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, EXT4_FEATURE_INCOMPAT_INLINE_DATA) && (inode.i_flags & EXT4_INLINE_DATA_FL)) return 0; retval = ENOMEM; fd.harray = 0; dir_buf = malloc(inode.i_size); if (!dir_buf) goto errout; fd.max_array = inode.i_size / 32; fd.num_array = 0; fd.harray = malloc(fd.max_array * sizeof(struct hash_entry)); if (!fd.harray) goto errout; fd.ctx = ctx; fd.buf = dir_buf; fd.inode = &inode; fd.ino = ino; fd.err = 0; fd.dir_size = 0; fd.compress = 0; fd.dir = ino; if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) || (inode.i_size / fs->blocksize) < 2) fd.compress = 1; fd.parent = 0; retry_nohash: /* Read in the entire directory into memory */ retval = ext2fs_block_iterate3(fs, ino, 0, 0, fill_dir_block, &fd); if (fd.err) { retval = fd.err; goto errout; } /* * If the entries read are less than a block, then don't index * the directory */ if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) { fd.compress = 1; fd.dir_size = 0; fd.num_array = 0; goto retry_nohash; } #if 0 printf("%d entries (%d bytes) found in inode %d\n", fd.num_array, fd.dir_size, ino); #endif /* Sort the list */ resort: if (fd.compress && fd.num_array > 1) qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry), hash_cmp); else qsort(fd.harray, fd.num_array, sizeof(struct hash_entry), hash_cmp); /* * Look for duplicates */ if (duplicate_search_and_fix(ctx, fs, ino, &fd)) goto resort; if (ctx->options & E2F_OPT_NO) { retval = 0; goto errout; } /* Sort non-hashed directories by inode number */ if (fd.compress && fd.num_array > 1) qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry), ino_cmp); /* * Copy the directory entries. In a htree directory these * will become the leaf nodes. */ retval = copy_dir_entries(ctx, &fd, &outdir); if (retval) goto errout; free(dir_buf); dir_buf = 0; if (!fd.compress) { /* Calculate the interior nodes */ retval = calculate_tree(fs, &outdir, ino, fd.parent); if (retval) goto errout; } retval = write_directory(ctx, fs, &outdir, ino, &inode, fd.compress); if (retval) goto errout; if (ctx->options & E2F_OPT_CONVERT_BMAP) retval = e2fsck_rebuild_extents_later(ctx, ino); else retval = e2fsck_check_rebuild_extents(ctx, ino, &inode, pctx); errout: free(dir_buf); free(fd.harray); free_out_dir(&outdir); return retval; }
errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino) { ext2_filsys fs = ctx->fs; errcode_t retval; struct ext2_inode inode; char *dir_buf = 0; struct fill_dir_struct fd; struct out_dir outdir; outdir.max = outdir.num = 0; outdir.buf = 0; outdir.hashes = 0; e2fsck_read_inode(ctx, ino, &inode, "rehash_dir"); retval = ENOMEM; fd.harray = 0; dir_buf = malloc(inode.i_size); if (!dir_buf) goto errout; fd.max_array = inode.i_size / 32; fd.num_array = 0; fd.harray = malloc(fd.max_array * sizeof(struct hash_entry)); if (!fd.harray) goto errout; fd.ctx = ctx; fd.buf = dir_buf; fd.inode = &inode; fd.err = 0; fd.dir_size = 0; fd.compress = 0; if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) || (inode.i_size / fs->blocksize) < 2) fd.compress = 1; fd.parent = 0; /* Read in the entire directory into memory */ retval = ext2fs_block_iterate2(fs, ino, 0, 0, fill_dir_block, &fd); if (fd.err) { retval = fd.err; goto errout; } #if 0 printf("%d entries (%d bytes) found in inode %d\n", fd.num_array, fd.dir_size, ino); #endif /* Sort the list */ resort: if (fd.compress) qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry), ino_cmp); else qsort(fd.harray, fd.num_array, sizeof(struct hash_entry), hash_cmp); /* * Look for duplicates */ if (duplicate_search_and_fix(ctx, fs, ino, &fd)) goto resort; if (ctx->options & E2F_OPT_NO) { retval = 0; goto errout; } /* * Copy the directory entries. In a htree directory these * will become the leaf nodes. */ retval = copy_dir_entries(ctx, &fd, &outdir); if (retval) goto errout; free(dir_buf); dir_buf = 0; if (!fd.compress) { /* Calculate the interior nodes */ retval = calculate_tree(fs, &outdir, ino, fd.parent); if (retval) goto errout; } retval = write_directory(ctx, fs, &outdir, ino, fd.compress); if (retval) goto errout; errout: free(dir_buf); free(fd.harray); free_out_dir(&outdir); return retval; }