[libgda] GdaSql & GdaBrowser: added --data-files-purge option



commit f86d2bdf0f77b0cd284d5dd09d34d27c425f3f20
Author: Vivien Malerba <malerba gnome-db org>
Date:   Fri Jan 7 16:12:38 2011 +0100

    GdaSql & GdaBrowser: added --data-files-purge option
    
    to help clean up the files created by these tools

 tools/browser/main.c |   18 ++++++-
 tools/config-info.c  |  153 +++++++++++++++++++++++++++++++++++++++++++++++++-
 tools/config-info.h  |    1 +
 tools/gda-sql.1.in   |   16 +++++
 tools/gda-sql.c      |   14 +++++
 5 files changed, 200 insertions(+), 2 deletions(-)
---
diff --git a/tools/browser/main.c b/tools/browser/main.c
index 53ea110..bed1f99 100644
--- a/tools/browser/main.c
+++ b/tools/browser/main.c
@@ -1,5 +1,5 @@
 /* 
- * Copyright (C) 2009 - 2010 The GNOME Foundation.
+ * Copyright (C) 2009 - 2011 The GNOME Foundation.
  *
  * AUTHORS:
  *      Vivien Malerba <malerba gnome-db org>
@@ -72,6 +72,7 @@ gchar *perspective = NULL;
 gboolean list_configs = FALSE;
 gboolean list_providers = FALSE;
 gboolean list_data_files = FALSE;
+gchar *purge_data_files = NULL;
 
 static GOptionEntry entries[] = {
         { "perspective", 'p', 0, G_OPTION_ARG_STRING, &perspective, "Perspective", "default perspective "
@@ -79,6 +80,7 @@ static GOptionEntry entries[] = {
         { "list-dsn", 'l', 0, G_OPTION_ARG_NONE, &list_configs, "List configured data sources and exit", NULL },
         { "list-providers", 'L', 0, G_OPTION_ARG_NONE, &list_providers, "List installed database providers and exit", NULL },
         { "data-files-list", 0, 0, G_OPTION_ARG_NONE, &list_data_files, "List files used to hold information related to each connection", NULL },
+        { "data-files-purge", 0, 0, G_OPTION_ARG_STRING, &purge_data_files, "Remove some files used to hold information related to each connection", "criteria"},
 	{ NULL, 0, 0, 0, NULL, NULL, NULL }
 };
 
@@ -146,6 +148,20 @@ main (int argc, char *argv[])
 				 error->message);	
 		return 0;
 	}
+	if (purge_data_files) {
+		gchar *tmp;
+
+		gda_init ();
+		tmp = config_info_purge_data_files (purge_data_files, &error);
+		if (tmp) {
+			g_print ("%s\n", tmp);
+			g_free (tmp);
+		}
+		if (error)
+			g_print (_("Error while purging files used to store information about each connection: %s\n"),
+				 error->message);	
+		return 0;
+	}
 
 	gdaui_init ();
 	gtk_init (&argc, &argv);
diff --git a/tools/config-info.c b/tools/config-info.c
index a2768da..979bf6f 100644
--- a/tools/config-info.c
+++ b/tools/config-info.c
@@ -1,5 +1,5 @@
 /* 
- * Copyright (C) 2010 The GNOME Foundation.
+ * Copyright (C) 2010 - 2011 The GNOME Foundation.
  *
  * AUTHORS:
  *      Vivien Malerba <malerba gnome-db org>
@@ -21,6 +21,7 @@
 
 #include "config-info.h"
 #include <glib/gi18n-lib.h>
+#include <glib/gstdio.h>
 
 GdaDataModel *
 config_info_list_all_dsn (void)
@@ -180,6 +181,22 @@ config_info_list_data_files (GError **error)
 			gda_data_model_set_value_at (model, 1, row, value, NULL);
 			gda_value_free (value);
 		}
+		else {
+			gchar *ptr;
+			gint i;
+			for (i = 0, ptr = dsn; *ptr; ptr++, i++) {
+				if (((*ptr < 'a') && (*ptr > 'z')) &&
+				    ((*ptr < '0') && (*ptr > '9')))
+					break;
+			}
+			if (*ptr || (i != 40)) {
+				/* this used to be a DSN which has been removed */
+				g_value_take_string ((value = gda_value_new (G_TYPE_STRING)),
+						     g_strdup_printf (_("(%s)"), dsn));
+				gda_data_model_set_value_at (model, 1, row, value, NULL);
+				gda_value_free (value);
+			}
+		}
 
 		/* Open the file */
 		GdaMetaStore *store;
