[capture_info] Move capture_info_new_file() to capture.c
[metze/wireshark/wip.git] / ui / capture.c
index 4ec0da4c37d09eaab935fd91fc2f748fb8f153c8..64c04c08bd21f7735345ae8035fcfaba20e20a0a 100644 (file)
@@ -39,6 +39,7 @@
 
 #include "wsutil/file_util.h"
 #include "wsutil/str_util.h"
+#include <wsutil/filesystem.h>
 #include "log.h"
 
 typedef struct if_stat_cache_item_s {
@@ -287,6 +288,106 @@ capture_input_read_all(capture_session *cap_session, gboolean is_tempfile,
     return TRUE;
 }
 
+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 Wireshark 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\" contains record data that Wireshark doesn't support.\n"
+                "(%s)", err_info != NULL ? err_info : "no information supplied");
+            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_subtype_string(file_type));
+            errmsg = errmsg_errno;
+            break;
+
+        case WTAP_ERR_UNWRITABLE_FILE_TYPE:
+            /* Seen only when opening a capture file for writing. */
+            errmsg = "Wireshark doesn't support writing capture files in that format.";
+            break;
+
+        case WTAP_ERR_UNWRITABLE_ENCAP:
+            /* Seen only when opening a capture file for writing. */
+            errmsg = "Wireshark can't save this capture in that format.";
+            break;
+
+        case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
+            if (for_writing)
+                errmsg = "Wireshark can't save this capture in that format.";
+            else
+                errmsg = "The file \"%s\" is a capture for a network type that Wireshark doesn't support.";
+            break;
+
+        case WTAP_ERR_BAD_FILE:
+            /* 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 != NULL ? err_info : "no information supplied");
+            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;
+
+        case WTAP_ERR_DECOMPRESS:
+            g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+                "The compressed file \"%%s\" appears to be damaged or corrupt.\n"
+                "(%s)", err_info != NULL ? err_info : "no information supplied");
+            g_free(err_info);
+            errmsg = errmsg_errno;
+            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;
+}
 
 /* capture child tells us we have a new (or the first) capture file */
 gboolean
@@ -295,6 +396,8 @@ capture_input_new_file(capture_session *cap_session, gchar *new_file)
     capture_options *capture_opts = cap_session->capture_opts;
     gboolean is_tempfile;
     int  err;
+    gchar *err_info;
+    gchar *err_msg;
 
     if(cap_session->state == CAPTURE_PREPARING) {
         g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started");
@@ -346,8 +449,18 @@ capture_input_new_file(capture_session *cap_session, gchar *new_file)
     }
 
     if(capture_opts->show_info) {
-        if (!capture_info_new_file(new_file, cap_session->cap_data_info))
+        if (cap_session->cap_data_info->wtap != NULL) {
+            wtap_close(cap_session->cap_data_info->wtap);
+        }
+
+        cap_session->cap_data_info->wtap = wtap_open_offline(new_file, WTAP_TYPE_AUTO, &err, &err_info, FALSE);
+        if (!cap_session->cap_data_info->wtap) {
+            err_msg = g_strdup_printf(cf_open_error_message(err, err_info, FALSE, WTAP_FILE_TYPE_SUBTYPE_UNKNOWN),
+                new_file);
+            g_warning("capture_info_new_file: %d (%s)", err, err_msg);
+            g_free(err_msg);
             return FALSE;
+        }
     }
 
     if(capture_opts->real_time_mode) {