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

[PATCH v2' 12/16] diff: use tempfile module

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Aug 12, 2015, 17:12 UTC
Message-ID
<2c01cbf0b64b95ddaca54e7f2f9df7bab8bab6cd.1439399089.git.mhagger@alum.mit.edu>
In-Reply-To
<xmqqy4hglar6.fsf@gitster.dls.corp.google.com>

Also add some code comments explaining how the fields in "struct diff_tempfile" are used.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
This is a replacement for tempfile patch v2 12/16 that includes some
extra code comments. It is also available from my GitHub repo [1] on
branch "tempfile".
[1] https://github.com/mhagger/git
 diff.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)
diff --git a/diff.c b/diff.c
index 7500c55..528d25c 100644
--- a/diff.c
+++ b/diff.c
@@ -2,6 +2,7 @@
  * Copyright (C) 2005 Junio C Hamano
  */
 #include "cache.h"
+#include "tempfile.h"
 #include "quote.h"
 #include "diff.h"
 #include "diffcore.h"
@@ -308,11 +309,26 @@ static const char *external_diff(void)
 	return external_diff_cmd;
 }
 
+/*
+ * Keep track of files used for diffing. Sometimes such an entry
+ * refers to a temporary file, sometimes to an existing file, and
+ * sometimes to "/dev/null".
+ */
 static struct diff_tempfile {
-	const char *name; /* filename external diff should read from */
+	/*
+	 * filename external diff should read from, or NULL if this
+	 * entry is currently not in use:
+	 */
+	const char *name;
+
 	char hex[41];
 	char mode[10];
-	char tmp_path[PATH_MAX];
+
+	/*
+	 * If this diff_tempfile instance refers to a temporary file,
+	 * this tempfile object is used to manage its lifetime.
+	 */
+	struct tempfile tempfile;
 } diff_temp[2];
 
 typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
@@ -564,25 +580,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {
 	die("BUG: diff is failing to clean up its tempfiles");
 }
 
-static int remove_tempfile_installed;
-
 static void remove_tempfile(void)
 {
 	int i;
 	for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
-		if (diff_temp[i].name == diff_temp[i].tmp_path)
-			unlink_or_warn(diff_temp[i].name);
+		if (is_tempfile_active(&diff_temp[i].tempfile))
+			delete_tempfile(&diff_temp[i].tempfile);
 		diff_temp[i].name = NULL;
 	}
 }
 
-static void remove_tempfile_on_signal(int signo)
-{
-	remove_tempfile();
-	sigchain_pop(signo);
-	raise(signo);
-}
-
 static void print_line_count(FILE *file, int count)
 {
 	switch (count) {
@@ -2817,8 +2824,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
 	strbuf_addstr(&template, "XXXXXX_");
 	strbuf_addstr(&template, base);
 
-	fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf,
-			strlen(base) + 1);
+	fd = mks_tempfile_ts(&temp->tempfile, template.buf, strlen(base) + 1);
 	if (fd < 0)
 		die_errno("unable to create temp-file");
 	if (convert_to_working_tree(path,
@@ -2828,8 +2834,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
 	}
 	if (write_in_full(fd, blob, size) != size)
 		die_errno("unable to write temp-file");
-	close(fd);
-	temp->name = temp->tmp_path;
+	close_tempfile(&temp->tempfile);
+	temp->name = get_tempfile_path(&temp->tempfile);
 	strcpy(temp->hex, sha1_to_hex(sha1));
 	temp->hex[40] = 0;
 	sprintf(temp->mode, "%06o", mode);
@@ -2854,12 +2860,6 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
 		return temp;
 	}
 
-	if (!remove_tempfile_installed) {
-		atexit(remove_tempfile);
-		sigchain_push_common(remove_tempfile_on_signal);
-		remove_tempfile_installed = 1;
-	}
-
 	if (!S_ISGITLINK(one->mode) &&
 	    (!one->sha1_valid ||
 	     reuse_worktree_file(name, one->sha1, 1))) {
-- 
2.5.0
Previous: Junio C HamanoNext: Michael Haggerty
Message 21 of 29 in “Introduce a tempfile module”
  1. 00/16 Introduce a tempfile moduleMichael Haggerty, Aug 10, 2015
  2. 01/16 Move lockfile documentation to lockfile.h and lockfile.cMichael Haggerty, Aug 10, 2015
  3. Junio C HamanoAug 11, 2015
  4. 02/16 create_bundle(): duplicate file descriptor to avoid closing it twiceMichael Haggerty, Aug 10, 2015
  5. 03/16 lockfile: add accessors get_lock_file_fd() and get_lock_file_fp()Michael Haggerty, Aug 10, 2015
  6. Junio C HamanoAug 11, 2015
  7. 04/16 lockfile: add accessor get_lock_file_path()Michael Haggerty, Aug 10, 2015
  8. Junio C HamanoAug 11, 2015
  9. 05/16 commit_lock_file(): use get_locked_file_path()Michael Haggerty, Aug 10, 2015
  10. 06/16 tempfile: a new module for handling temporary filesMichael Haggerty, Aug 10, 2015
  11. 07/16 prepare_tempfile_object(): new function, extracted from create_tempfile()Michael Haggerty, Aug 10, 2015
  12. Junio C HamanoAug 11, 2015
  13. 08/16 tempfile: add several functions for creating temporary filesMichael Haggerty, Aug 10, 2015
  14. 09/16 register_tempfile(): new function to handle an existing temporary fileMichael Haggerty, Aug 10, 2015
  15. 10/16 write_shared_index(): use tempfile moduleMichael Haggerty, Aug 10, 2015
  16. 11/16 setup_temporary_shallow(): use tempfile moduleMichael Haggerty, Aug 10, 2015
  17. 12/16 diff: use tempfile moduleMichael Haggerty, Aug 10, 2015
  18. Junio C HamanoAug 11, 2015
  19. Michael HaggertyAug 12, 2015
  20. Junio C HamanoAug 12, 2015
  21. 12/16 diff: use tempfile moduleMichael Haggerty, Aug 12, 2015
  22. 13/16 lock_repo_for_gc(): compute the path to "gc.pid" only onceMichael Haggerty, Aug 10, 2015
  23. Junio C HamanoAug 11, 2015
  24. Junio C HamanoAug 11, 2015
  25. 14/16 gc: use tempfile module to handle gc.pid fileMichael Haggerty, Aug 10, 2015
  26. 15/16 credential-cache--daemon: delete socket from main()Michael Haggerty, Aug 10, 2015
  27. 16/16 credential-cache--daemon: use tempfile moduleMichael Haggerty, Aug 10, 2015
  28. Junio C HamanoAug 11, 2015
  29. Michael HaggertyAug 12, 2015

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.