@@ -213,9 +230,143 @@ config_info_list_data_files (GError **error)
 	return model;
 }
 
+typedef enum {
+	PURGE_ALL,
+	PURGE_NON_DSN,
+	PURGE_NON_EXIST_DSN,
+	
+	PURGE_LIST_ONLY,
+
+	PURGE_UNKNOWN
+} PurgeCriteria;
+
+static PurgeCriteria
+parse_criteria (const gchar *criteria)
+{
+	if (!g_ascii_strcasecmp (criteria, "all"))
+		return PURGE_ALL;
+	else if (!g_ascii_strcasecmp (criteria, "non-dsn"))
+		return PURGE_NON_DSN;
+	else if (!g_ascii_strcasecmp (criteria, "non-exist-dsn"))
+		return PURGE_NON_EXIST_DSN;
+	else if (!g_ascii_strcasecmp (criteria, "list-only"))
+		return PURGE_LIST_ONLY;
+	else
+		return PURGE_UNKNOWN;
+}
 
+gchar *
+config_info_purge_data_files (const gchar *criteria, GError **error)
+{
+	PurgeCriteria cri = PURGE_UNKNOWN;
+	GString *string = NULL;
+	gchar **array;
+	gint i;
+	gboolean list_only = FALSE;
+
+	array = g_strsplit_set (criteria, ",:", 0);
+	for (i = 0; array[i]; i++) {
+		PurgeCriteria tmpcri;
+		tmpcri = parse_criteria (array[i]);
+		if (tmpcri == PURGE_LIST_ONLY)
+			list_only = TRUE;
+		else if (tmpcri != PURGE_UNKNOWN)
+			cri = tmpcri;
+	}
+	g_strfreev (array);
+	if (cri == PURGE_UNKNOWN) {
+		g_set_error (error, 0, 0, "Unknown criteria '%s'", criteria);
+		return NULL;
+	}
 
+	const gchar *name;
+	gchar *confdir;
+	GDir *dir;
+	GString *errstring = NULL;
 
+	/* open directory */
+	confdir = config_info_compute_dict_directory ();
+	dir = g_dir_open (confdir, 0, error);
+	if (!dir) {
+		g_free (confdir);
+		return NULL;
+	}
+	while ((name = g_dir_read_name (dir))) {
+		gchar *copy, *dsn, *fname;
+		gboolean to_remove = FALSE;
+		
+		if (! g_str_has_suffix (name, ".db"))
+			continue;
+		if (! g_str_has_prefix (name, "gda-sql-"))
+			continue;
+
+		fname = g_build_filename (confdir, name, NULL);
+		copy = g_strdup (name);
+
+		dsn = copy + 8;
+		dsn [strlen (dsn) - 3] = 0;
+
+		if (cri == PURGE_ALL)
+			to_remove = TRUE;
+		else if ((cri == PURGE_NON_DSN) || (cri == PURGE_NON_EXIST_DSN)) {
+			GdaDsnInfo *dsninfo;
+			dsninfo = gda_config_get_dsn_info (dsn);
+			if (! dsninfo) {
+				to_remove = TRUE;
+				if (cri == PURGE_NON_DSN) {
+					gchar *ptr;
+					gint i;
+					for (i = 0, ptr = dsn; *ptr; ptr++, i++) {
+						if (((*ptr < 'a') && (*ptr > 'z')) &&
+						    ((*ptr < '0') && (*ptr > '9')))
+							break;
+					}
+					if (*ptr || (i != 40))
+						/* this used to be a DSN which has been removed */
+						to_remove = FALSE;
+				}
+			}
+		}		
+		
+		if (to_remove) {
+			if ((! list_only) && g_unlink (fname)) {
+				if (! errstring)
+					errstring = g_string_new ("");
+
+				g_string_append_c (errstring, '\n');
+				g_string_append (errstring, _("Failed to remove: "));
+				g_string_append (errstring, fname);
+			}
+			else {
+				if (! string)
+					string = g_string_new (fname);
+				else {
+					g_string_append_c (string, '\n');
+					g_string_append (string, fname);
+				}
+			}
+		}
+		
+		g_free (copy);
+		g_free (fname);
+	}
+	g_free (confdir);
+	g_dir_close (dir);
+
+	if (errstring) {
+		g_set_error (error, 0, 0, "%s", errstring->str);
+		g_string_free (errstring, TRUE);
+	}
+
+	if (string)
+		return g_string_free (string, FALSE);
+	else {
+		if (!errstring)
+			return g_strdup_printf (_("No file to purge!"));
+		else
+			return NULL;
+	}
+}
 
 
 gchar *
