Clean up indentation a bit.
authorGuy Harris <guy@alum.mit.edu>
Wed, 1 Mar 2006 01:46:52 +0000 (01:46 -0000)
committerGuy Harris <guy@alum.mit.edu>
Wed, 1 Mar 2006 01:46:52 +0000 (01:46 -0000)
In "capture_input_new_file()", don't call the callbacks unless we
succeed in opening the new file.  Have "capture_info_new_file()" return
a success/failure indication.

Improve the message logged when we fail to open the new file if we're
only opening it for the quick packet counts.  We really should put up an
alert box and give up on the capture at that point.

svn path=/trunk/; revision=17437

capture.c
capture_info.c
capture_info.h

index b5556ce84cccccba34dc1449bbb1f05387610662..18158aec4667242eb70c4ed1dcb8bc1e801fcb73 100644 (file)
--- a/capture.c
+++ b/capture.c
@@ -279,7 +279,7 @@ capture_input_new_file(capture_options *capture_opts, gchar *new_file)
   /* if we are in real-time mode, open the new file now */
   if(capture_opts->real_time_mode) {
     /* Attempt to open the capture file and set up to read from it. */
-       switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
+    switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
     case CF_OK:
       break;
     case CF_ERROR:
@@ -290,15 +290,18 @@ capture_input_new_file(capture_options *capture_opts, gchar *new_file)
       return FALSE;
       break;
     }
+  }
+
+  if(capture_opts->show_info) {
+    if (!capture_info_new_file(new_file))
+      return FALSE;
+  }
 
+  if(capture_opts->real_time_mode) {
     cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
   } else {
     cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
   }
-
-  if(capture_opts->show_info)
-    capture_info_new_file(new_file);
-
   capture_opts->state = CAPTURE_RUNNING;
 
   return TRUE;
index d9bc8fbfcd619faeff58f89d5d06329943cf7f26..084856d32232b30bfa21be4efe14463f3c82c744 100644 (file)
@@ -34,6 +34,7 @@
 /* XXX - try to remove this later */
 #include <epan/prefs.h>
 /* XXX - try to remove this later */
+#include <epan/filesystem.h>
 
 #include "capture_info.h"
 
@@ -95,11 +96,111 @@ void capture_info_open(const char *iface)
 }
 
 
+static const char *
+cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
+                      int file_type)
+{
+  const char *errmsg;
+  static char errmsg_errno[1024+1];
+
+  if (err < 0) {
+    /* Wiretap error. */
+    switch (err) {
+
+    case WTAP_ERR_NOT_REGULAR_FILE:
+      errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file.";
+      break;
+
+    case WTAP_ERR_FILE_UNKNOWN_FORMAT:
+      /* Seen only when opening a capture file for reading. */
+      errmsg = "The file \"%s\" isn't a capture file in a format Tethereal understands.";
+      break;
+
+    case WTAP_ERR_UNSUPPORTED:
+      /* Seen only when opening a capture file for reading. */
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+               "The file \"%%s\" isn't a capture file in a format Tethereal understands.\n"
+               "(%s)", err_info);
+      g_free(err_info);
+      errmsg = errmsg_errno;
+      break;
+
+    case WTAP_ERR_CANT_WRITE_TO_PIPE:
+      /* Seen only when opening a capture file for writing. */
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+              "The file \"%%s\" is a pipe, and %s capture files can't be "
+              "written to a pipe.", wtap_file_type_string(file_type));
+      errmsg = errmsg_errno;
+      break;
+
+    case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
+      /* Seen only when opening a capture file for writing. */
+      errmsg = "Tethereal doesn't support writing capture files in that format.";
+      break;
+
+    case WTAP_ERR_UNSUPPORTED_ENCAP:
+      if (for_writing)
+        errmsg = "Tethereal can't save this capture in that format.";
+      else {
+        g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+                 "The file \"%%s\" is a capture for a network type that Tethereal doesn't support.\n"
+                 "(%s)", err_info);
+        g_free(err_info);
+        errmsg = errmsg_errno;
+      }
+      break;
+
+    case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
+      if (for_writing)
+        errmsg = "Tethereal can't save this capture in that format.";
+      else
+        errmsg = "The file \"%s\" is a capture for a network type that Tethereal doesn't support.";
+      break;
+
+    case WTAP_ERR_BAD_RECORD:
+      /* Seen only when opening a capture file for reading. */
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+               "The file \"%%s\" appears to be damaged or corrupt.\n"
+               "(%s)", err_info);
+      g_free(err_info);
+      errmsg = errmsg_errno;
+      break;
+
+    case WTAP_ERR_CANT_OPEN:
+      if (for_writing)
+        errmsg = "The file \"%s\" could not be created for some unknown reason.";
+      else
+        errmsg = "The file \"%s\" could not be opened for some unknown reason.";
+      break;
+
+    case WTAP_ERR_SHORT_READ:
+      errmsg = "The file \"%s\" appears to have been cut short"
+               " in the middle of a packet or other data.";
+      break;
+
+    case WTAP_ERR_SHORT_WRITE:
+      errmsg = "A full header couldn't be written to the file \"%s\".";
+      break;
+
+    default:
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+              "The file \"%%s\" could not be %s: %s.",
+              for_writing ? "created" : "opened",
+              wtap_strerror(err));
+      errmsg = errmsg_errno;
+      break;
+    }
+  } else
+    errmsg = file_open_error_message(err, for_writing);
+  return errmsg;
+}
+
 /* new file arrived */
-void capture_info_new_file(const char *new_filename)
+gboolean capture_info_new_file(const char *new_filename)
 {
     int err;
     gchar *err_info;
+    char err_msg[2048+1];
 
 
     if(info_data.wtap != NULL) {
@@ -108,9 +209,13 @@ void capture_info_new_file(const char *new_filename)
 
     info_data.wtap = wtap_open_offline(new_filename, &err, &err_info, FALSE);
     if (!info_data.wtap) {
-        g_warning("capture_info_new_file: wtap open failed: %s", err_info);
-    }
-
+        g_snprintf(err_msg, sizeof err_msg,
+                   cf_open_error_message(err, err_info, FALSE, WTAP_FILE_PCAP),
+                   new_filename);
+        g_warning("capture_info_new_file: %s", err_msg);
+        return FALSE;
+    } else
+        return TRUE;
 }
 
 
index fefedbf564c58e7ef9640de91eb60468646dfb8d..107d9ea9315f9514e3c58fcbd7b53e45a164f040 100644 (file)
@@ -37,7 +37,7 @@
 extern void capture_info_open(const char *iface);
 
 /* new file arrived - (eventually close old wtap), open wtap */
-extern void capture_info_new_file(const char *new_filename);
+extern gboolean capture_info_new_file(const char *new_filename);
 
 /* new packets arrived - read from wtap, count */
 extern void capture_info_new_packets(int to_read);