From: <ga...@us...> - 2012-08-10 07:26:29
|
Revision: 5925 http://jnode.svn.sourceforge.net/jnode/?rev=5925&view=rev Author: galatnm Date: 2012-08-10 07:26:22 +0000 (Fri, 10 Aug 2012) Log Message: ----------- Further support for ext4 extents and bug fixes Modified Paths: -------------- trunk/fs/src/fs/org/jnode/fs/ext2/ExtentHeader.java trunk/fs/src/fs/org/jnode/fs/ext2/ExtentIndex.java trunk/fs/src/fs/org/jnode/fs/ext2/INode.java Modified: trunk/fs/src/fs/org/jnode/fs/ext2/ExtentHeader.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/ext2/ExtentHeader.java 2012-08-10 07:23:31 UTC (rev 5924) +++ trunk/fs/src/fs/org/jnode/fs/ext2/ExtentHeader.java 2012-08-10 07:26:22 UTC (rev 5925) @@ -103,33 +103,74 @@ return extentEntries; } - public long getBlockNumber(long index) { + public long getBlockNumber(Ext2FileSystem fs, long index) throws IOException { if (getDepth() > 0) { - ExtentIndex[] indexes = getIndexEntries(); + ExtentIndex extentIndex = binarySearchIndexes(index, getIndexEntries()); + byte[] indexData = fs.getBlock(extentIndex.getLeafLow()); - throw new UnsupportedOperationException(); + ExtentHeader indexHeader = new ExtentHeader(indexData); + return indexHeader.getBlockNumber(fs, index); } else { - Extent[] extents = getExtentEntries(); + Extent extent = binarySearchExtents(index, getExtentEntries()); + return index - extent.getBlockIndex() + extent.getStartLow(); + } + } - int lowIndex = 0; - int highIndex = extents.length - 1; - Extent extent = null; + /** + * Performs a binary search in the extent indexes. + * + * @param index the index of the block to match. + * @param indexes the indexes to search in. + * @return the matching index. + */ + private ExtentIndex binarySearchIndexes(long index, ExtentIndex[] indexes) + { + int lowIndex = 0; + int highIndex = indexes.length - 1; + ExtentIndex extentIndex = null; - while (lowIndex <= highIndex) { - int middle = lowIndex + (highIndex - lowIndex) / 2; - extent = extents[middle]; + while (lowIndex <= highIndex) { + int middle = lowIndex + (highIndex - lowIndex) / 2; + extentIndex = indexes[middle]; - if (index < extent.getBlockIndex()) { - highIndex = middle - 1; - } - else { - lowIndex = middle + 1; - } + if (index < extentIndex.getBlockIndex()) { + highIndex = middle - 1; } + else { + lowIndex = middle + 1; + } + } - return index - extent.getBlockIndex() + extent.getStartLow(); + return indexes[Math.max(0, lowIndex - 1)]; + } + + /** + * Performs a binary search in the extents. + * + * @param index the index of the block to match. + * @param extents the extents to search in. + * @return the matching extent. + */ + private Extent binarySearchExtents(long index, Extent[] extents) + { + int lowIndex = 0; + int highIndex = extents.length - 1; + Extent extent = null; + + while (lowIndex <= highIndex) { + int middle = lowIndex + (highIndex - lowIndex) / 2; + extent = extents[middle]; + + if (index < extent.getBlockIndex()) { + highIndex = middle - 1; + } + else { + lowIndex = middle + 1; + } } + + return extents[Math.max(0, lowIndex - 1)]; } @Override Modified: trunk/fs/src/fs/org/jnode/fs/ext2/ExtentIndex.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/ext2/ExtentIndex.java 2012-08-10 07:23:31 UTC (rev 5924) +++ trunk/fs/src/fs/org/jnode/fs/ext2/ExtentIndex.java 2012-08-10 07:26:22 UTC (rev 5925) @@ -24,6 +24,11 @@ public ExtentIndex(byte[] data) { this.data = new byte[EXTENT_INDEX_LENGTH]; System.arraycopy(data, 0, this.data, 0, EXTENT_INDEX_LENGTH); + + // Safety check + if (getLeafHigh() != 0) { + throw new UnsupportedOperationException("Extent indexes that use the high bits aren't supported yet"); + } } public long getBlockIndex() { @@ -37,4 +42,10 @@ public int getLeafHigh() { return Ext2Utils.get16(data, 8); } + + @Override + public String toString() { + return String.format("ExtentIndex: blockindex:%d leaf(low:%d high:%d)", getBlockIndex(), getLeafLow(), + getLeafHigh()); + } } Modified: trunk/fs/src/fs/org/jnode/fs/ext2/INode.java =================================================================== --- trunk/fs/src/fs/org/jnode/fs/ext2/INode.java 2012-08-10 07:23:31 UTC (rev 5924) +++ trunk/fs/src/fs/org/jnode/fs/ext2/INode.java 2012-08-10 07:26:22 UTC (rev 5925) @@ -300,7 +300,7 @@ extentHeader = new ExtentHeader(getINodeBlockData()); } - return extentHeader.getBlockNumber(i); + return extentHeader.getBlockNumber(fs, i); } else { return getDataBlockNrIndirect(i); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |