[tracker/urho-sync] Added missing files



commit f697a726b69df2e91b5c5daeb31e9252fa2a3ba2
Author: Philip Van Hoof <philip codeminded be>
Date:   Fri Jul 31 17:43:28 2009 +0200

    Added missing files

 src/libtracker-db/tracker-db-backup.c |  226 +++++++++++++++++++++++++++++++++
 src/libtracker-db/tracker-db-backup.h |   47 +++++++
 2 files changed, 273 insertions(+), 0 deletions(-)
---
diff --git a/src/libtracker-db/tracker-db-backup.c b/src/libtracker-db/tracker-db-backup.c
new file mode 100644
index 0000000..a7246af
--- /dev/null
+++ b/src/libtracker-db/tracker-db-backup.c
@@ -0,0 +1,226 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009, Nokia
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ *
+ * Author: Philip Van Hoof <philip codeminded be>
+ */
+
+#include "config.h"
+
+#include <string.h>
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include <sqlite3.h>
+
+#include <raptor.h>
+
+#include <libtracker-common/tracker-ontology.h>
+#include <libtracker-db/tracker-db-manager.h>
+
+#include "tracker-db-backup.h"
+
+
+typedef struct {
+	TrackerBackupFinished callback;
+	GDestroyNotify destroy;
+	gpointer user_data;
+	GError *error;
+	sqlite3_stmt *stmt;
+	sqlite3 *db, *backup_temp;
+	sqlite3_backup *backup_db;
+	gchar *backup_fname;
+	int result;
+} BackupInfo;
+
+GQuark
+tracker_db_backup_error_quark (void)
+{
+	return g_quark_from_static_string ("tracker-db-backup-error-quark");
+}
+
+static gboolean
+perform_callback (gpointer user_data)
+{
+	BackupInfo *info = user_data;
+
+	if (info->callback) {
+		info->callback (info->error, info->user_data);
+	}
+
+	return FALSE;
+}
+
+static void
+backup_info_free (gpointer user_data)
+{
+	BackupInfo *info = user_data;
+
+	if (info->destroy) {
+		info->destroy (info->user_data);
+	}
+
+	g_clear_error (&info->error);
+
+	if (info->stmt) {
+		sqlite3_finalize (info->stmt);
+	}
+
+	if (info->backup_db) {
+		sqlite3_backup_finish (info->backup_db);
+		info->backup_db = NULL;
+	}
+
+	if (info->backup_temp) {
+		sqlite3_close (info->backup_temp);
+	}
+
+	if (info->db) {
+		sqlite3_close (info->db);
+	}
+
+	if (info->backup_fname) {
+		g_free (info->backup_fname);
+	}
+
+	g_free (info);
+}
+
+
+static gboolean
+backup_file_step (gpointer user_data)
+{
+	BackupInfo *info = user_data;
+	guint cnt = 0;
+	gboolean cont = TRUE;
+
+	while (cont && info->result == SQLITE_OK) {
+
+		info->result = sqlite3_backup_step(info->backup_db, 5);
+
+		switch (info->result) {
+			case SQLITE_OK:
+			break;
+
+			case SQLITE_ERROR:
+			default:
+			cont = FALSE;
+			break;
+		}
+
+		if (cnt > 100) {
+			break;
+		}
+
+		cnt++;
+	}
+
+	return cont;
+}
+
+static void
+on_backup_temp_finished (gpointer user_data)
+{
+	BackupInfo *info = user_data;
+
+	if (info->backup_db) {
+		sqlite3_backup_finish (info->backup_db);
+		info->backup_db = NULL;
+	}
+
+	if (info->db) {
+		sqlite3_close (info->db);
+		info->db = NULL;
+	}
+
+
+	if (!info->error && info->result != SQLITE_DONE) {
+		g_set_error (&info->error, TRACKER_DB_BACKUP_ERROR, 
+		             TRACKER_DB_BACKUP_ERROR_UNKNOWN,
+		             "%s", sqlite3_errmsg (info->backup_temp));
+	}
+
+	perform_callback (info);
+
+	backup_info_free (info);
+
+	return;
+}
+
+
+void
+tracker_db_backup_save (TrackerBackupFinished callback,
+                        gpointer user_data,
+                        GDestroyNotify destroy)
+{
+	BackupInfo *info = g_new0 (BackupInfo, 1);
+	const gchar *db_file = tracker_db_manager_get_file (TRACKER_DB_METADATA);
+	GFile *file, *parent;
+
+	info->callback = callback;
+	info->user_data = user_data;
+	info->destroy = destroy;
+
+	if (sqlite3_open_v2 (db_file, &info->db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
+		g_set_error (&info->error, TRACKER_DB_BACKUP_ERROR, TRACKER_DB_BACKUP_ERROR_UNKNOWN,
+		             "Could not open sqlite3 database:'%s'", db_file);
+
+		g_idle_add_full (G_PRIORITY_DEFAULT, perform_callback, info, 
+		                 backup_info_free);
+
+		return;
+	}
+
+	file = g_file_new_for_path (db_file);
+	parent = g_file_get_parent (file);
+	g_object_unref (file);
+	file = g_file_get_child (parent, "meta-backup.db");
+	g_object_unref (parent);
+	info->backup_fname = g_file_get_path (file);
+	g_object_unref (file);
+
+	if (sqlite3_open (info->backup_fname, &info->backup_temp) != SQLITE_OK) {
+		g_set_error (&info->error, TRACKER_DB_BACKUP_ERROR, TRACKER_DB_BACKUP_ERROR_UNKNOWN,
+		             "Could not open sqlite3 database:'%s'", info->backup_fname);
+
+		g_idle_add_full (G_PRIORITY_DEFAULT, perform_callback, info, 
+		                 backup_info_free);
+
+		return;
+	}
+
+	info->backup_db = sqlite3_backup_init (info->backup_temp, "main", 
+	                                       info->db, "main");
+
+	if (!info->backup_db) {
+		g_set_error (&info->error, TRACKER_DB_BACKUP_ERROR, TRACKER_DB_BACKUP_ERROR_UNKNOWN,
+		             "Unknown error creating backup db: '%s'", info->backup_fname);
+
+		g_idle_add_full (G_PRIORITY_DEFAULT, perform_callback, info, 
+		                 backup_info_free);
+
+		return;
+	}
+
+	g_idle_add_full (G_PRIORITY_DEFAULT, backup_file_step, info, 
+	                 on_backup_temp_finished);
+}
+
+
+
+
diff --git a/src/libtracker-db/tracker-db-backup.h b/src/libtracker-db/tracker-db-backup.h
new file mode 100644
index 0000000..e8ec27a
--- /dev/null
+++ b/src/libtracker-db/tracker-db-backup.h
@@ -0,0 +1,47 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009, Nokia
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ *
+ * Author: Philip Van Hoof <philip codeminded be>
+ */
+
+#ifndef __TRACKER_DB_BACKUP_H__
+#define __TRACKER_DB_BACKUP_H__
+
+#include <glib.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define TRACKER_DB_BACKUP_ERROR	    (tracker_db_backup_error_quark ())
+
+typedef enum {
+	TRACKER_DB_BACKUP_ERROR_UNKNOWN,
+} TrackerDBBackupError;
+
+typedef void (*TrackerBackupFinished)   (GError *error, gpointer user_data);
+
+GQuark    tracker_db_backup_error_quark (void);
+
+void      tracker_db_backup_save        (TrackerBackupFinished callback,
+                                         gpointer user_data,
+                                         GDestroyNotify destroy);
+
+G_END_DECLS
+
+#endif /* __TRACKER_DB_BACKUP_H__ */



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