Make the EXT2 magic number check more robust

Project:JNode FS
Component:Code
Category:feature request
Priority:minor
Assigned:Unassigned
Status:patch (code needs review)
Description

The EXT2 magic number check is only two bytes long and matches random files when looking for disk images. The following change to Ext2FileSystemType at least checks the values in a second location:

        //need to check the magic
        ByteBuffer magic = ByteBuffer.allocate(2);
        ByteBuffer revLevel = ByteBuffer.allocate(4);
        try {
            devApi.read(1024 + 56, magic);
            devApi.read(1024 + 78, revLevel);
        } catch (IOException e) {
            return false;
        }
        return
            (Ext2Utils.get16(magic.array(), 0) == 0xEF53) &&
            (Ext2Utils.get32(revLevel.array(), 0) == 0 || Ext2Utils.get32(revLevel.array(), 0) == 1);

Below are some s_rev_level usages in the linux kernel:

EXT2 code:
http://www.kneuro.net/cgi-bin/lxr/http/source/fs/ext2/super.c#L288
http://www.kneuro.net/cgi-bin/lxr/http/source/include/linux/ext2_fs.h#L4...

EXT3 code:
http://lxr.free-electrons.com/source/fs/ext3/super.c#L338
http://lxr.free-electrons.com/source/include/linux/ext3_fs.h#L603