2
2
* Copyright (C) 2005 Junio C Hamano
3
3
*/
4
4
#include "cache.h"
5
+ #include "tempfile.h"
5
6
#include "quote.h"
6
7
#include "diff.h"
7
8
#include "diffcore.h"
@@ -308,11 +309,26 @@ static const char *external_diff(void)
308
309
return external_diff_cmd ;
309
310
}
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
+ */
311
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
+
313
324
char hex [41 ];
314
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 ;
316
332
} diff_temp [2 ];
317
333
318
334
typedef unsigned long (* sane_truncate_fn )(char * line , unsigned long len );
@@ -564,25 +580,16 @@ static struct diff_tempfile *claim_diff_tempfile(void) {
564
580
die ("BUG: diff is failing to clean up its tempfiles" );
565
581
}
566
582
567
- static int remove_tempfile_installed ;
568
-
569
583
static void remove_tempfile (void )
570
584
{
571
585
int i ;
572
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 );
575
589
diff_temp [i ].name = NULL ;
576
590
}
577
591
}
578
592
579
- static void remove_tempfile_on_signal (int signo )
580
- {
581
- remove_tempfile ();
582
- sigchain_pop (signo );
583
- raise (signo );
584
- }
585
-
586
593
static void print_line_count (FILE * file , int count )
587
594
{
588
595
switch (count ) {
@@ -2817,8 +2824,7 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
2817
2824
strbuf_addstr (& template , "XXXXXX_" );
2818
2825
strbuf_addstr (& template , base );
2819
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 );
2822
2828
if (fd < 0 )
2823
2829
die_errno ("unable to create temp-file" );
2824
2830
if (convert_to_working_tree (path ,
@@ -2828,8 +2834,8 @@ static void prep_temp_blob(const char *path, struct diff_tempfile *temp,
2828
2834
}
2829
2835
if (write_in_full (fd , blob , size ) != size )
2830
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 ) ;
2833
2839
strcpy (temp -> hex , sha1_to_hex (sha1 ));
2834
2840
temp -> hex [40 ] = 0 ;
2835
2841
sprintf (temp -> mode , "%06o" , mode );
@@ -2854,12 +2860,6 @@ static struct diff_tempfile *prepare_temp_file(const char *name,
2854
2860
return temp ;
2855
2861
}
2856
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
2863
if (!S_ISGITLINK (one -> mode ) &&
2864
2864
(!one -> sha1_valid ||
2865
2865
reuse_worktree_file (name , one -> sha1 , 1 ))) {
0 commit comments