Skip to content

Commit

Permalink
ext2: add support to various inode sizes
Browse files Browse the repository at this point in the history
the script used for generating test inputs:

    declare -A sizes=(
	[1024,1024]=$((130048 + 1024))
	[2048,512]=$((  45056 + 2048))
	[4096,128]=$((  53248 + 4096))
    )

    adjust_size()
    {
	local f=$1
	local bsize=$2
	local isize=$3

	truncate --size=${sizes[$bsize,$isize]} $f
    }

    makeimg()
    {
	local bsize=$1
	local isize=$2

	local f=tests/files/bsize-${bsize}-isize-${isize}.ext2
	dd if=/dev/zero of=$f bs=${bsize} count=128
	mkfs.ext2 -I ${isize} -b ${bsize} $f
	sudo mount $f __tmp__
	echo hello | sudo dd if=/dev/stdin of=__tmp__/source
	(cd __tmp__/; sudo ln -s source target)
	sudo umount __tmp__
	adjust_size $f $bsize $isize
    }

    mkdir -p __tmp__
    makeimg 1024 1024
    makeimg 2048 512
    makeimg 4096 128
    rmdir __tmp__

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake authored and vstinner committed Feb 12, 2024
1 parent f9ed7d5 commit 65f8c20
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions hachoir/parser/file_system/ext2.py
Expand Up @@ -201,10 +201,13 @@ class Inode(FieldSet):
7: "Reserved group descriptors",
8: "EXT3 journal"
}
static_size = (68 + 15 * 4) * 8

def __init__(self, parent, name, index):
FieldSet.__init__(self, parent, name, None)
inode_size = self["/superblock/inode_size"].value
if inode_size == 0:
inode_size = 128
self._size = inode_size * 8
self.uniq_id = 1 + index

def createDescription(self):
Expand Down Expand Up @@ -751,7 +754,10 @@ def validate(self):
return "Invalid magic number"
if not (0 <= self["superblock/log_block_size"].value <= 2):
return "Invalid (log) block size"
if self["superblock/inode_size"].value not in (0, 128):
blocksize = (1 << 10) << self["superblock/log_block_size"].value
acceptable_inode_sizes = (s for s in [0, 128, 256, 512, 1024, 2048, 4096]
if s <= blocksize)
if self["superblock/inode_size"].value not in acceptable_inode_sizes:
return "Unsupported inode size"
return True

Expand Down
Binary file added tests/files/bsize-1024-isize-1024.ext2
Binary file not shown.
Binary file added tests/files/bsize-2048-isize-512.ext2
Binary file not shown.
Binary file added tests/files/bsize-4096-isize-128.ext2
Binary file not shown.
10 changes: 10 additions & 0 deletions tests/test_parser.py
Expand Up @@ -348,6 +348,16 @@ def test_ext2_variety_inode_types(self):
self.checkDesc(parser, "/group[0]/inode_table/inode[12]",
"Inode 13: Symbolic link (-> XYZ), size=3 bytes, mode=lrwxrwxrwx")

def test_ext2_various_inode_sizes(self):
for bsize, isize in [(1024, 1024), (2048, 512), (4096, 128)]:
fname = "bsize-%d-isize-%d.ext2" % (bsize, isize)
parser = self.parse(fname)
self.checkValue(parser, "/superblock/inode_size", isize)
self.checkValue(parser, "/group[0]/inode_table/inode[11]/size", 6)
self.checkDesc(parser, "/group[0]/inode_table/inode[12]",
"Inode 13: Symbolic link (-> source), size=6 bytes, mode=lrwxrwxrwx")
self.checkValue(parser, "/group[0]/inode[11]block[0]", b"hello\n" + b'\0' * (bsize - 6))

def test_bmp2(self):
parser = self.parse("article01.bmp")
self.checkDisplay(parser, "/header/red_mask", '0x00ff0000')
Expand Down

0 comments on commit 65f8c20

Please sign in to comment.