Re: backup deleting old files



i couldve sworn i sent this in already...

i did it as i first suggested, with database.pdb.0 etc., but only 'cos im
lazy. just change the sprintf statements to change the behaviour.

this is against gnome-pilot-0.1.52 w/o my other patch

(sorry for any inconvenience :)

mibus

-- 
Robert Mibus <mibus@bigpond.com>
Do not underestimate the value of print statements for debugging.
Don't have aesthetic convulsions when using them, either.
--- backup_conduit.c.orig	Mon Jul 10 15:05:06 2000
+++ backup_conduit.c	Mon Jul 10 15:02:32 2000
@@ -16,6 +16,7 @@
 #include <pwd.h>
 #include <signal.h>
 #include <errno.h>
+#include <dirent.h>
 
 #include <gpilotd/gnome-pilot-conduit-backup.h>
 #include "backup_conduit.h"
@@ -26,10 +27,13 @@
 static void 
 load_configuration(ConduitCfg **c,guint32 pilotId) 
 {
+	gchar *temp_name;
 	gchar *prefix;
 	gchar **exclude_files;
 	gint num_of_exclude_files = 0;
 	guint i;
+	DIR *dir;
+	struct dirent *entry;
 
 	*c = g_new0(ConduitCfg,1);
 	(*c)->child = -1;
@@ -40,6 +44,7 @@
 	(*c)->backup_dir = gnome_config_get_string("backup_dir");
 	(*c)->updated_only = gnome_config_get_bool("updated_only=TRUE");
 	(*c)->remove_deleted = gnome_config_get_bool("remove_deleted=FALSE");	
+	(*c)->no_of_backups = gnome_config_get_int("no_of_backups=0");
 
 	(*c)->exclude_files = NULL;
 	gnome_config_get_vector("exclude_files",
@@ -55,7 +60,6 @@
 	}
 	gnome_config_pop_prefix();
 	
-	(*c)->files_in_backup = NULL;
 	if((*c)->backup_dir != NULL && 
 	   mkdir((*c)->backup_dir,(mode_t)0755) < 0) { /* Wow, I never though I would
 							  use octal in C :) */
@@ -65,6 +69,23 @@
 			*/
 		}
 	}    
+
+        (*c)->files_in_backup = g_list_alloc();
+        dir = opendir ((*c)->backup_dir);
+	while ((entry = readdir (dir))) {
+		if (entry->d_name)
+		    if (strlen(entry->d_name) > 4)
+		    if ((!strcmp(entry->d_name + strlen(entry->d_name)-4, ".pdb")) || (!strcmp(entry->d_name + strlen(entry->d_name)-4, ".prc"))) {
+				if ((*c)->backup_dir[strlen((*c)->backup_dir)-1] == '/')
+					temp_name = g_strdup_printf ("%s%s", ((*c)->backup_dir), entry->d_name);
+				else
+					temp_name = g_strdup_printf ("%s/%s", ((*c)->backup_dir), entry->d_name);
+
+				g_list_append ((*c)->files_in_backup, temp_name);
+			}
+	}
+	closedir (dir);
+
 	(*c)->pilotId = pilotId;
 	g_free(prefix);
 }
