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

[JGIT PATCH 2/2] index-pack: Use fsync to ensure received pack data is on disk

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 27, 2008, 21:48 UTC
Message-ID
<1219873724-13348-2-git-send-email-spearce@spearce.org>
In-Reply-To
<1219873724-13348-1-git-send-email-spearce@spearce.org>

C Git uses fsync() to ensure that a pack file is stable on disk before it starts to delete any other copies of the data, or before it tries to update refs to refer to data stored in the pack. This makes it a lot less likely that a repository will see corruption due to refs pointing at unavailable objects.

We now force our pack and its associated .idx file to disk before we close the file descriptors, but after we have finished reading and writing all of the data. This way we can be fairly certain a future user of the data (like a RefUpdate) always has objects.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/transport/IndexPack.java  |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java b/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java
index db9268e..7b1f7ee 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java
@@ -259,6 +259,8 @@ public void index(final ProgressMonitor progress) throws IOException {
 						fixThinPack(progress);
 					}
 				}
+				if (packOut != null)
+					packOut.getChannel().force(true);
 
 				packDigest = null;
 				baseById = null;
@@ -510,6 +512,7 @@ private void writeIdx() throws IOException {
 			else
 				iw = PackIndexWriter.createVersion(os, outputVersion);
 			iw.write(list, packcsum);
+			os.getChannel().force(true);
 		} finally {
 			os.close();
 		}
-- 
1.6.0.174.gd789c
Previous: Shawn O. PearceNext: Nicolas Pitre
Message 2 of 5 in “index-pack: Avoid disk corruption yielding a valid pack footer checksum”
  1. 1/2 index-pack: Avoid disk corruption yielding a valid pack footer checksumShawn O. Pearce, Aug 27, 2008
  2. 2/2 index-pack: Use fsync to ensure received pack data is on diskShawn O. Pearce, Aug 27, 2008
  3. Nicolas PitreAug 28, 2008
  4. Shawn O. PearceAug 28, 2008
  5. Nicolas PitreAug 28, 2008

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.