Split "load_cap_file()" into "open_cap_file()" and "read_cap_file()".
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sun, 15 Aug 1999 00:26:11 +0000 (00:26 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sun, 15 Aug 1999 00:26:11 +0000 (00:26 +0000)
The former, which used to be called by "load_cap_file()", now just opens
the file and, if the open succeeds, closes any capture file we
previously had open, reinitializes any protocols that need
reinitialization, and saves information about the new capture file in
the "capture_file" structure to which it was passed a pointer.  The
latter reads the file already opened by "read_cap_file()".

For "File/Open", call "open_cap_file()" before dismissing the file
selection box; if it fails, "open_cap_file()" will have popped up a
message box complaining about it - just return, leaving the file
selection box open so the user can, after dismissing the message box,
either try again with a different file name, or dismiss the file
selection box.  (Other file selection boxes should be made to work the
same way.)  If "open_cap_file()" succeeds, dismiss the file selection
box, and read the capture file in.

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

capture.c
ethereal.c
file.c
file.h

index b063a5dd612cc5c25b954c321b0b659e39c1648d..106fc1302e7ec3b9590d46893da573b846fae608 100644 (file)
--- a/capture.c
+++ b/capture.c
@@ -1,7 +1,7 @@
 /* capture.c
  * Routines for packet capture windows
  *
- * $Id: capture.c,v 1.47 1999/08/14 23:47:19 guy Exp $
+ * $Id: capture.c,v 1.48 1999/08/15 00:26:10 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -625,8 +625,8 @@ capture(void) {
   if (pch) {
     /* "pch" is non-NULL only if we successfully started a capture.
        If we haven't, there's no capture file to load. */
-    err = load_cap_file(cf.save_file, NULL, &cf);
-    if (err == 0) {
+    if ((err = open_cap_file(cf.save_file, &cf)) == 0 &&
+       (err = read_cap_file(cf.save_file, NULL, &cf)) == 0) {
       set_menu_sensitivity("/File/Save", TRUE);
       set_menu_sensitivity("/File/Save As...", FALSE);
     }
index b1ddf01e81de1c9128c2bf4e21f836e810f1b240..e23b08ccca48c196be1ae258eea090dd54ea7e0b 100644 (file)
@@ -1,6 +1,6 @@
 /* ETHEREal.c
  *
- * $Id: ethereal.c,v 1.85 1999/08/14 19:53:31 gram Exp $
+ * $Id: ethereal.c,v 1.86 1999/08/15 00:26:09 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -433,11 +433,21 @@ file_open_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
          rfilter = g_strdup(s);
   else
          rfilter = NULL;
+
+  /* Try to open the capture file. */
+  if ((err = open_cap_file(cf_name, &cf)) != 0) {
+    /* We couldn't open it; don't dismiss the open dialog box,
+       just leave it around so that the user can, after they
+       dismiss the alert box popped up for the open error,
+       try again. */
+    return;
+  }
+
   gtk_widget_hide(GTK_WIDGET (fs));
   gtk_widget_destroy(GTK_WIDGET (fs));
 
   /* save the directory name. We can write over cf_name */
-  if ((err = load_cap_file(cf_name, rfilter, &cf)) == 0) {
+  if ((err = read_cap_file(cf_name, rfilter, &cf)) == 0) {
          s = strrchr(cf_name, '/');
          if (s && last_open_dir) {
                  *s = '\0';
@@ -517,8 +527,8 @@ file_save_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
        g_free(cf.save_file);
        cf.save_file = g_strdup(cf_name);
        cf.user_saved = 1;
-       err = load_cap_file(cf_name, g_strdup(cf.rfilter), &cf);
-       if (err == 0) {
+        if ((err = open_cap_file(cf_name, &cf)) == 0 &&
+           (err = read_cap_file(cf_name, g_strdup(cf.rfilter), &cf)) == 0) {
                set_menu_sensitivity("/File/Save", FALSE);
                set_menu_sensitivity("/File/Save As...", TRUE);
        }
@@ -537,8 +547,8 @@ file_save_as_ok_cb(GtkWidget *w, GtkFileSelection *fs) {
        g_free(cf.filename);
        cf.filename = g_strdup(cf_name);
        cf.user_saved = 1;
-       err = load_cap_file(cf.filename, g_strdup(cf.rfilter), &cf);
-       if (err == 0) {
+        if ((err = open_cap_file(cf.filename, &cf)) == 0 &&
+           (err = read_cap_file(cf.filename, g_strdup(cf.rfilter), &cf)) == 0) {
                set_menu_sensitivity("/File/Save", FALSE);
                set_menu_sensitivity("/File/Save As...", TRUE);
        }
@@ -554,7 +564,8 @@ file_reload_cmd_cb(GtkWidget *w, gpointer data) {
 
   if (cf.dfilter) g_free(cf.dfilter);
   cf.dfilter = g_strdup(gtk_entry_get_text(GTK_ENTRY(filter_te)));
-  load_cap_file(cf.filename, g_strdup(cf.rfilter), &cf);
+  if (open_cap_file(cf.filename, &cf) == 0)
+    read_cap_file(cf.filename, g_strdup(cf.rfilter), &cf);
   /* XXX - change the menu if it fails? */
 }
 
@@ -1404,7 +1415,8 @@ main(int argc, char *argv[])
      alert box, so, if we get one of those, it's more likely to come
      up on top of us. */
   if (cf_name) {
-    err = load_cap_file(cf_name, rfilter, &cf);
+    if ((err = open_cap_file(cf_name, &cf)) == 0)
+       err = read_cap_file(cf_name, rfilter, &cf);
     cf_name[0] = '\0';
     if (err == 0)
       set_menu_sensitivity("/File/Save As...", TRUE);
diff --git a/file.c b/file.c
index 0bc659fb05f4e507936421cb541147a4f3b531fe..d4e13f02ba15bc558e160acf42eb89bd32be16c9 100644 (file)
--- a/file.c
+++ b/file.c
@@ -1,7 +1,7 @@
 /* file.c
  * File I/O routines
  *
- * $Id: file.c,v 1.67 1999/08/14 18:51:26 gram Exp $
+ * $Id: file.c,v 1.68 1999/08/15 00:26:09 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -102,52 +102,75 @@ static gint dfilter_progress_cb(gpointer p);
 int
 open_cap_file(char *fname, capture_file *cf) {
   struct stat cf_stat;
+  FILE       *fh;
+  wtap       *wth;
+  int        err;
 
   /* First, make sure the file is valid */
-  if (stat(fname, &cf_stat))
-    return (errno);
+  if (stat(fname, &cf_stat)) {
+    err = errno;
+    goto fail;
+  }
 #ifndef WIN32
-  if (! S_ISREG(cf_stat.st_mode) && ! S_ISFIFO(cf_stat.st_mode))
-    return (OPEN_CAP_FILE_NOT_REGULAR);
+  if (! S_ISREG(cf_stat.st_mode) && ! S_ISFIFO(cf_stat.st_mode)) {
+    err = OPEN_CAP_FILE_NOT_REGULAR;
+    goto fail;
+  }
 #endif
 
-  /* Next, try to open the file */
-  cf->fh = fopen(fname, "r");
-  if (cf->fh == NULL)
-    return (errno);
-
-  fseek(cf->fh, 0L, SEEK_END);
-  cf->f_len = ftell(cf->fh);
-  fclose(cf->fh);
-  cf->fh = NULL;
-  /* set the file name beacuse we need it to set the follow stream filter */
-  cf->filename = g_strdup( fname );
-
-  /* Next, find out what type of file we're dealing with */
-  cf->cd_t      = WTAP_FILE_UNKNOWN;
-  cf->cd_t_desc = "unknown";
-  cf->count     = 0;
-  cf->drops     = 0;
-  cf->esec      = 0;
-  cf->eusec     = 0;
-  cf->snap      = 0;
-  firstsec = 0, firstusec = 0;
-  prevsec = 0, prevusec = 0;
-  cf->wth = wtap_open_offline(fname);
-  if (cf->wth == NULL) {
+  /* Next, try to open the file.
+     XXX - we only need to do this because "wtap_open_offline()"
+     doesn't return an indication of whether the open failed because
+     we don't have access to the file, or because it's not a valid
+     capture file, so we first have to open it with "fopen()" to
+     make sure we have access to it as a boring ordinary file. */
+  fh = fopen(fname, "r");
+  if (fh == NULL) {
+    err = errno;
+    goto fail;
+  }
+  fclose(fh);
 
+  /* Next, try to open it as a wiretap capture file. */
+  wth = wtap_open_offline(fname);
+  if (wth == NULL) {
     /* XXX - we assume that, because we were able to open it above,
        this must have failed because it's not a capture file in
        a format we can read. */
-    return (OPEN_CAP_FILE_UNKNOWN_FORMAT);
+    err = OPEN_CAP_FILE_UNKNOWN_FORMAT;
+    goto fail;
   }
 
+  /* The open succeeded.  Close whatever capture file we had open,
+     and fill in the information for this file. */
+  close_cap_file(cf, info_bar, file_ctx);
+
+  /* Initialize protocol-specific variables */
+  ncp_init_protocol();
+
+  cf->wth = wth;
   cf->fh = wtap_file(cf->wth);
-  cf->cd_t = wtap_file_type(cf->wth);
+  cf->f_len = cf_stat.st_size;
+
+  /* set the file name because we need it to set the follow stream filter */
+  cf->filename = g_strdup(fname);
+
+  cf->cd_t      = wtap_file_type(cf->wth);
   cf->cd_t_desc = wtap_file_type_string(cf->wth);
-  cf->snap = wtap_snapshot_length(cf->wth);
+  cf->count     = 0;
+  cf->drops     = 0;
+  cf->esec      = 0;
+  cf->eusec     = 0;
+  cf->snap      = wtap_snapshot_length(cf->wth);
+  firstsec = 0, firstusec = 0;
+  prevsec = 0, prevusec = 0;
   return (0);
+
+fail:
+  simple_dialog(ESD_TYPE_WARN, NULL,
+                       file_open_error_message(err, FALSE), fname);
+  return (err);
 }
 
 /* Reset everything to a pristine state */
@@ -187,7 +210,7 @@ close_cap_file(capture_file *cf, void *w, guint context) {
 }
 
 int
-load_cap_file(char *fname, char *rfilter, capture_file *cf) {
+read_cap_file(char *fname, char *rfilter, capture_file *cf) {
   gchar  *name_ptr, *load_msg, *load_fmt = " Loading: %s...";
   gchar  *done_fmt = " File: %s  Drops: %d";
   gchar  *err_fmt  = " Error: Could not load '%s'";
@@ -195,11 +218,6 @@ load_cap_file(char *fname, char *rfilter, capture_file *cf) {
   size_t  msg_len;
   int     err;
 
-  close_cap_file(cf, info_bar, file_ctx);
-
-  /* Initialize protocol-specific variables */
-  ncp_init_protocol();
-
   if ((name_ptr = (gchar *) strrchr(fname, '/')) == NULL)
     name_ptr = fname;
   else
@@ -215,11 +233,8 @@ load_cap_file(char *fname, char *rfilter, capture_file *cf) {
   }
 
   err = open_cap_file(fname, cf);
-  if (err != 0) {
-    simple_dialog(ESD_TYPE_WARN, NULL,
-                       file_open_error_message(err, FALSE), fname);
+  if (err != 0)
     goto fail;
-  }
 
   load_msg = g_malloc(strlen(name_ptr) + strlen(load_fmt) + 2);
   sprintf(load_msg, load_fmt, name_ptr);
@@ -353,11 +368,6 @@ tail_cap_file(char *fname, capture_file *cf) {
   int     err;
   int     i;
 
-  close_cap_file(cf, info_bar, file_ctx);
-
-  /* Initialize protocol-specific variables */
-  ncp_init_protocol();
-
   err = open_cap_file(fname, cf);
   if ((err == 0) && (cf->cd_t != WTAP_FILE_UNKNOWN)) {
 
diff --git a/file.h b/file.h
index c6a0b663552d296a136f62bdeb094383c5ace809..0469fc5972cd66f17a766bb7e537ec34f70c1fac 100644 (file)
--- a/file.h
+++ b/file.h
@@ -1,7 +1,7 @@
 /* file.h
  * Definitions for file structures and routines
  *
- * $Id: file.h,v 1.32 1999/08/13 23:47:42 gram Exp $
+ * $Id: file.h,v 1.33 1999/08/15 00:26:11 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
@@ -106,7 +106,7 @@ typedef struct _capture_file {
 
 int  open_cap_file(char *, capture_file *);
 void close_cap_file(capture_file *, void *, guint);
-int  load_cap_file(char *, char *, capture_file *);
+int  read_cap_file(char *, char *, capture_file *);
 int  tail_cap_file(char *, capture_file *);
 /* size_t read_frame_header(capture_file *); */