@@ -72,13 +93,30 @@
 static void 
 destroy_configuration(ConduitCfg **c) 
 {
+	GList *temp_list;
+	char *backup_name=NULL;
+
 	g_return_if_fail(c!=NULL);
 	g_return_if_fail(*c!=NULL);
 
 	if((*c)->remove_deleted) {
-		g_list_free((*c)->files_in_backup);
+		g_message ("Checking what to delete...");
+		temp_list = (*c)->files_in_backup;
+		while (temp_list) {
+			if (temp_list->data) {
+                backup_name = g_strdup_printf ("%s.del", (char *) temp_list->data);
+				g_message ("Deleting %s", (char *) temp_list->data);
+				g_message (" (to %s)", backup_name);
+				if (rename (temp_list->data, backup_name) != 0)
+				    g_message ("Error renaming :(");
+                g_free (backup_name);
+				g_free (temp_list->data);
+			}
+			temp_list = temp_list->next;
+		}
+	g_list_free((*c)->files_in_backup);
 	}
-	
+
 	g_list_foreach((*c)->exclude_files,(GFunc)g_free,NULL);
 	g_list_free((*c)->exclude_files);
 	g_free((*c)->backup_dir);
@@ -133,6 +171,9 @@
 					ConduitCfg *cfg)
 {
 	char name[256];      /* FIXME: potentiel overun  -  (UGH, this sux -jrb) */
+	char *backup_name_from=NULL;
+	char *backup_name_to=NULL;
+	int i;
 	struct pi_file *f;
 	struct stat statb;
 	struct utimbuf times;
@@ -169,14 +210,13 @@
 
 	if (cfg->remove_deleted) {
 		GList *link;
-
-		link = g_list_find_custom (cfg->files_in_backup,
-					   name,
-					   (GCompareFunc) g_strcasecmp);
-		cfg->files_in_backup =
-			g_list_remove_link (cfg->files_in_backup,
-					    link);
+		link = cfg->files_in_backup;
+		while (link) {
+			if (link->data) if (strcasecmp(link->data, name) == 0) g_list_remove_link (cfg->files_in_backup, link);
+			link=link->next;
+		}
 	}
+
 	if (cfg->updated_only) {
 		if (stat (name, &statb) == 0) {
 			if (PI_DBINFO (dbinfo)->modifyDate == statb.st_mtime) {
@@ -198,7 +238,23 @@
 	g_message(_("Making backup of %s"),PI_DBINFO (dbinfo)->name);
 	gnome_pilot_conduit_message(GNOME_PILOT_CONDUIT(conduit),
 				    _("Making backup of %s"),PI_DBINFO (dbinfo)->name);
-
+				    
+    /* create backup of old db*/
+    for (i = cfg->no_of_backups - 1;i >= 0; i--) {
+        if (i)
+            backup_name_from = g_strdup_printf ("%s.%d", name, i-1);
+        else
+            backup_name_from = strdup (name);
+        backup_name_to = g_strdup_printf ("%s.%d", name, i);
+
+        g_message ("Moving backup from %s to %s", backup_name_from, backup_name_to);
+        if (rename (backup_name_from, backup_name_to) == -1)
+            g_message ("  (failed)");
+        
+        free (backup_name_from);
+        free (backup_name_to);
+    }
+    /* backup new db */
 	f = pi_file_create (name, PI_DBINFO (dbinfo));
 	if(f==0) {
 		g_warning(_("Could not create backup file %s"),name);


--- backup_conduit.h.orig	Mon Jul 10 15:05:14 2000
+++ backup_conduit.h	Mon Jul 10 15:09:09 2000
@@ -12,6 +12,7 @@
   GList *exclude_files;
   GList *files_in_backup;   /* contains the file in backup, any entries when
                                destroy is called are considered deleted on PDA */
+  int no_of_backups;        /* # of backups to keep after the main one */
   gboolean remove_deleted;
   gboolean updated_only;
   guint32 pilotId;


--- backup-conduit-control-applet.c.orig	Mon Jul 10 15:05:32 2000
+++ backup-conduit-control-applet.c	Mon Jul 10 14:55:07 2000
@@ -72,6 +72,8 @@
 	(*c)->backup_dir = gnome_config_get_string("backup_dir");
 	(*c)->updated_only = gnome_config_get_bool("updated_only=TRUE");
 	(*c)->remove_deleted = gnome_config_get_bool("remove_deleted=FALSE");	
+    (*c)->no_of_backups = gnome_config_get_int("no_of_backups=0");
+
 	exclude = gnome_config_get_string("exclude_files");
 	if(exclude != NULL) {
 	  (*c)->exclude_files = NULL;
@@ -133,6 +135,7 @@
 	gnome_config_set_string("backup_dir",c->backup_dir);
 	gnome_config_set_bool("updated_only",c->updated_only);
 	gnome_config_set_bool("remove_deleted",c->remove_deleted);
+	gnome_config_set_int("no_of_backups",c->no_of_backups);
 	gnome_config_set_string("exclude_files",exc);
 	gnome_config_pop_prefix();
 	gnome_config_sync();
@@ -155,6 +158,7 @@
 	d->exclude_files = g_list_copy(c->exclude_files);
 	if(d->files_in_backup) g_list_free(d->files_in_backup);
 	d->files_in_backup = g_list_copy(c->files_in_backup);
+	d->no_of_backups = c->no_of_backups;
 }
 
 static ConduitCfg*


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