diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | gconf/gconf.c | 55 | ||||
-rw-r--r-- | gconf/gconf.h | 5 | ||||
-rw-r--r-- | tests/Makefile.am | 6 | ||||
-rw-r--r-- | tests/testunique.c | 46 |
5 files changed, 116 insertions, 1 deletions
@@ -1,3 +1,8 @@ +2000-01-13 Havoc Pennington <hp@pobox.com> + + * gconf/gconf.c (gconf_unique_key): function to generate + a pseudo-random very-very-likely-to-be-unique key name. + 2000-01-13 Havoc Pennington <hp@redhat.com> * gconf/gconf-internals.c (gconf_concat_key_and_dir): move diff --git a/gconf/gconf.c b/gconf/gconf.c index 18a286a9..021d9318 100644 --- a/gconf/gconf.c +++ b/gconf/gconf.c @@ -1630,6 +1630,61 @@ gconf_key_is_below (const gchar* above, const gchar* below) return strncmp(below, above, strlen(above)) == 0; } +gchar* +gconf_unique_key (void) +{ + /* This function is hardly cryptographically random but should be + "good enough" */ + + static guint serial = 0; + gchar* key; + guint t, ut, p, u, r; + struct timeval tv; + + gettimeofday(&tv, NULL); + + t = tv.tv_sec; + ut = tv.tv_usec; + + p = getpid(); + + u = getuid(); + + /* don't bother to seed; if it's based on the time or any other + changing info we can get, we may as well just use that changing + info. since we don't seed we'll at least get a different number + on every call to this function in the same executable. */ + r = rand(); + + /* The letters may increase uniqueness by preventing "melds" + i.e. 01t01k01 and 0101t0k1 are not the same */ + key = g_strdup_printf("%ut%uut%uu%up%ur%uk%u", + /* Duplicate keys must be generated + by two different program instances */ + serial, + /* Duplicate keys must be generated + in the same microsecond */ + t, + ut, + /* Duplicate keys must be generated by + the same user */ + u, + /* Duplicate keys must be generated by + two programs that got the same PID */ + p, + /* Duplicate keys must be generated with the + same random seed and the same index into + the series of pseudorandom values */ + r, + /* Duplicate keys must result from running + this function at the same stack location */ + (guint)&key); + + ++serial; + + return key; +} + /* * Table of connections */ diff --git a/gconf/gconf.h b/gconf/gconf.h index 1939c497..4229a47d 100644 --- a/gconf/gconf.h +++ b/gconf/gconf.h @@ -103,6 +103,11 @@ gboolean gconf_key_is_below (const gchar* above, const gchar* below); /* Returns allocated concatenation of these two */ gchar* gconf_concat_key_and_dir(const gchar* dir, const gchar* key); +/* Returns a different string every time (at least, the chances of + getting a duplicate are like one in a zillion). The key is a + legal gconf key name (a single element of one) */ +gchar* gconf_unique_key (void); + /* * Higher-level stuff */ diff --git a/tests/Makefile.am b/tests/Makefile.am index f3a6c3fd..fb45f955 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,7 +4,11 @@ EFENCE= INCLUDES = -I$(top_srcdir) -I$(top_builddir) -I$(includedir) $(ORBIT_CFLAGS) $(GLIB_CFLAGS) \ -DG_LOG_DOMAIN=\"GConf-Tests\" -DGCONF_ENABLE_INTERNALS=1 -noinst_PROGRAMS=testgconf testlisteners testschemas testchangeset testencode +noinst_PROGRAMS=testgconf testlisteners testschemas testchangeset testencode testunique + +testunique_SOURCES=testunique.c + +testunique_LDADD = $(ORBIT_LIBS) $(GLIB_LIBS) $(top_builddir)/gconf/libgconf.la $(EFENCE) testgconf_SOURCES=testgconf.c diff --git a/tests/testunique.c b/tests/testunique.c new file mode 100644 index 00000000..53324e54 --- /dev/null +++ b/tests/testunique.c @@ -0,0 +1,46 @@ +/* GConf + * Copyright (C) 1999, 2000 Red Hat Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* This isn't an automated test */ + +#include <gconf/gconf.h> +#include <stdio.h> +#include <unistd.h> +#include <math.h> + +int +main (int argc, char** argv) +{ + guint i = 0; + + while (i < 50) + { + gchar* key; + + key = gconf_unique_key(); + + printf("key: %s\n", key); + + g_free(key); + + ++i; + } + + return 0; +} |