masahiro331 / go-ext4-filesystem

Apache License 2.0
7 stars 10 forks source link

Error reading directory entries; extents() using incorrect buffer size #17

Open racingmars opened 1 week ago

racingmars commented 1 week ago

I'm attempting to open the file "/usr/lib/test.txt" on my ext4 filesystem, but receiving an error.

If I do _, err := filesystem.Open("/usr/lib/test.txt"), err is:

open usr/lib/test.txt: failed to read directory: read directory usr/lib/: failed to list directory entries inode(15336348): failed to get directory entries: failed to get extents: failed to get extents: failed to get extents: failed to read leaf node extent: unexpected EOF

Going through the code in the debugger, I believe I know what the problem is.

In the extents() function https://github.com/masahiro331/go-ext4-filesystem/blob/ca14e6327bbda08ab71ef90ae78ff23cb638bfa4/ext4/ext4.go#L121 I can see that I get to a point where extentHeader.Entries is 50. The extent tree header itself is 12 bytes, and whether internal or leaf nodes of the extent tree, the entries are each 12 bytes. So the header + 50 entries is 612 bytes.

However, when reading the block to pass in to recursive calls to extents(), the buffer is always created to match the SectorSize, 512.

https://github.com/masahiro331/go-ext4-filesystem/blob/ca14e6327bbda08ab71ef90ae78ff23cb638bfa4/ext4/ext4.go#L145

That is why when looping through the 50 entries, extents() is getting an EOF. It creates a byte reader around the 512 bytes it was called with, but it needs to be able to read 612 bytes of data.

I believe the read should be a full block, not just a sector of data. If I change the line b := make([]byte, SectorSize) to b := make([]byte, ext4.sb.GetBlockSize()), I no longer get the error.