diff options
author | gerald <gerald@f5534014-38df-0310-8fa8-9805f1628bb7> | 2009-07-02 17:20:27 +0000 |
---|---|---|
committer | gerald <gerald@f5534014-38df-0310-8fa8-9805f1628bb7> | 2009-07-02 17:20:27 +0000 |
commit | 3cccdf86442ace51610b710e5a83534661636ba5 (patch) | |
tree | f0d1a8503bfebe75fcb95ebbfc905154f58b21de | |
parent | 5604295b1d794a5764a51562f033096cab752143 (diff) |
Pull mkstemp() into tempfile.c. That's the only place we use it now, and
it's arguably the only place we _should_ use it. Add create_tempdir() to
tempfile.c and use it to create a temp directory for IP maps. This
should fix bug 3530.
(This still doesn't work on IE 8 / Vista here. IE gives an access denied
error in OpenLayers.js, but this is a separate issue).
git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@28920 f5534014-38df-0310-8fa8-9805f1628bb7
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | Makefile.common | 2 | ||||
-rw-r--r-- | Makefile.nmake | 5 | ||||
-rw-r--r-- | config.h.win32 | 3 | ||||
-rw-r--r-- | configure.in | 9 | ||||
-rw-r--r-- | gtk/hostlist_table.c | 33 | ||||
-rw-r--r-- | mkstemp.c | 88 | ||||
-rw-r--r-- | mkstemp.h | 24 | ||||
-rw-r--r-- | tempfile.c | 146 | ||||
-rw-r--r-- | tempfile.h | 14 |
10 files changed, 177 insertions, 149 deletions
diff --git a/Makefile.am b/Makefile.am index f00404defb..ecb004d21f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -238,7 +238,7 @@ include Makefile.common # by wireshark and tshark and stuff needed only by one or the # other. wireshark_optional_objects = @GETOPT_O@ @STRERROR_O@ \ - @STRCASECMP_O@ @STRNCASECMP_O@ @MKSTEMP_O@ @STRPTIME_O@ + @STRCASECMP_O@ @STRNCASECMP_O@ @STRPTIME_O@ if ENABLE_STATIC wireshark_LDFLAGS = -Wl,-static -all-static diff --git a/Makefile.common b/Makefile.common index 8dea91fbcb..8e871139f1 100644 --- a/Makefile.common +++ b/Makefile.common @@ -125,7 +125,6 @@ EXTRA_wireshark_SOURCES = \ getopt.c \ inet_ntop.c \ inet_pton.c \ - mkstemp.c \ strerror.c \ strcasecmp.c \ strncasecmp.c \ @@ -135,7 +134,6 @@ EXTRA_wireshark_SOURCES = \ EXTRA_wireshark_INCLUDES = \ getopt.h \ inet_v6defs.h \ - mkstemp.h \ strerror.h \ strptime.h diff --git a/Makefile.nmake b/Makefile.nmake index 958a961091..ccfc26ad0a 100644 --- a/Makefile.nmake +++ b/Makefile.nmake @@ -76,7 +76,6 @@ EXTRA_OBJECTS = \ getopt.obj \ inet_ntop.obj \ inet_pton.obj \ - mkstemp.obj \ strptime.obj wireshark_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ @@ -329,10 +328,10 @@ randpkt.exe : $(randpkt_OBJECTS) getopt.obj mt.exe -nologo -manifest "randpkt.exe.manifest" -outputresource:randpkt.exe;1 !ENDIF -dumpcap.exe : $(LIBS_CHECK) config.h svnversion.h $(dumpcap_OBJECTS) getopt.obj inet_ntop.obj mkstemp.obj wsutil\libwsutil.lib wiretap\wiretap-$(WTAP_VERSION).lib image\dumpcap.res +dumpcap.exe : $(LIBS_CHECK) config.h svnversion.h $(dumpcap_OBJECTS) getopt.obj inet_ntop.obj wsutil\libwsutil.lib wiretap\wiretap-$(WTAP_VERSION).lib image\dumpcap.res @echo Linking $@ $(LINK) @<< - /OUT:dumpcap.exe $(conflags) $(conlibsdll) $(LDFLAGS) /SUBSYSTEM:console $(dumpcap_LIBS) $(dumpcap_OBJECTS) getopt.obj inet_ntop.obj mkstemp.obj image\dumpcap.res + /OUT:dumpcap.exe $(conflags) $(conlibsdll) $(LDFLAGS) /SUBSYSTEM:console $(dumpcap_LIBS) $(dumpcap_OBJECTS) getopt.obj inet_ntop.obj image\dumpcap.res << !IF $(MSC_VER_REQUIRED) >= 1400 mt.exe -nologo -manifest "dumpcap.exe.manifest" -outputresource:dumpcap.exe;1 diff --git a/config.h.win32 b/config.h.win32 index 5196d6e3ec..5b04308131 100644 --- a/config.h.win32 +++ b/config.h.win32 @@ -56,7 +56,8 @@ /* #undef NEED_STRERROR_H */ -#define NEED_MKSTEMP 1 +/* #undef HAVE_MKSTEMP */ +/* #undef HAVE_MKDTEMP */ @HAVE_LIBPCAP@ @HAVE_PCAP_BREAKLOOP@ diff --git a/configure.in b/configure.in index 396c732d64..dea2305f9a 100644 --- a/configure.in +++ b/configure.in @@ -1448,14 +1448,7 @@ fi AC_SUBST(STRNCASECMP_C) AC_SUBST(STRNCASECMP_O) -AC_CHECK_FUNC(mkstemp, MKSTEMP_O="", - MKSTEMP_O="mkstemp.o") -if test "$ac_cv_func_mkstemp" = no ; then - MKSTEMP_C="mkstemp.c" - MKSTEMP_O="mkstemp.o" -fi -AC_SUBST(MKSTEMP_C) -AC_SUBST(MKSTEMP_O) +AC_CHECK_FUNCS(mkstemp mkdtemp) # # XXX - if inet_aton isn't found, the build fails, with a complaint from diff --git a/gtk/hostlist_table.c b/gtk/hostlist_table.c index aac4237b95..5345424448 100644 --- a/gtk/hostlist_table.c +++ b/gtk/hostlist_table.c @@ -595,11 +595,9 @@ open_as_map_cb(GtkWindow *copy_bt, gpointer data _U_) FILE *out_file; gchar *file_uri; gboolean uri_open; - char *map_data_filename; - int temp_fd; + char *map_path, *map_data_filename; char *src_file_path; - char *temp_path; - GString *dst_file_path; + char *dst_file_path; gboolean hosts_written = FALSE; hostlist_table *hosts=g_object_get_data(G_OBJECT(copy_bt), HOST_PTR_KEY); @@ -644,14 +642,14 @@ open_as_map_cb(GtkWindow *copy_bt, gpointer data _U_) /* open the TSV output file */ /* XXX - add error handling */ - temp_fd = create_tempfile(&map_data_filename, "ipmap_"); - if(temp_fd == -1) { + if (! create_tempdir(&map_path, "Wireshark IP Map ")) { simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, - "Could not create temporary file %s: %s", - map_data_filename, strerror(errno)); + "Could not create temporary directory\n%s", + map_path); return; } - out_file = fdopen(temp_fd, "w"); + map_data_filename = g_strdup_printf("%s%cipmap.txt", map_path, G_DIR_SEPARATOR); + out_file = ws_fopen(map_data_filename, "w"); if(out_file == NULL) { open_failure_alert_box(map_data_filename, errno, TRUE); return; @@ -726,27 +724,22 @@ open_as_map_cb(GtkWindow *copy_bt, gpointer data _U_) } /* copy ipmap.html to temp dir */ - temp_path = g_strdup(map_data_filename); - get_dirname(temp_path); src_file_path = get_datafile_path("ipmap.html"); - dst_file_path = g_string_new(""); - g_string_printf(dst_file_path, "%s%cipmap.html", temp_path, G_DIR_SEPARATOR); - g_free(temp_path); + dst_file_path = g_strdup_printf("%s%cipmap.html", map_path, G_DIR_SEPARATOR); - if (!copy_file_binary_mode(src_file_path, dst_file_path->str)) { + if (!copy_file_binary_mode(src_file_path, dst_file_path)) { g_free(src_file_path); - g_string_free(dst_file_path, TRUE); + g_free(dst_file_path); return; } g_free(src_file_path); /* open the webbrowser */ - g_string_append_printf(dst_file_path, "#%s", get_basename(map_data_filename)); - file_uri = filename2uri(dst_file_path->str); - g_string_free(dst_file_path, TRUE); + file_uri = filename2uri(dst_file_path); + g_free(dst_file_path); uri_open = browser_open_url (file_uri); if(!uri_open) { - simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't open the file: \"%s\" in the webbrowser", file_uri); + simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "Couldn't open the file: \"%s\" in your web browser", file_uri); g_free(file_uri); return; } diff --git a/mkstemp.c b/mkstemp.c deleted file mode 100644 index 93e34a6cbf..0000000000 --- a/mkstemp.c +++ /dev/null @@ -1,88 +0,0 @@ -/* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include <stdlib.h> -#include <string.h> -#include <errno.h> -#include <stdio.h> - -#ifdef HAVE_FCNTL_H -#include <fcntl.h> -#endif - -#ifdef HAVE_UNISTD_H -#include <unistd.h> -#endif - -#ifdef _WIN32 -#include <process.h> /* For spawning child process */ -#endif - -#include <glib.h> - -#include <wsutil/file_util.h> - -#ifndef __set_errno -#define __set_errno(x) errno=(x) -#endif - -/* Generate a unique temporary file name from TEMPLATE. - The last six characters of TEMPLATE must be "XXXXXX"; - they are replaced with a string that makes the filename unique. - Returns a file descriptor open on the file for reading and writing. */ -int -mkstemp (template) - char *template; -{ - static const char letters[] - = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - size_t len; - size_t i; - - len = strlen (template); - if (len < 6 || strcmp (&template[len - 6], "XXXXXX")) - { - __set_errno (EINVAL); - return -1; - } - - if (g_snprintf (&template[len - 5], 6, "%.5u", - (unsigned int) getpid () % 100000) != 5) - /* Inconceivable lossage. */ - return -1; - - for (i = 0; i < sizeof (letters); ++i) - { - int fd; - - template[len - 6] = letters[i]; - - fd = ws_open (template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600); - if (fd >= 0) - return fd; - } - - /* We return the null string if we can't find a unique file name. */ - - template[0] = '\0'; - return -1; -} diff --git a/mkstemp.h b/mkstemp.h deleted file mode 100644 index be2cae0f79..0000000000 --- a/mkstemp.h +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - - -/* Generate a unique temporary file name from TEMPLATE. - The last six characters of TEMPLATE must be "XXXXXX"; - they are replaced with a string that makes the filename unique. - Returns a file descriptor open on the file for reading and writing. */ -int mkstemp (char *template); diff --git a/tempfile.c b/tempfile.c index 7b3ede6917..1a292b5fc7 100644 --- a/tempfile.c +++ b/tempfile.c @@ -33,6 +33,10 @@ #include <stdio.h> #include <errno.h> +#ifdef HAVE_FCNTL_H +#include <fcntl.h> +#endif + #ifdef HAVE_UNISTD_H #include <unistd.h> #endif @@ -41,10 +45,108 @@ #include <windows.h> #endif +#ifdef _WIN32 +#include <process.h> /* For getpid() */ +#endif + #include "tempfile.h" -#include "mkstemp.h" #include <wsutil/file_util.h> +#ifndef __set_errno +#define __set_errno(x) errno=(x) +#endif + +#ifndef HAVE_MKSTEMP +/* Generate a unique temporary file name from TEMPLATE. + The last six characters of TEMPLATE must be "XXXXXX"; + they are replaced with a string that makes the filename unique. + Returns a file descriptor open on the file for reading and writing. */ +static int +mkstemp (template) + char *template; +{ + static const char letters[] + = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + size_t len; + size_t i; + + len = strlen (template); + if (len < 6 || strcmp (&template[len - 6], "XXXXXX")) + { + __set_errno (EINVAL); + return -1; + } + + if (g_snprintf (&template[len - 5], 6, "%.5u", + (unsigned int) getpid () % 100000) != 5) + /* Inconceivable lossage. */ + return -1; + + for (i = 0; i < sizeof (letters); ++i) + { + int fd; + + template[len - 6] = letters[i]; + + fd = ws_open (template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600); + if (fd >= 0) + return fd; + } + + /* We return the null string if we can't find a unique file name. */ + + template[0] = '\0'; + return -1; +} + +#endif /* HAVE_MKSTEMP */ + +#ifndef HAVE_MKDTEMP +/* Generate a unique temporary directory name from TEMPLATE. + The last six characters of TEMPLATE must be "XXXXXX"; + they are replaced with a string that makes the filename unique. + Returns 0 on success or -1 on error (from mkdir(2)). */ +char * +mkdtemp (template) + char *template; +{ + static const char letters[] + = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + size_t len; + size_t i; + + len = strlen (template); + if (len < 6 || strcmp (&template[len - 6], "XXXXXX")) + { + __set_errno (EINVAL); + return NULL; + } + + if (g_snprintf (&template[len - 5], 6, "%.5u", + (unsigned int) getpid () % 100000) != 5) + /* Inconceivable lossage. */ + return NULL; + + for (i = 0; i < sizeof (letters); ++i) + { + int ret; + + template[len - 6] = letters[i]; + + ret = ws_mkdir(template, 0700); + if (ret >= 0) + return template; + } + + /* We return the null string if we can't find a unique file name. */ + + template[0] = '\0'; + return NULL; +} + +#endif /* HAVE_MKDTEMP */ + + #define INITIAL_PATH_SIZE 128 #define TMP_FILE_SUFFIX "XXXXXXXXXX" @@ -100,3 +202,45 @@ create_tempfile(char **namebuf, const char *pfx) umask(old_umask); return fd; } + +/** + * Create a directory with the given prefix (e.g. "wireshark"). The path + * is created using g_get_tmp_dir and mkdtemp. + * + * @param pfx A prefix for the temporary directory. + * @return The temporary directory path on success, or NULL on failure. + * Must NOT be freed. + */ +const char * +create_tempdir(char **namebuf, const char *pfx) +{ + static char *td_path[3]; + static int td_path_len[3]; + static int idx; + const char *tmp_dir; + + idx = (idx + 1) % 3; + + /* + * Allocate the buffer if it's not already allocated. + */ + if (td_path[idx] == NULL) { + td_path_len[idx] = INITIAL_PATH_SIZE; + td_path[idx] = g_malloc(td_path_len[idx]); + } + + /* + * We can't use get_tempfile_path here because we're called from dumpcap.c. + */ + tmp_dir = g_get_tmp_dir(); + + while (g_snprintf(td_path[idx], td_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > td_path_len[idx]) { + td_path_len[idx] *= 2; + td_path[idx] = g_realloc(td_path[idx], td_path_len[idx]); + } + + if (namebuf) { + *namebuf = td_path[idx]; + } + return mkdtemp(td_path[idx]); +} diff --git a/tempfile.h b/tempfile.h index 95a03794a6..2a7fc5148c 100644 --- a/tempfile.h +++ b/tempfile.h @@ -39,12 +39,24 @@ extern "C" { * is created using g_get_tmp_dir and mkstemp. * * @param namebuf If not NULL, receives the full path of the temp file. - * Should NOT be freed. + * Must NOT be freed. * @param pfx A prefix for the temporary file. * @return The file descriptor of the new tempfile, from mkstemp(). */ int create_tempfile(char **namebuf, const char *pfx); +/** + * Create a directory with the given prefix (e.g. "wireshark"). The path + * is created using g_get_tmp_dir and mkdtemp. + * + * @param namebuf If not NULL, receives the full path of the temp directory. + * Must NOT be freed. + * @param pfx A prefix for the temporary directory. + * @return The temporary directory path on success, or NULL on failure. + * Must NOT be freed. + */ +const char *create_tempdir(char **namebuf, const char *pfx); + #ifdef __cplusplus } #endif /* __cplusplus */ |