#!/bin/sh
# DF-3082 craft (host): build an ext2 image with an htree-indexed directory
# whose index has 2 leaf blocks (logical blocks 2 and 3 of the dir file).
#
# Layout produced (verified by parsing the dx root):
#   dir d: i_size 4096, blocks (dx-root, spare, leaf2, leaf3)
#   dx entries: hash < 0xad07d0a4 -> leaf block 2, else -> leaf block 3
#   inode flag 0x1000 (EXT2_INDEX_FL), fs feature dir_index
set -e
cd "$(dirname "$0")"
W=${W:-/tmp/opencode/e2work}
mkdir -p "$W"; cd "$W"
rm -f htree.img cmds.txt payloadname* 2>/dev/null || true
mke2fs -q -F -t ext2 -b 1024 -I 128 -O dir_index,^metadata_csum htree.img 16384
for i in $(seq -w 1 100); do printf 'x' > "payloadname$i"; done
{ echo "mkdir d"; echo "cd /d";
  for i in $(seq -w 1 100); do echo "write payloadname$i payloadname$i"; done
} > cmds.txt
debugfs -w -f cmds.txt htree.img >/dev/null 2>&1
e2fsck -fyD htree.img >/dev/null 2>&1
echo "image: $W/htree.img"
debugfs -R "stat d" htree.img 2>/dev/null | grep -E "Size:|Flags"
