Add a new routine to create the ".ethereal" directory for a user.
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 23 Oct 2001 05:01:02 +0000 (05:01 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Tue, 23 Oct 2001 05:01:02 +0000 (05:01 +0000)
Use that routine rather than duplicating that code in the routines to
write out the preference file and filter files.

Use it in the code for the color filter dialog, so that the directory in
question is created if necessary.

As that routine returns an error indication, have the code that calls
that routine put up a message box if the attempt fails.

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

epan/filesystem.c
epan/filesystem.h
filters.c
gtk/colors.c
gtk/filter_prefs.c
gtk/prefs_dlg.c
prefs.c

index 244a88dfa448a524d5ba09cce1eb50f96a7517db..60331ae0776f85be2187bea34bccd8daf578ba52 100644 (file)
@@ -1,7 +1,7 @@
 /* filesystem.c
  * Filesystem utility routines
  *
- * $Id: filesystem.c,v 1.10 2001/10/23 03:40:39 guy Exp $
+ * $Id: filesystem.c,v 1.11 2001/10/23 05:00:59 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -386,3 +386,37 @@ get_persconffile_dir(void)
 #endif
        return pf_dir;
 }
+
+/*
+ * Create the directory that holds personal configuration files, if
+ * necessary.  If we attempted to create it, and failed, return -1 and
+ * set "*pf_dir_path_return" to the pathname of the directory; otherwise,
+ * return 0.
+ */
+int
+create_persconffile_dir(const char **pf_dir_path_return)
+{
+       const char *pf_dir_path;
+       struct stat s_buf;
+       int ret;
+
+       pf_dir_path = get_persconffile_dir();
+       if (stat(pf_dir_path, &s_buf) != 0) {
+#ifdef WIN32
+               ret = mkdir(pf_dir_path);
+#else
+               ret = mkdir(pf_dir_path, 0755);
+#endif
+       } else {
+               /*
+                * Something with that pathname exists; if it's not
+                * a directory, we'll get an error if we try to put
+                * something in it, so we don't fail here, we wait
+                * for that attempt fo fail.
+                */
+               ret = 0;
+       }
+       if (ret == -1)
+               *pf_dir_path_return = pf_dir_path;
+       return ret;
+}
index a0956dbb69560a8b294aa8f31234372587c69879..cb4cfde27efe408cd9251e4571d6f1c864e5908a 100644 (file)
@@ -1,7 +1,7 @@
 /* filesystem.h
  * Filesystem utility definitions
  *
- * $Id: filesystem.h,v 1.7 2001/10/22 23:16:01 guy Exp $
+ * $Id: filesystem.h,v 1.8 2001/10/23 05:00:59 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -76,4 +76,12 @@ const char *get_systemfile_dir(void);
  */
 const char *get_persconffile_dir(void);
 
+/*
+ * Create the directory that holds personal configuration files, if
+ * necessary.  If we attempted to create it, and failed, return -1 and
+ * set "*pf_dir_path_return" to the pathname of the directory; otherwise,
+ * return 0.
+ */
+int create_persconffile_dir(const char **pf_dir_path_return);
+
 #endif /* FILESYSTEM_H */
index 5c115e0b439a47640586615ac51f65f1e651d560..9433b244abbdac45e0fce1477080fc07849926d6 100644 (file)
--- a/filters.c
+++ b/filters.c
@@ -1,7 +1,7 @@
 /* filters.c
  * Code for reading and writing the filters file.
  *
- * $Id: filters.c,v 1.11 2001/10/22 22:59:23 guy Exp $
+ * $Id: filters.c,v 1.12 2001/10/23 05:00:57 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
 #include <ctype.h>
 #include <errno.h>
 
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
-#ifdef HAVE_DIRECT_H
-#include <direct.h>            /* to declare "mkdir()" on Windows */
-#endif
-
 #include <glib.h>
 
 #include <filesystem.h>
@@ -425,7 +417,6 @@ save_filter_list(filter_list_type_t list, char **pref_path_return,
   GList      *flp;
   filter_def *filt;
   FILE       *ff;
-  struct stat s_buf;
   guchar     *p, c;
   
   *pref_path_return = NULL;    /* assume no error */
@@ -448,13 +439,6 @@ save_filter_list(filter_list_type_t list, char **pref_path_return,
   }
 
   pf_dir_path = get_persconffile_dir();
-  if (stat(pf_dir_path, &s_buf) != 0)
-#ifdef WIN32
-    mkdir(pf_dir_path);
-#else
-    mkdir(pf_dir_path, 0755);
-#endif
-    
   path_length = strlen(pf_dir_path) + strlen(ff_name) + 2;
   ff_path = (gchar *) g_malloc(path_length);
   sprintf(ff_path, "%s" G_DIR_SEPARATOR_S "%s", pf_dir_path, ff_name);
index 4df4a807e967d1f756293c4fd75fd018b775a3a5..ac29b6db799880beedcb1245325bb5cce785c625 100644 (file)
@@ -1,7 +1,7 @@
 /* colors.c
  * Definitions for color structures and routines
  *
- * $Id: colors.c,v 1.12 2001/10/22 22:59:26 guy Exp $
+ * $Id: colors.c,v 1.13 2001/10/23 05:01:02 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -39,6 +39,7 @@
 #endif
 
 #include <epan/filesystem.h>
+
 #include "gtk/main.h"
 #include "packet.h"
 #include "colors.h"
@@ -287,9 +288,19 @@ write_filter(gpointer filter_arg, gpointer file_arg)
 gboolean
 write_filters(colfilter *filter)
 {
+       const gchar *pf_dir_path;
        const gchar *path;
        FILE *f;
 
+       /* Create the directory that holds personal configuration files,
+          if necessary.  */
+       if (create_persconffile_dir(&pf_dir_path) == -1) {
+         simple_dialog(ESD_TYPE_WARN, NULL,
+               "Can't create directory\n\"%s\"\nfor color files: %s.",
+               pf_dir_path, strerror(errno));
+         return FALSE;
+       }
+
        path = get_colorfilter_file_path();
        if ((f = fopen(path, "w+")) == NULL) {
          simple_dialog(ESD_TYPE_CRIT, NULL,
index 3f23d9103b2a1fe924f17bbdf59d16dcb0b3259a..50f645035d1daf23db22bf7b789a3ed0545b8d1e 100644 (file)
@@ -3,12 +3,11 @@
  * (This used to be a notebook page under "Preferences", hence the
  * "prefs" in the file name.)
  *
- * $Id: filter_prefs.c,v 1.30 2001/06/27 10:00:14 guy Exp $
+ * $Id: filter_prefs.c,v 1.31 2001/10/23 05:01:02 guy Exp $
  *
  * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@zing.org>
+ * By Gerald Combs <gerald@ethereal.com>
  * Copyright 1998 Gerald Combs
- *
  * 
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -33,7 +32,7 @@
 
 #include <gtk/gtk.h>
 
-#include <epan.h>
+#include <epan/filesystem.h>
 
 #include "filters.h"
 #include "gtk/main.h"
@@ -709,10 +708,20 @@ static void
 filter_dlg_save_cb(GtkWidget *save_bt, gpointer data)
 {
        filter_list_type_t list = *(filter_list_type_t *)data;
+       const char *pf_dir_path;
        char *f_path;
        int f_save_errno;
        char *filter_type;
 
+       /* Create the directory that holds personal configuration files,
+          if necessary.  */
+       if (create_persconffile_dir(&pf_dir_path) == -1) {
+               simple_dialog(ESD_TYPE_WARN, NULL,
+                   "Can't create directory\n\"%s\"\nfor filter files: %s.",
+                   pf_dir_path, strerror(errno));
+               return;
+       }
+
        save_filter_list(list, &f_path, &f_save_errno);
        if (f_path != NULL) {
                /* We had an error saving the filter. */
index 38c91f1f82a0c463ee326804364b5570b5516aa7..75d2e399faf3042e3c5e1ca9c8c0b147c790d0c3 100644 (file)
@@ -1,7 +1,7 @@
 /* prefs_dlg.c
  * Routines for handling preferences
  *
- * $Id: prefs_dlg.c,v 1.30 2001/10/22 22:59:26 guy Exp $
+ * $Id: prefs_dlg.c,v 1.31 2001/10/23 05:01:02 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
@@ -45,6 +45,8 @@
 #include <sys/stat.h>
 #endif
 
+#include <epan/filesystem.h>
+
 #include "main.h"
 #include "gtkglobals.h"
 #include "packet.h"
@@ -654,12 +656,20 @@ prefs_main_save_cb(GtkWidget *save_bt, gpointer parent_w)
   gui_prefs_fetch(gtk_object_get_data(GTK_OBJECT(parent_w), E_GUI_PAGE_KEY));
   prefs_module_foreach(module_prefs_fetch, &must_redissect);
 
-  /* Write the preferencs out. */
-  err = write_prefs(&pf_path);
-  if (err != 0) {
+  /* Create the directory that holds personal configuration files, if
+     necessary.  */
+  if (create_persconffile_dir(&pf_path) == -1) {
      simple_dialog(ESD_TYPE_WARN, NULL,
-      "Can't open preferences file\n\"%s\": %s.", pf_path,
-      strerror(err));
+      "Can't create directory\n\"%s\"\nfor preferences file: %s.", pf_path,
+      strerror(errno));
+  } else {
+    /* Write the preferencs out. */
+    err = write_prefs(&pf_path);
+    if (err != 0) {
+       simple_dialog(ESD_TYPE_WARN, NULL,
+        "Can't open preferences file\n\"%s\": %s.", pf_path,
+        strerror(err));
+    }
   }
 
   /* Now apply those preferences.
diff --git a/prefs.c b/prefs.c
index 7e0badd020e5208a7724a426d97c2ab601805218..1a7833f5b8ef67b94d0cd3bd3af594d458aad45b 100644 (file)
--- a/prefs.c
+++ b/prefs.c
@@ -1,7 +1,7 @@
 /* prefs.c
  * Routines for handling preferences
  *
- * $Id: prefs.c,v 1.66 2001/10/22 22:59:23 guy Exp $
+ * $Id: prefs.c,v 1.67 2001/10/23 05:00:57 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@ethereal.com>
 #include <sys/types.h>
 #endif
 
-#ifdef HAVE_DIRECT_H
-#include <direct.h>
-#endif
-
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
 #include <unistd.h>
 #endif
 
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
 #include <filesystem.h>
 #include "globals.h"
 #include "packet.h"
@@ -1463,10 +1455,8 @@ write_module_prefs(gpointer data, gpointer user_data)
 int
 write_prefs(const char **pf_path_return)
 {
-  const char  *pf_dir_path;
   const char  *pf_path;
   FILE        *pf;
-  struct stat  s_buf;
   GList       *clp, *col_l;
   fmt_data    *cfmt;
 
@@ -1476,17 +1466,6 @@ write_prefs(const char **pf_path_return)
    *   so that duplication can be avoided with filter.c
    */
 
-  /*
-   * Create the directory that holds personal configuration files.
-   */
-  pf_dir_path = get_persconffile_dir();
-  if (stat(pf_dir_path, &s_buf) != 0)
-#ifdef WIN32
-    mkdir(pf_dir_path);
-#else
-    mkdir(pf_dir_path, 0755);
-#endif
-
   pf_path = get_preffile_path();
   if ((pf = fopen(pf_path, "w")) == NULL) {
     *pf_path_return = pf_path;