tools: Add a common helper to cleanup temp files.

This commit is contained in:
Alexandre Julliard 2023-01-25 11:19:55 +01:00
parent ecb651c01f
commit ca398e2762
10 changed files with 57 additions and 98 deletions

View file

@ -325,6 +325,7 @@ static inline char *replace_extension( const char *name, const char *old_ext, co
/* temp files management */
extern const char *temp_dir;
extern struct strarray temp_files;
static inline char *make_temp_dir(void)
{
@ -351,10 +352,11 @@ static inline char *make_temp_dir(void)
exit(1);
}
static inline int make_temp_file( const char *prefix, const char *suffix, char **name )
static inline char *make_temp_file( const char *prefix, const char *suffix )
{
static unsigned int value;
int fd, count;
char *name;
if (!temp_dir) temp_dir = make_temp_dir();
if (!suffix) suffix = "";
@ -363,15 +365,28 @@ static inline int make_temp_file( const char *prefix, const char *suffix, char *
for (count = 0; count < 0x8000; count++)
{
*name = strmake( "%s/%s-%08x%s", temp_dir, prefix, value++, suffix );
fd = open( *name, O_RDWR | O_CREAT | O_EXCL, 0600 );
if (fd >= 0) return fd;
free( *name );
name = strmake( "%s/%s-%08x%s", temp_dir, prefix, value++, suffix );
fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 );
if (fd >= 0)
{
strarray_add( &temp_files, name );
close( fd );
return name;
}
free( name );
}
fprintf( stderr, "failed to create temp file for %s%s in %s\n", prefix, suffix, temp_dir );
exit(1);
}
static inline void remove_temp_files(void)
{
unsigned int i;
for (i = 0; i < temp_files.count; i++) if (temp_files.str[i]) unlink( temp_files.str[i] );
if (temp_dir) rmdir( temp_dir );
}
static inline void *read_file( const char *name, size_t *size )
{

View file

@ -509,11 +509,6 @@ void pop_import(void)
fclose(yyin);
yy_delete_buffer( YY_CURRENT_BUFFER );
yy_switch_to_buffer( import_stack[ptr].state );
if (temp_name) {
unlink(temp_name);
free(temp_name);
}
temp_name = import_stack[ptr].temp_name;
input_name = import_stack[ptr].input_name;
line_number = import_stack[ptr].line_number;
import_stack_ptr--;
@ -530,7 +525,7 @@ int do_import(char *fname)
char *path, *name;
struct imports *import;
int ptr = import_stack_ptr;
int ret, fd;
int ret;
import = first_import;
while (import && strcmp(import->name, fname))
@ -552,24 +547,22 @@ int do_import(char *fname)
if (import_stack_ptr == MAX_IMPORT_DEPTH)
error_loc("Exceeded max import depth\n");
import_stack[ptr].temp_name = temp_name;
import_stack[ptr].input_name = input_name;
import_stack[ptr].line_number = line_number;
import_stack_ptr++;
input_name = path;
line_number = 1;
fd = make_temp_file( "widl-pp", NULL, &name );
temp_name = name;
if (!(f = fdopen(fd, "wt")))
name = make_temp_file( "widl-pp", NULL );
if (!(f = fopen(name, "wt")))
error("Could not open fd %s for writing\n", name);
ret = wpp_parse( path, f );
fclose( f );
if (ret) exit(1);
if((f = fopen(temp_name, "r")) == NULL)
error_loc("Unable to open %s\n", temp_name);
if((f = fopen(name, "r")) == NULL)
error_loc("Unable to open %s\n", name);
import_stack[ptr].state = YY_CURRENT_BUFFER;
yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));
@ -587,7 +580,7 @@ void abort_import(void)
static void switch_to_acf(void)
{
int ptr = import_stack_ptr;
int ret, fd;
int ret;
char *name;
FILE *f;
@ -597,17 +590,16 @@ static void switch_to_acf(void)
acf_name = NULL;
line_number = 1;
fd = make_temp_file( "widl-acf", NULL, &name );
temp_name = name;
if (!(f = fdopen(fd, "wt")))
name = make_temp_file( "widl-acf", NULL );
if (!(f = fopen(name, "wt")))
error("Could not open fd %s for writing\n", name);
ret = wpp_parse(input_name, f);
fclose(f);
if (ret) exit(1);
if((f = fopen(temp_name, "r")) == NULL)
error_loc("Unable to open %s\n", temp_name);
if((f = fopen(name, "r")) == NULL)
error_loc("Unable to open %s\n", name);
import_stack[ptr].state = YY_CURRENT_BUFFER;
yy_switch_to_buffer(yy_create_buffer(f, YY_BUF_SIZE));

View file

@ -131,7 +131,7 @@ char *server_token;
char *regscript_name;
char *regscript_token;
static char *idfile_name;
char *temp_name;
struct strarray temp_files = { 0 };
const char *temp_dir = NULL;
const char *prefix_client = "";
const char *prefix_server = "";
@ -702,6 +702,7 @@ int main(int argc,char *argv[])
int i;
int ret = 0;
struct strarray files;
char *input;
signal( SIGTERM, exit_on_signal );
signal( SIGINT, exit_on_signal );
@ -839,6 +840,7 @@ int main(int argc,char *argv[])
wpp_add_cmdline_define("_WIN32=1");
atexit(rm_tempfile);
input = input_name;
if (!no_preprocess)
{
chat("Starting preprocess\n");
@ -846,16 +848,14 @@ int main(int argc,char *argv[])
if (!preprocess_only)
{
FILE *output;
int fd;
char *name;
char *name = make_temp_file( header_name, NULL );
fd = make_temp_file( header_name, NULL, &name );
temp_name = name;
if (!(output = fdopen(fd, "wt")))
if (!(output = fopen(name, "wt")))
error("Could not open fd %s for writing\n", name);
ret = wpp_parse( input_name, output );
fclose( output );
input = name;
}
else
{
@ -864,16 +864,11 @@ int main(int argc,char *argv[])
if(ret) exit(1);
if(preprocess_only) exit(0);
if(!(parser_in = fopen(temp_name, "r"))) {
fprintf(stderr, "Could not open %s for input\n", temp_name);
return 1;
}
}
else {
if(!(parser_in = fopen(input_name, "r"))) {
fprintf(stderr, "Could not open %s for input\n", input_name);
return 1;
}
if(!(parser_in = fopen(input, "r"))) {
fprintf(stderr, "Could not open %s for input\n", input);
return 1;
}
header_token = make_token(header_name);
@ -897,8 +892,6 @@ int main(int argc,char *argv[])
static void rm_tempfile(void)
{
abort_import();
if(temp_name)
unlink(temp_name);
if (do_header)
unlink(header_name);
if (local_stubs_name)
@ -915,6 +908,5 @@ static void rm_tempfile(void)
unlink(proxy_name);
if (do_typelib)
unlink(typelib_name);
if (temp_dir)
rmdir(temp_dir);
remove_temp_files();
}

View file

@ -260,8 +260,6 @@ extern struct strarray find_link_tool(void);
extern struct strarray get_as_command(void);
extern struct strarray get_ld_command(void);
extern const char *get_nm_command(void);
extern void cleanup_tmp_files(void);
extern char *get_temp_file_name( const char *prefix, const char *suffix );
extern void output_standard_file_header(void);
extern FILE *open_input_file( const char *srcdir, const char *name );
extern void close_input_file( FILE *file );

View file

@ -627,7 +627,7 @@ static char *create_undef_symbols_file( DLLSPEC *spec )
output_gnu_stack_note();
fclose( output_file );
obj_file = get_temp_file_name( output_file_name, ".o" );
obj_file = make_temp_file( output_file_name, ".o" );
assemble_file( as_file, obj_file );
return obj_file;
}
@ -640,7 +640,7 @@ static const char *ldcombine_files( DLLSPEC *spec, struct strarray files )
struct strarray args = get_ld_command();
undef_file = create_undef_symbols_file( spec );
ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
ld_tmp_file = make_temp_file( output_file_name, ".o" );
strarray_add( &args, "-r" );
strarray_add( &args, "-o" );
@ -1566,7 +1566,7 @@ static void assemble_files( const char *prefix )
for (i = 0; i < as_files.count; i++)
{
char *obj = get_temp_file_name( prefix, ".o" );
char *obj = make_temp_file( prefix, ".o" );
assemble_file( as_files.str[i], obj );
as_files.str[i] = obj;
}

View file

@ -182,6 +182,7 @@ static void set_target( const char *name )
static void cleanup(void)
{
if (output_file_name) unlink( output_file_name );
if (!save_temps) remove_temp_files();
}
/* clean things up when aborting on a signal */
@ -631,7 +632,6 @@ int main(int argc, char **argv)
files = parse_options( argc, argv, short_options, long_options, 0, option_callback );
atexit( cleanup ); /* make sure we remove the output file on exit */
if (!save_temps) atexit( cleanup_tmp_files );
if (spec->file_name && !strchr( spec->file_name, '.' ))
strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );

View file

@ -671,7 +671,7 @@ void output_res_o_file( DLLSPEC *spec )
return;
}
res_file = get_temp_file_name( output_file_name, ".res" );
res_file = make_temp_file( output_file_name, ".res" );
flush_output_buffer( res_file );
args = find_tool( "windres", NULL );