diff --git a/tools/config-info.h b/tools/config-info.h
index e2c0789..c794c86 100644
--- a/tools/config-info.h
+++ b/tools/config-info.h
@@ -32,5 +32,6 @@ GdaDataModel *config_info_list_all_dsn (void);
 GdaDataModel *config_info_list_all_providers (void);
 GdaDataModel *config_info_detail_provider (const gchar *provider, GError **error);
 GdaDataModel *config_info_list_data_files (GError **error);
+gchar        *config_info_purge_data_files (const gchar *criteria, GError **error);
 
 #endif
diff --git a/tools/gda-sql.1.in b/tools/gda-sql.1.in
index 9260376..0573f98 100644
--- a/tools/gda-sql.1.in
+++ b/tools/gda-sql.1.in
@@ -15,6 +15,7 @@ gda-sql - an SQL console based on Libgda
 [\-s] [\-\-http\-port \fI<port>\fP]
 [\-t] [\-\-http\-token \fI<token phrase>\fP]
 [\-\-data\-files\-list]
+[\-\-data\-files\-purge \fI<criteria>\fP]
 [connection's spec] [connection's spec...]
 
 .SH DESCRIPTION
@@ -72,7 +73,22 @@ Requires HTTP clients to authenticate by providing the \fI<token phrase>\fP
 .B \-\-data\-files\-list
 Lists all the files used to hold information related to each connection (ie.
 information gathered by the tool about the connection such as meta data, defined statements,...)
+.TP 8
+.B \-\-data\-files\-purge \fI<criteria>\fP
+Removes file used to hold information related to each connection for the criteria passed as argument
+(note that adding \fB"list-only"\fP to the criteria, either before or after it using a comma, will
+not actually remove the file):
+
+\fB"non-dsn"\fP: remove all the files which do not correspond to a DSN (data source name). These are the
+files created when a connection is specified using connection parameters instead of using a DSN
+
+\fB"non-exist-dsn"\fP: same as \fB"non-dsn"\fP except it also removes the files which were for DSN which
+don't exist anymore
+
+\fB"all"\fP: remove all the files, for a complete cleanup
 
+For example: \fB\-\-data\-files\-purge all,list-only\fP lists all the files (which would be removed
+if the command was \fB\-\-data\-files\-purge all\fP).
 
 .SH ENVIRONMENT
 gda-sql can be configured through some environment variables:
diff --git a/tools/gda-sql.c b/tools/gda-sql.c
index 6c4252c..c475fb1 100644
--- a/tools/gda-sql.c
+++ b/tools/gda-sql.c
@@ -68,6 +68,7 @@ gboolean list_configs = FALSE;
 gboolean list_providers = FALSE;
 
 gboolean list_data_files = FALSE;
+gchar *purge_data_files = NULL;
 
 gchar *outfile = NULL;
 gboolean has_threads;
@@ -90,6 +91,7 @@ static GOptionEntry entries[] = {
 	{ "http-token", 't', 0, G_OPTION_ARG_STRING, &auth_token, "Authentication token (required to authenticate clients)", "token phrase" },
 #endif
         { "data-files-list", 0, 0, G_OPTION_ARG_NONE, &list_data_files, "List files used to hold information related to each connection", NULL },
+        { "data-files-purge", 0, 0, G_OPTION_ARG_STRING, &purge_data_files, "Remove some files used to hold information related to each connection", "criteria"},
         { NULL, 0, 0, 0, NULL, NULL, NULL }
 };
 
@@ -232,6 +234,18 @@ main (int argc, char *argv[])
 				 error->message);	
 		goto cleanup;
 	}
+	if (purge_data_files) {
+		gchar *tmp;
+		tmp = config_info_purge_data_files (purge_data_files, &error);
+		if (tmp) {
+			output_string (tmp);
+			g_free (tmp);
+		}
+		if (error)
+			g_print (_("Error while purging files used to store information about each connection: %s\n"),
+				 error->message);	
+		goto cleanup;
+	}
 
 	/* commands file */
 	if (commandsfile) {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]