Have "cf_merge_files()" take a pointer-to-pointer-to-char as the output
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 17 Feb 2005 03:05:54 +0000 (03:05 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Thu, 17 Feb 2005 03:05:54 +0000 (03:05 +0000)
file name argument; if the pointed-to pointer is null, it opens a
temporary file, and sets that pointer to a mallocated copy of the
pathname of the temporary file.  It no longer needs a file descriptor as
an argument.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@13419 f5534014-38df-0310-8fa8-9805f1628bb7

file.c
file.h
gtk/drag_and_drop.c
gtk/file_dlg.c

diff --git a/file.c b/file.c
index 09d074038f9238831fa6993a79a2b873cafe917f..43c3990322d0bf68318929f50b12a47a7d40d7d7 100644 (file)
--- a/file.c
+++ b/file.c
@@ -939,11 +939,14 @@ read_packet(capture_file *cf, long offset)
 }
 
 cf_status_t
-cf_merge_files(const char *out_filename, int out_fd, int in_file_count,
+cf_merge_files(char **out_filenamep, int in_file_count,
                char *const *in_filenames, int file_type, gboolean do_append)
 {
   merge_in_file_t  *in_files;
   wtap             *wth;
+  char             *out_filename;
+  char              tmpname[128+1];
+  int               out_fd;
   wtap_dumper      *pdh;
   int               open_err, read_err, write_err, close_err;
   gchar            *err_info;
@@ -977,6 +980,26 @@ cf_merge_files(const char *out_filename, int out_fd, int in_file_count,
     return CF_ERROR;
   }
 
+  if (*out_filenamep != NULL) {
+    out_filename = *out_filenamep;
+    out_fd = open(out_filename, O_CREAT|O_TRUNC|O_BINARY, 0600);
+    if (out_fd == -1)
+      open_err = errno;
+  } else {
+    out_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
+    if (out_fd == -1)
+      open_err = errno;
+    out_filename = g_strdup(tmpname);
+    *out_filenamep = out_filename;
+  }
+  if (out_fd == -1) {
+    err_info = NULL;
+    merge_close_in_files(in_file_count, in_files);
+    free(in_files);
+    cf_open_failure_alert_box(out_filename, open_err, NULL, TRUE, file_type);
+    return CF_ERROR;
+  }
+
   pdh = wtap_dump_fdopen(out_fd, file_type,
       merge_select_frame_type(in_file_count, in_files),
       merge_max_snapshot_length(in_file_count, in_files), &open_err);
diff --git a/file.h b/file.h
index bb56e31585fa037e44c0c427574f8eeb09719710..5c10c314f153a59731218d16eda5d8d61e853612 100644 (file)
--- a/file.h
+++ b/file.h
@@ -406,8 +406,9 @@ char *cf_read_error_message(int err, const gchar *err_info);
  * Merge two (or more) capture files into one.
  * @todo is this the right place for this function? It doesn't have to do a lot with capture_file.
  *
- * @param out_filename output filename
- * @param out_fd output file descriptor
+ * @param out_filename pointer to output filename; if output filename is
+ * NULL, a temporary file name is generated and *out_filename is set
+ * to point to the generated file name
  * @param in_file_count the number of input files to merge
  * @param in_filnames array of input filenames
  * @param file_type the output filetype
@@ -415,8 +416,7 @@ char *cf_read_error_message(int err, const gchar *err_info);
  * @return one of cf_status_t
  */
 cf_status_t
-cf_merge_files(const char *out_filename, int out_fd, int in_file_count,
+cf_merge_files(char **out_filename, int in_file_count,
                char *const *in_filenames, int file_type, gboolean do_append);
 
-
 #endif /* file.h */
index fbdbafb897ed91967f4d367e54201295c4999e53..4a124c706b236b9fa4be330f51084586fe75d82a 100644 (file)
@@ -134,20 +134,18 @@ dnd_uri2filename(gchar *cf_name)
 static void
 dnd_merge_files(int in_file_count, char **in_filenames)
 {
-    int out_fd;
+    char *tmpname;
     gboolean merge_ok;
     int err;
-    char      tmpname[128+1];
-
-
-    out_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
 
     /* merge the files in chonological order */
-    merge_ok = cf_merge_files(tmpname, out_fd, in_file_count, in_filenames,
+    tmpname = NULL;
+    merge_ok = cf_merge_files(&tmpname, in_file_count, in_filenames,
                               WTAP_FILE_PCAP, FALSE);
 
     if (!merge_ok) {
         /* merge failed */
+        g_free(tmpname);
        return;
     }
 
@@ -159,8 +157,10 @@ dnd_merge_files(int in_file_count, char **in_filenames)
           just leave it around so that the user can, after they
           dismiss the alert box popped up for the open error,
           try again. */
+       g_free(tmpname);
        return;
     }
+    g_free(tmpname);
 
     switch (cf_read(&cfile)) {
 
index 92d16ed651a8b8736b4d4323e26e91a7542ebaba..40f95813476235bb0e7764112fec53a02a7eb564 100644 (file)
@@ -1000,8 +1000,7 @@ file_merge_ok_cb(GtkWidget *w, gpointer fs) {
   int          err;
   cf_status_t  merge_status;
   char        *in_filenames[2];
-  int          out_fd;
-  char         tmpname[128+1];
+  char        *tmpname;
 
 #if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION >= 4) || GTK_MAJOR_VERSION > 2
   cf_name = g_strdup(gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fs)));
@@ -1027,30 +1026,28 @@ file_merge_ok_cb(GtkWidget *w, gpointer fs) {
        return;
   }
 
-  out_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
-
   /* merge or append the two files */
   rb = OBJECT_GET_DATA(w, E_MERGE_CHRONO_KEY);
+  tmpname = NULL;
   if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON (rb))) {
       /* chronological order */
       in_filenames[0] = cfile.filename;
       in_filenames[1] = cf_name;
-      merge_status = cf_merge_files(tmpname, out_fd, 2, in_filenames,
-                                filetype, FALSE);
+      merge_status = cf_merge_files(&tmpname, 2, in_filenames, filetype, FALSE);
   } else {
       rb = OBJECT_GET_DATA(w, E_MERGE_PREPEND_KEY);
       if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON (rb))) {
           /* prepend file */
           in_filenames[0] = cfile.filename;
           in_filenames[1] = cf_name;
-          merge_status = cf_merge_files(tmpname, out_fd, 2, in_filenames,
-                                    filetype, TRUE);
+          merge_status = cf_merge_files(&tmpname, 2, in_filenames, filetype,
+                                        TRUE);
       } else {
           /* append file */
           in_filenames[0] = cf_name;
           in_filenames[1] = cfile.filename;
-          merge_status = cf_merge_files(tmpname, out_fd, 2, in_filenames,
-                                    filetype, TRUE);
+          merge_status = cf_merge_files(&tmpname, 2, in_filenames, filetype,
+                                        TRUE);
       }
   }
 
@@ -1059,6 +1056,7 @@ file_merge_ok_cb(GtkWidget *w, gpointer fs) {
   if (merge_status != CF_OK) {
     if (rfcode != NULL)
       dfilter_free(rfcode);
+    g_free(tmpname);
     return;
   }
 
@@ -1075,8 +1073,10 @@ file_merge_ok_cb(GtkWidget *w, gpointer fs) {
        try again. */
     if (rfcode != NULL)
       dfilter_free(rfcode);
+    g_free(tmpname);
     return;
   }
+  g_free(tmpname);
 
   /* Attach the new read filter to "cf" ("cf_open()" succeeded, so
      it closed the previous capture file, and thus destroyed any