git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[EGIT PATCH] Improve end-of-file detection in DirCache

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
Apr 8, 2009, 15:50 UTC
Message-ID
<1239205852-28138-1-git-send-email-robin.rosenberg@dewire.com>

When reading from an BufferInputStream attached to a FileInputStream the available() method seems to return the number of unread bytes in the buffer plus the unread number of bytes in the file.

There is no guarantee for this behaviour so this quick fix might not be as stable as we would wish.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
  http://code.google.com/p/egit/issues/detail?id=66
---
 .../src/org/spearce/jgit/dircache/DirCache.java    |   16 ++++++++++------
 1 files changed, 10 insertions(+), 6 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
index 8eb4022..657762e 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
@@ -45,7 +45,6 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
-import java.nio.channels.FileChannel;
 import java.security.DigestOutputStream;
 import java.security.MessageDigest;
 import java.util.Comparator;
@@ -344,8 +343,6 @@ public void clear() {
 
 	private void readFrom(final FileInputStream inStream) throws IOException,
 			CorruptObjectException {
-		final FileChannel fd = inStream.getChannel();
-		final long sizeOnDisk = fd.size();
 		final BufferedInputStream in = new BufferedInputStream(inStream);
 
 		// Read the index header and verify we understand it.
@@ -369,9 +366,16 @@ private void readFrom(final FileInputStream inStream) throws IOException,
 			sortedEntries[i] = new DirCacheEntry(infos, i * INFO_LEN, in);
 		lastModified = liveFile.lastModified();
 
-		// After the file entries are index extensions.
-		//
-		while (fd.position() - in.available() < sizeOnDisk - 20) {
+		/*
+		 * InputStream.available() on file input streams seems to return the
+		 * rest of the file i.e. buffer size + unread file on disk. There is no
+		 * guarantee for this so we need to fix this or we may miss extensions
+		 * when reading the index in a few races cases.
+		 *
+		 * That is currently no disaster though.
+		 */
+		while (in.available() > 20) {
+			// After the file entries are index extensions.
 			NB.readFully(in, hdr, 0, 8);
 			switch (NB.decodeInt32(hdr, 0)) {
 			case EXT_TREE: {
-- 
1.6.2.2.446.gfbdc0
Next: Shawn O. Pearce
Message 1 of 6 in “Improve end-of-file detection in DirCache”
  1. Improve end-of-file detection in DirCacheRobin Rosenberg, Apr 8, 2009
  2. 1/2 Improve end-of-file detection in DirCacheShawn O. Pearce, Apr 9, 2009
  3. 2/2 Validate the DIRC footer during readShawn O. Pearce, Apr 9, 2009
  4. Robin RosenbergApr 10, 2009
  5. Robin RosenbergApr 13, 2009
  6. Shawn O. PearceApr 13, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.