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

[PATCH] fast-import.c: don't try to commit marks file if write failed

From
Brandon Casey <casey@nrlssc.navy.mil>
Date
Jan 17, 2008, 16:58 UTC
Message-ID
<478F893A.6050105@nrlssc.navy.mil>
In-Reply-To
<7vk5m9kvf7.fsf@gitster.siamese.dyndns.org>

We also move the assignment of -1 to the lock file descriptor up, so that rollback_lock_file() can be called safely after a possible attempt to fclose(). This matches the contents of the 'if' statement just above testing success of fdopen().

Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil>
---

Since we forget the lock file descriptor there is a chance that we will leave the file open if a write error occurs. We'll still delete the file. I don't think it's worth bending over backwards to make sure the file is closed on this failure.

-brandon
 fast-import.c |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
diff --git a/fast-import.c b/fast-import.c
index 3609c24..4cf5092 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1545,19 +1545,27 @@ static void dump_marks(void)
 		return;
 	}
 
-	dump_marks_helper(f, 0, marks);
-	if (ferror(f) || fclose(f))
-		failure |= error("Unable to write marks file %s: %s",
-			mark_file, strerror(errno));
 	/*
-	 * Since the lock file was fdopen()'ed and then fclose()'ed above,
-	 * assign -1 to the lock file descriptor so that commit_lock_file()
+	 * Since the lock file was fdopen()'ed, it should not be close()'ed.
+	 * Assign -1 to the lock file descriptor so that commit_lock_file()
 	 * won't try to close() it.
 	 */
 	mark_lock.fd = -1;
-	if (commit_lock_file(&mark_lock))
-		failure |= error("Unable to write commit file %s: %s",
+
+	dump_marks_helper(f, 0, marks);
+	if (ferror(f) || fclose(f)) {
+		rollback_lock_file(&mark_lock);
+		failure |= error("Unable to write marks file %s: %s",
 			mark_file, strerror(errno));
+		return;
+	}
+
+	if (commit_lock_file(&mark_lock)) {
+		rollback_lock_file(&mark_lock);
+		failure |= error("Unable to commit marks file %s: %s",
+			mark_file, strerror(errno));
+		return;
+	}
 }
 
 static int read_next_command(void)
-- 
1.5.4.rc3.17.gb63a4
Previous: Johannes SixtNext: Junio C Hamano
Message 3 of 8 in “What's in git.git (stable frozen)”
  1. Junio C HamanoJan 17, 2008
  2. Johannes SixtJan 17, 2008
  3. fast-import.c: don't try to commit marks file if write failedBrandon Casey, Jan 17, 2008
  4. Junio C HamanoJan 17, 2008
  5. Shawn O. PearceJan 18, 2008
  6. Brandon CaseyJan 17, 2008
  7. Junio C HamanoJan 17, 2008
  8. Shawn O. PearceJan 18, 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.