diff: use tempfile module
Also add some code comments explaining how the fields in "struct diff_tempfile" are used. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Aug 12, 2015 at 19:12 UTC
284098f13fd1c378883ed844fc2966e0b78bbbe7
1 file changed
+23
-23
diff.c
+23
-23
@@ -2,6 +2,7 @@
2
* Copyright (C) 2005 Junio C Hamano
3
*/
4
#include "cache.h"
5
+#include "tempfile.h"
6
#include "quote.h"
7
#include "diff.h"
8
#include "diffcore.h"
@@ -308,11 +309,26 @@ static const char *external_diff(void)
309
return external_diff_cmd;
310
}
311
312
+/*
313
+ * Keep track of files used for diffing. Sometimes such an entry
314
+ * refers to a temporary file, sometimes to an existing file, and
315
+ * sometimes to "/dev/null".
316
+ */
317
static struct diff_tempfile {
312
- const char *name; /* filename external diff should read from */
318
+ /*
319
+ * filename external diff should read from, or NULL if this
320
+ * entry is currently not in use:
321
+ */
322
+ const char *name;
323
+
324
char hex[41];
325
char mode[10];
315
- char tmp_path[PATH_MAX];
326
+
327
+ /*
328
+ * If this diff_tempfile instance refers to a temporary file,
329
+ * this tempfile object is used to manage its lifetime.
330
+ */
331
+ struct tempfile tempfile;
332
} diff_temp[2];
333
334
typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
@@ -564,25 +580,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {
580
die("BUG: diff is failing to clean up its tempfiles");
581
}
582
567
-static int remove_tempfile_installed;
568
-
583
static void remove_tempfile(void)
584
{
585
int i;
586
for (i = 0; i < ARRAY_SIZE(diff_temp); i++) {
573
- if (diff_temp[i].name == diff_temp[i].tmp_path)
574
- unlink_or_warn(diff_temp[i].name);
587
+ if (is_tempfile_active(&diff_temp[i].tempfile))
588
+ delete_tempfile(&diff_temp[i].tempfile);
589
diff_temp[i].name = NULL;
590
}
591
}
592
579
-static void remove_tempfile_on_signal(int signo)
580
-{
581
- remove_tempfile();
582
- sigchain_pop(signo);
583
- raise(signo);
584
-}
585
-
593
static void print_line_count(FILE *file, int count)
594
{
595
switch (count) {
@@ -2817,8 +2824,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
2824
strbuf_addstr(&template, "XXXXXX_");
2825
strbuf_addstr(&template, base);
2826
2820
- fd = git_mkstemps(temp->tmp_path, PATH_MAX, template.buf,
2821
- strlen(base) + 1);
2827
+ fd = mks_tempfile_ts(&temp->tempfile, template.buf, strlen(base) + 1);
2828
if (fd < 0)
2829
die_errno("unable to create temp-file");
2830
if (convert_to_working_tree(path,
@@ -2828,8 +2834,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
2834
}
2835
if (write_in_full(fd, blob, size) != size)
2836
die_errno("unable to write temp-file");
2831
- close(fd);
2832
- temp->name = temp->tmp_path;
2837
+ close_tempfile(&temp->tempfile);
2838
+ temp->name = get_tempfile_path(&temp->tempfile);
2839
strcpy(temp->hex, sha1_to_hex(sha1));
2840
temp->hex[40] = 0;
2841
sprintf(temp->mode, "%06o", mode);
@@ -2854,12 +2860,6 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
2860
return temp;
2861
}
2862
2857
- if (!remove_tempfile_installed) {
2858
- atexit(remove_tempfile);
2859
- sigchain_push_common(remove_tempfile_on_signal);
2860
- remove_tempfile_installed = 1;
2861
- }
2862
-
2863
if (!S_ISGITLINK(one->mode) &&
2864
(!one->sha1_valid ||
2865
reuse_worktree_file(name, one->sha1, 1))) {