Module: wine Branch: master Commit: bdc40b4b60d0f8a8532e920547de7c8fa2975474 URL: https://gitlab.winehq.org/wine/wine/-/commit/bdc40b4b60d0f8a8532e920547de7c8...
Author: Alexandre Julliard julliard@winehq.org Date: Thu Jan 12 13:00:55 2023 +0100
tools: Create a temporary directory to store temp files.
---
tools/tools.h | 46 +++++++++++++++++++++++++++++++++------------- tools/widl/widl.c | 3 +++ tools/winebuild/utils.c | 2 ++ tools/winegcc/winegcc.c | 2 ++ tools/wrc/wrc.c | 2 ++ 5 files changed, 42 insertions(+), 13 deletions(-)
diff --git a/tools/tools.h b/tools/tools.h index 1e86e38356d..456633e4277 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -319,33 +319,53 @@ static inline char *replace_extension( const char *name, const char *old_ext, co return strmake( "%.*s%s", name_len, name, new_ext ); }
+/* temp files management */
-static inline int make_temp_file( const char *prefix, const char *suffix, char **name ) +extern const char *temp_dir; + +static inline char *make_temp_dir(void) { - static unsigned int value; - int fd, count; + unsigned int value = time(NULL) + getpid(); + int count; + char *name; const char *tmpdir = NULL;
- if (!prefix) prefix = "tmp"; - if (!suffix) suffix = ""; - value += time(NULL) + getpid(); - for (count = 0; count < 0x8000; count++) { if (tmpdir) - *name = strmake( "%s/%s-%08x%s", tmpdir, prefix, value, suffix ); + name = strmake( "%s/tmp%08x", tmpdir, value ); else - *name = strmake( "%s-%08x%s", prefix, value, suffix ); - fd = open( *name, O_RDWR | O_CREAT | O_EXCL, 0600 ); - if (fd >= 0) return fd; + name = strmake( "tmp%08x", value ); + if (!mkdir( name, 0700 )) return name; value += 7777; - if (errno == EACCES && !tmpdir && !strchr( prefix, '/' )) + if (errno == EACCES && !tmpdir) { if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp"; } + free( name ); + } + fprintf( stderr, "failed to create directory for temp files\n" ); + exit(1); +} + +static inline int make_temp_file( const char *prefix, const char *suffix, char **name ) +{ + static unsigned int value; + int fd, count; + + if (!temp_dir) temp_dir = make_temp_dir(); + if (!suffix) suffix = ""; + if (!prefix) prefix = "tmp"; + else prefix = get_basename_noext( prefix ); + + 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 ); } - fprintf( stderr, "failed to create temp file for %s%s\n", prefix, suffix ); + fprintf( stderr, "failed to create temp file for %s%s in %s\n", prefix, suffix, temp_dir ); exit(1); }
diff --git a/tools/widl/widl.c b/tools/widl/widl.c index 4b26e3a0991..79d895b52bf 100644 --- a/tools/widl/widl.c +++ b/tools/widl/widl.c @@ -135,6 +135,7 @@ char *regscript_name; char *regscript_token; static char *idfile_name; char *temp_name; +const char *temp_dir = NULL; const char *prefix_client = ""; const char *prefix_server = ""; static const char *includedir; @@ -931,4 +932,6 @@ static void rm_tempfile(void) unlink(proxy_name); if (do_typelib) unlink(typelib_name); + if (temp_dir) + rmdir(temp_dir); } diff --git a/tools/winebuild/utils.c b/tools/winebuild/utils.c index 21de209f65c..22eb60236b0 100644 --- a/tools/winebuild/utils.c +++ b/tools/winebuild/utils.c @@ -29,6 +29,7 @@
#include "build.h"
+const char *temp_dir = NULL; static struct strarray tmp_files; static const char *output_file_source_name;
@@ -37,6 +38,7 @@ 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 ); }
diff --git a/tools/winegcc/winegcc.c b/tools/winegcc/winegcc.c index 142ab81de12..2e656f7ea83 100644 --- a/tools/winegcc/winegcc.c +++ b/tools/winegcc/winegcc.c @@ -147,6 +147,7 @@ static const char *output_file_name; 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; #ifdef HAVE_SIGSET_T static sigset_t signal_mask; @@ -222,6 +223,7 @@ static void clean_temp_files(void)
for (i = 0; i < tmp_files.count; i++) unlink(tmp_files.str[i]); + if (temp_dir) rmdir( temp_dir ); }
/* clean things up when aborting on a signal */ diff --git a/tools/wrc/wrc.c b/tools/wrc/wrc.c index 2448fff11eb..a1e59c3d2b3 100644 --- a/tools/wrc/wrc.c +++ b/tools/wrc/wrc.c @@ -135,6 +135,7 @@ 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;
static int stdinc = 1; static int po_mode; @@ -519,4 +520,5 @@ static void cleanup_files(void) { if (output_name) unlink(output_name); if (temp_name) unlink(temp_name); + if (temp_dir) rmdir(temp_dir); }