View file

@ -30,18 +30,9 @@
#include "build.h"
const char *temp_dir = NULL;
static struct strarray tmp_files;
struct strarray temp_files = { 0 };
static const char *output_file_source_name;
/* atexit handler to clean tmp files */
void cleanup_tmp_files(void)
{
unsigned int i;
for (i = 0; i < tmp_files.count; i++) if (tmp_files.str[i]) unlink( tmp_files.str[i] );
if (temp_dir) rmdir( temp_dir );
}
char *strupper(char *s)
{
char *p;
@ -194,7 +185,7 @@ void spawn( struct strarray args )
static const char *find_clang_tool( struct strarray clang, const char *tool )
{
const char *out = get_temp_file_name( "print_tool", ".out" );
const char *out = make_temp_file( "print_tool", ".out" );
struct strarray args = empty_strarray;
int sout = -1;
char *path, *p;
@ -406,18 +397,6 @@ const char *get_nm_command(void)
return nm_command.str[0];
}
/* get a name for a temp file, automatically cleaned up on exit */
char *get_temp_file_name( const char *prefix, const char *suffix )
{
char *name;
int fd;
if (prefix) prefix = get_basename_noext( prefix );
fd = make_temp_file( prefix, suffix, &name );
close( fd );
strarray_add( &tmp_files, name );
return name;
}
/*******************************************************************
* buffer management
@ -592,7 +571,7 @@ void close_output_file(void)
*/
char *open_temp_output_file( const char *suffix )
{
char *tmp_file = get_temp_file_name( output_file_name, suffix );
char *tmp_file = make_temp_file( output_file_name, suffix );
if (!(output_file = fopen( tmp_file, "w" )))
fatal_error( "Unable to create output file '%s'\n", tmp_file );
return tmp_file;

View file

@ -145,7 +145,7 @@ static const char *output_debug_file;
static const char *output_implib;
static int keep_generated = 0;
const char *temp_dir = NULL;
static struct strarray tmp_files;
struct strarray temp_files = { 0 };
#ifdef HAVE_SIGSET_T
static sigset_t signal_mask;
#endif
@ -214,13 +214,7 @@ static void cleanup_output_files(void)
static void clean_temp_files(void)
{
unsigned int i;
if (keep_generated) return;
for (i = 0; i < tmp_files.count; i++)
unlink(tmp_files.str[i]);
if (temp_dir) rmdir( temp_dir );
if (!keep_generated) remove_temp_files();
}
/* clean things up when aborting on a signal */
@ -231,7 +225,6 @@ static void exit_on_signal( int sig )
static char* get_temp_file(const char* prefix, const char* suffix)
{
int fd;
char *tmp;
#ifdef HAVE_SIGPROCMASK
@ -239,9 +232,7 @@ static char* get_temp_file(const char* prefix, const char* suffix)
/* block signals while manipulating the temp files list */
sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
#endif
fd = make_temp_file( prefix, suffix, &tmp );
close( fd );
strarray_add(&tmp_files, tmp);
tmp = make_temp_file( prefix, suffix );
#ifdef HAVE_SIGPROCMASK
sigprocmask( SIG_SETMASK, &old_set, NULL );
#endif
@ -421,7 +412,6 @@ static struct strarray get_link_args( struct options *opts, const char *output_n
create_file( mapfile, 0644, "text = A%s;\ndata = A%s;\n", align, align );
strarray_add( &flags, strmake("-Wl,-M,%s", mapfile) );
strarray_add( &tmp_files, mapfile );
}
break;

View file

@ -130,9 +130,9 @@ int check_utf8 = 1; /* whether to check for valid utf8 */
static char *output_name; /* The name given by the -o option */
const char *input_name = NULL; /* The name given on the command-line */
static char *temp_name = NULL; /* Temporary file for preprocess pipe */
static struct strarray input_files;
const char *temp_dir = NULL;
struct strarray temp_files = { 0 };
static int stdinc = 1;
static int po_mode;
@ -233,7 +233,7 @@ static int load_file( const char *input_name, const char *output_name )
if(!no_preprocess)
{
FILE *output;
int ret, fd;
int ret;
char *name;
/*
@ -257,9 +257,8 @@ static int load_file( const char *input_name, const char *output_name )
exit(0);
}
fd = make_temp_file( output_name, "", &name );
temp_name = name;
if (!(output = fdopen(fd, "wt")))
name = make_temp_file( output_name, "" );
if (!(output = fopen(name, "wt")))
error("Could not open fd %s for writing\n", name);
ret = wpp_parse( input_name, output );
@ -281,11 +280,6 @@ static int load_file( const char *input_name, const char *output_name )
ret = parser_parse();
fclose(parser_in);
parser_lex_destroy();
if (temp_name)
{
unlink( temp_name );
temp_name = NULL;
}
return ret;
}
@ -501,6 +495,5 @@ int main(int argc,char *argv[])
static void cleanup_files(void)
{
if (output_name) unlink(output_name);
if (temp_name) unlink(temp_name);
if (temp_dir) rmdir(temp_dir);
remove_temp_files();
}