Ethereal-dev: [Ethereal-dev] [PATCH] mergecap : better error handling

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Martin <gmsoft@xxxxxxxxxx>
Date: Tue, 28 Dec 2004 16:43:50 +0100
Hi all,

Here is a patch which improve error handling of mergecap.
Instead of quitting if a input file have some error (the file was
trunkated in my case), it continue to read the next file for merging.

The patch is attached.

Regards,

-- 
Guy Martin
Gentoo Linux - HPPA port Lead / IPv6 team
Lug Charleroi (Belgium)
--- merge.c.orig	2004-12-28 15:35:43.335717392 +0100
+++ merge.c	2004-12-28 16:31:55.266106056 +0100
@@ -156,10 +156,10 @@
  * to be merged.
  */
 wtap *
-merge_read_packet(int in_file_count, merge_in_file_t in_files[], int *err,
+merge_read_packet(int in_file_count, merge_in_file_t in_files[],
                   gchar **err_info)
 {
-  int i;
+  int i, err;
   int ei = -1;
   struct timeval tv = {LONG_MAX, LONG_MAX};
   struct wtap_pkthdr *phdr;
@@ -175,10 +175,11 @@
        * No packet available, and we haven't seen an error or EOF yet,
        * so try to read the next packet.
        */
-      if (!wtap_read(in_files[i].wth, err, err_info, &in_files[i].data_offset)) {
-        if (*err != 0) {
+      if (!wtap_read(in_files[i].wth, &err, err_info, &in_files[i].data_offset)) {
+        if (err != 0) {
           in_files[i].state = GOT_ERROR;
-          return NULL;
+          in_files[i].error = err;
+          continue;
         }
         in_files[i].state = AT_EOF;
       } else
@@ -196,7 +197,6 @@
 
   if (ei == -1) {
     /* All the streams are at EOF.  Return an EOF indication. */
-    *err = 0;
     return NULL;
   }
 
@@ -213,29 +213,29 @@
  */
 wtap *
 merge_append_read_packet(int in_file_count, merge_in_file_t in_files[],
-                         int *err, gchar **err_info)
+                         gchar **err_info)
 {
-  int i;
+  int i, err;
 
   /*
    * Find the first file not at EOF, and read the next packet from it.
    */
   for (i = 0; i < in_file_count; i++) {
-    if (in_files[i].state == AT_EOF)
-      continue; /* This file is already at EOF */
-    if (wtap_read(in_files[i].wth, err, err_info, &in_files[i].data_offset))
+    if (in_files[i].state == AT_EOF && in_files[i].state == GOT_ERROR)
+      continue; /* This file is already at EOF or there was a error reading it */
+    if (wtap_read(in_files[i].wth, &err, err_info, &in_files[i].data_offset))
       break; /* We have a packet */
-    if (*err != 0) {
-      /* Read error - quit immediately. */
+    if (err != 0) {
+      /* Read error - continue to the next file. */
       in_files[i].state = GOT_ERROR;
-      return NULL;
+      in_files[i].error = err;
+      continue;
     }
     /* EOF - flag this file as being at EOF, and try the next one. */
     in_files[i].state = AT_EOF;
   }
   if (i == in_file_count) {
     /* All the streams are at EOF.  Return an EOF indication. */
-    *err = 0;
     return NULL;
   }
 
--- mergecap.c.orig	2004-12-28 15:38:41.370651968 +0100
+++ mergecap.c	2004-12-28 16:19:36.087478288 +0100
@@ -140,7 +140,7 @@
   wtap        *wth;
   struct wtap_pkthdr *phdr, snap_phdr;
   wtap_dumper *pdh;
-  int          open_err, read_err, write_err, close_err;
+  int          open_err, write_err, close_err;
   gchar       *err_info;
   int          err_fileno;
   char        *out_filename = NULL;
@@ -298,7 +298,7 @@
               out_filename, strerror(errno));
       exit(1);
     }
-  }  
+  } 
     
   /* prepare the outfile */
   pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, &open_err);
@@ -314,14 +314,10 @@
   count = 1;
   for (;;) {
     if (do_append)
-      wth = merge_append_read_packet(in_file_count, in_files, &read_err,
-                                     &err_info);
+      wth = merge_append_read_packet(in_file_count, in_files, &err_info);
     else
-      wth = merge_read_packet(in_file_count, in_files, &read_err,
-                              &err_info);
+      wth = merge_read_packet(in_file_count, in_files, &err_info);
     if (wth == NULL) {
-      if (read_err != 0)
-        got_read_error = TRUE;
       break;
     }
 
@@ -345,29 +341,28 @@
   }
 
   merge_close_in_files(in_file_count, in_files);
-  if (!got_read_error && !got_write_error) {
+  if (!got_write_error) {
     if (!wtap_dump_close(pdh, &write_err))
       got_write_error = TRUE;
   } else
     wtap_dump_close(pdh, &close_err);
 
-  if (got_read_error) {
-    /*
-     * Find the file on which we got the error, and report the error.
-     */
-    for (i = 0; i < in_file_count; i++) {
-      if (in_files[i].state == GOT_ERROR) {
-        fprintf(stderr, "mergecap: Error reading %s: %s\n",
-                in_files[i].filename, wtap_strerror(read_err));
-        switch (read_err) {
-
-        case WTAP_ERR_UNSUPPORTED:
-        case WTAP_ERR_UNSUPPORTED_ENCAP:
-        case WTAP_ERR_BAD_RECORD:
-          fprintf(stderr, "(%s)\n", err_info);
-          g_free(err_info);
-          break;
-        }
+  /*
+   * Find the file on which we got the error, and report the error.
+   */
+  for (i = 0; i < in_file_count; i++) {
+    if (in_files[i].state == GOT_ERROR) {
+      fprintf(stderr, "mergecap: Error reading %s: %s\n",
+              in_files[i].filename, wtap_strerror(in_files[i].error));
+      switch (in_files[i].error) {
+
+      case WTAP_ERR_UNSUPPORTED:
+      case WTAP_ERR_UNSUPPORTED_ENCAP:
+      case WTAP_ERR_BAD_RECORD:
+				got_read_error = TRUE;
+        fprintf(stderr, "(%s)\n", err_info);
+        g_free(err_info);
+          continue; 
       }
     }
   }
--- merge.h.orig	2004-12-28 15:52:18.805383000 +0100
+++ merge.h	2004-12-28 16:39:09.308121640 +0100
@@ -45,6 +45,7 @@
   long            data_offset;
   in_file_state_e state;
   long            size;		/* file size */
+  int             error;
 } merge_in_file_t;
 
 /** Open a number of input files to merge.
@@ -99,7 +100,7 @@
  * error or EOF
  */
 extern wtap *
-merge_read_packet(int in_file_count, merge_in_file_t in_files[], int *err,
+merge_read_packet(int in_file_count, merge_in_file_t in_files[],
                   gchar **err_info);
 
 
@@ -115,7 +116,7 @@
  */
 extern wtap *
 merge_append_read_packet(int in_file_count, merge_in_file_t in_files[],
-                         int *err, gchar **err_info);
+                         gchar **err_info);
 
 #ifdef __cplusplus
 }

Attachment: pgpSHFb1OspIc.pgp
Description: PGP signature