diff options
Diffstat (limited to 'e-util')
-rw-r--r-- | e-util/ChangeLog | 9 | ||||
-rw-r--r-- | e-util/Makefile.am | 4 | ||||
-rw-r--r-- | e-util/e-db3-utils.c | 185 | ||||
-rw-r--r-- | e-util/e-db3-utils.h | 29 | ||||
-rw-r--r-- | e-util/e-dbhash.c | 69 |
5 files changed, 273 insertions, 23 deletions
diff --git a/e-util/ChangeLog b/e-util/ChangeLog index 8f350ea0f5..0084885485 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,12 @@ +2001-05-31 Christopher James Lahey <clahey@ximian.com> + + * Makefile.am (INCLUDES): Added db3 cflags. + + * e-dbhash.c: Made this use db3. + + * e-db3-utils.c, e-db3-utils.h: New files with some helper + functions. + 2001-05-27 Dan Winship <danw@ximian.com> * e-gui-utils.c: Remove e_pixmaps_update (moved to libeshell) so diff --git a/e-util/Makefile.am b/e-util/Makefile.am index 822c766f43..4b901acd30 100644 --- a/e-util/Makefile.am +++ b/e-util/Makefile.am @@ -3,6 +3,7 @@ SUBDIRS = . ename imagesdir = $(datadir)/images/evolution INCLUDES = \ + $(DB3_CFLAGS) \ -I$(top_srcdir) \ $(GNOME_INCLUDEDIR) \ $(EXTRA_GNOME_CFLAGS) \ @@ -44,6 +45,8 @@ libeutil_la_SOURCES = \ e-time-utils.h \ e-url.c \ e-url.h \ + e-db3-utils.c \ + e-db3-utils.h \ e-dbhash.c \ e-dbhash.h \ md5-utils.c \ @@ -52,6 +55,7 @@ libeutil_la_SOURCES = \ libeutil_la_LIBADD = $(GAL_LIBS) libeutil_static_la_SOURCES = $(libeutil_la_SOURCES) +libeutil_static_la_LIBADD = $(libeutil_la_LIBADD) libeutil_static_la_LDFLAGS = --all-static pilot_sources = \ diff --git a/e-util/e-db3-utils.c b/e-util/e-db3-utils.c new file mode 100644 index 0000000000..c1da265453 --- /dev/null +++ b/e-util/e-db3-utils.c @@ -0,0 +1,185 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ + +#include "config.h" + +#include "e-db3-utils.h" + +#include <db.h> + +#include <errno.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <unistd.h> +#include <libgnome/gnome-defs.h> +#include <libgnome/gnome-util.h> + +#if DB_VERSION_MAJOR != 3 || \ + DB_VERSION_MINOR != 1 || \ + DB_VERSION_PATCH != 17 +#error Including wrong DB3. Need libdb 3.1.17. +#endif + +static char * +get_check_filename (const char *filename) +{ + return g_strdup_printf ("%s-upgrading", filename); +} + +static char * +get_copy_filename (const char *filename) +{ + return g_strdup_printf ("%s-copy", filename); +} + +static int +cp_file (const char *src, const char *dest) +{ + int i; + int o; + char buffer[1024]; + int length; + int place; + + i = open (src, O_RDONLY); + if (i == -1) + return -1; + o = creat (dest, S_IREAD | S_IWRITE); + if (o == -1) { + close (i); + return -1; + } + while (1) { + length = read (i, &buffer, sizeof (buffer)); + + if (length == 0) + break; + + if (length == -1) { + if (errno == EINTR) + continue; + else { + close (i); + close (o); + unlink (dest); + return -1; + } + } + + place = 0; + while (length != 0) { + int count; + count = write (o, buffer + place, length); + if (count == -1) { + if (errno == EINTR) + continue; + else { + close (i); + close (o); + unlink (dest); + return -1; + } + } + + length -= count; + place += count; + } + } + if (close (i)) + return -1; + if (close (o)) + return -1; + return 0; +} + +static int +touch_file (const char *file) +{ + int o; + o = creat (file, S_IREAD | S_IWRITE); + if (o == -1) + return -1; + + if (close (o) == -1) + return -1; + + return 0; +} + +static int +resume_upgrade (const char *filename, const char *copy_filename, const char *check_filename) +{ + DB *db; + int ret_val; + + ret_val = db_create (&db, NULL, 0); + + if (ret_val == 0) + ret_val = cp_file (copy_filename, filename); + + if (ret_val == 0) + ret_val = db->upgrade (db, filename, 0); + + if (ret_val == 0) + ret_val = unlink (check_filename); + if (ret_val == 0) + ret_val = unlink (copy_filename); + + db->close (db, 0); + + return ret_val; +} + +int +e_db3_utils_maybe_recover (const char *filename) +{ + int ret_val = 0; + char *copy_filename; + char *check_filename; + + copy_filename = get_copy_filename (filename); + check_filename = get_check_filename (filename); + + if (g_file_exists (check_filename)) { + ret_val = resume_upgrade(filename, copy_filename, check_filename); + } else if (g_file_exists (copy_filename)) { + unlink (copy_filename); + } + + g_free (copy_filename); + g_free (check_filename); + return ret_val; +} + +int +e_db3_utils_upgrade_format (const char *filename) +{ + char *copy_filename; + char *check_filename; + DB *db; + int ret_val; + + ret_val = db_create (&db, NULL, 0); + if (ret_val != 0) + return ret_val; + + copy_filename = get_copy_filename (filename); + check_filename = get_check_filename (filename); + + ret_val = cp_file (filename, copy_filename); + + if (ret_val == 0) + ret_val = touch_file (check_filename); + if (ret_val == 0) + ret_val = db->upgrade (db, filename, 0); + if (ret_val == 0) + ret_val = unlink (check_filename); + + if (ret_val == 0) + ret_val = unlink (copy_filename); + + db->close (db, 0); + + g_free (check_filename); + g_free (copy_filename); + return ret_val; +} diff --git a/e-util/e-db3-utils.h b/e-util/e-db3-utils.h new file mode 100644 index 0000000000..a574e59178 --- /dev/null +++ b/e-util/e-db3-utils.h @@ -0,0 +1,29 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * db3 utils. + * + * Author: + * Chris Lahey <clahey@ximian.com> + * + * Copyright 2001, Ximian, Inc. + */ + +#ifndef __E_DB3_UTILS_H__ +#define __E_DB3_UTILS_H__ + +#include <glib.h> + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus */ + +int e_db3_utils_maybe_recover (const char *filename); +int e_db3_utils_upgrade_format (const char *filename); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* ! __E_DB3_UTILS_H__ */ + diff --git a/e-util/e-dbhash.c b/e-util/e-dbhash.c index e286500066..a61318e7ab 100644 --- a/e-util/e-dbhash.c +++ b/e-util/e-dbhash.c @@ -7,19 +7,19 @@ */ #include <config.h> + +#include "e-dbhash.h" + #include <string.h> #include <fcntl.h> -#ifdef HAVE_DB_185_H -#include <db_185.h> -#else -#ifdef HAVE_DB1_DB_H -#include <db1/db.h> -#else #include <db.h> -#endif -#endif #include "md5-utils.h" -#include "e-dbhash.h" + +#if DB_VERSION_MAJOR != 3 || \ + DB_VERSION_MINOR != 1 || \ + DB_VERSION_PATCH != 17 +#error Including wrong DB3. Need libdb 3.1.17. +#endif struct _EDbHashPrivate { @@ -31,20 +31,37 @@ e_dbhash_new (const char *filename) { EDbHash *edbh; DB *db; + int rv; + + int major, minor, patch; + + db_version (&major, &minor, &patch); + + if (major != 3 || + minor != 1 || + patch != 17) { + g_warning ("Wrong version of libdb."); + return NULL; + } /* Attempt to open the database */ - db = dbopen (filename, O_RDWR, 0666, DB_HASH, NULL); - if (db == NULL) { - db = dbopen (filename, O_RDWR | O_CREAT, 0666, DB_HASH, NULL); + rv = db_create (&db, NULL, 0); + if (rv != 0) { + return NULL; + } + + rv = db->open (db, filename, NULL, DB_HASH, 0, 0666); + if (rv != 0) { + rv = db->open (db, filename, NULL, DB_HASH, DB_CREATE, 0666); - if (db == NULL) + if (rv != 0) return NULL; } - + edbh = g_new (EDbHash, 1); edbh->priv = g_new (EDbHashPrivate, 1); edbh->priv->db = db; - + return edbh; } @@ -86,7 +103,7 @@ e_dbhash_add (EDbHash *edbh, const gchar *key, const gchar *data) md5_to_dbt (local_hash, &ddata); /* Add to database */ - db->put (db, &dkey, &ddata, 0); + db->put (db, NULL, &dkey, &ddata, 0); } void @@ -105,7 +122,7 @@ e_dbhash_remove (EDbHash *edbh, const char *key) string_to_dbt (key, &dkey); /* Remove from database */ - db->del (db, &dkey, 0); + db->del (db, NULL, &dkey, 0); } void @@ -114,6 +131,7 @@ e_dbhash_foreach_key (EDbHash *edbh, EDbHashFunc func, gpointer user_data) DB *db; DBT dkey; DBT ddata; + DBC *dbc; int db_error = 0; g_return_if_fail (edbh != NULL); @@ -122,13 +140,20 @@ e_dbhash_foreach_key (EDbHash *edbh, EDbHashFunc func, gpointer user_data) db = edbh->priv->db; - db_error = db->seq(db, &dkey, &ddata, R_FIRST); + db_error = db->cursor (db, NULL, &dbc, 0); + + if (db_error != 0) { + return; + } + + db_error = dbc->c_get(dbc, &dkey, &ddata, DB_FIRST); while (db_error == 0) { (*func) ((const char *)dkey.data, user_data); - db_error = db->seq(db, &dkey, &ddata, R_NEXT); + db_error = dbc->c_get(dbc, &dkey, &ddata, DB_NEXT); } + dbc->c_close (dbc); } EDbHashStatus @@ -151,7 +176,7 @@ e_dbhash_compare (EDbHash *edbh, const char *key, const char *compare_data) /* Lookup in database */ memset (&ddata, 0, sizeof (DBT)); - db->get (db, &dkey, &ddata, 0); + db->get (db, NULL, &dkey, &ddata, 0); /* Compare */ if (ddata.data) { @@ -191,10 +216,8 @@ e_dbhash_destroy (EDbHash *edbh) db = edbh->priv->db; /* Close datbase */ - db->close (db); + db->close (db, 0); g_free (edbh->priv); g_free (edbh); } - - |