Pull mkstemp() into tempfile.c. That's the only place we use it now, and
authorgerald <gerald@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 2 Jul 2009 17:20:27 +0000 (17:20 +0000)
committergerald <gerald@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 2 Jul 2009 17:20:27 +0000 (17:20 +0000)
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

Makefile.am
Makefile.common
Makefile.nmake
config.h.win32
configure.in
gtk/hostlist_table.c
mkstemp.c [deleted file]
mkstemp.h [deleted file]
tempfile.c
tempfile.h

index f00404defb3e45432770a444b1bde90c8ee77541..ecb004d21f95e9acedf2dc4b0d601d27e950e7e0 100644 (file)
@@ -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
index 8dea91fbcb05efb98dac58d6a75e9c3138bcd246..8e871139f116dd8b631d6a4d38a6103e17298b8a 100644 (file)
@@ -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
 
index 958a961091e6a147ebe8e4e2cae2d4fdaf7d3408..ccfc26ad0a8b4d1caa1e7ba73935d48ebdf55013 100644 (file)
@@ -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
index 5196d6e3ec469f1ade41def7f733a5d01d62095e..5b04308131c467c892c8d87c6d3bb4ee62b4f847 100644 (file)
@@ -56,7 +56,8 @@
 
 /* #undef NEED_STRERROR_H */
 
-#define NEED_MKSTEMP 1
+/* #undef HAVE_MKSTEMP */
+/* #undef HAVE_MKDTEMP */
 
 @HAVE_LIBPCAP@
 @HAVE_PCAP_BREAKLOOP@
index 396c732d64fb9a9b66c398b0b0bcb2638dfeefe4..dea2305f9a05cd3622180c1199b2f8eaf89c7d47 100644 (file)
@@ -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
index aac4237b95108d5e040f1cb52fa8e7b4793fc69b..53454244484a21fc3ea2d1aa3233a9d287be3215 100644 (file)
@@ -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 (file)
index 93e34a6..0000000
--- 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 (file)
index be2cae0..0000000
--- 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);
index 7b3ede6917d7afd4a25af2b0655809c24feef5aa..1a292b5fc73d472dd6a8309dcd6b782626110174 100644 (file)
 #include <stdio.h>
 #include <errno.h>
 
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #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]);
+}
index 95a03794a6a452e9495161a2c70db408feaf42cf..2a7fc5148c5d7fb2353b977fa30d100db6596de0 100644 (file)
@@ -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 */