1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
|
/* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include "gconf/gconf-backend.h"
#include "gconf/gconf-internals.h"
#include "gconf/gconf.h"
#include "xml-cache.h"
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
/*
* Overview
*
* Basically we have a directory tree underneath an arbitrary root
* directory. The directory tree reflects the configuration
* namespace. Each directory contains an XML file which contains
* metadata for the directory and the key-value pairs in that
* directory. The magic file in each directory is called %gconf.xml,
* and can't clash with the database namespace because names containing
* % aren't allowed. So:
*
* /
* %gconf.xml
* guppi/
* %gconf.xml
* gnumeric/
* %gconf.xml
*
*
* Locking
*
* Locking doesn't _really_ matter because there's only one instance
* of the daemon at a time. However, eventually we want a non-daemon
* command line tool and library, e.g. for the debconf stuff,
* so we will eventually have locking. I'll figure out then how
* it will work.
*
* Caching
*
* I haven't decided the best way to do caching yet. As a first cut;
* we'll cache the parse tree for any files we've looked at. The cache
* will contain time stamps; we'll nuke cache entries that haven't been
* used in a while, either in a main loop timeout or by checking whenever
* we add a new cache entry. Parse trees correspond to "directories" in the
* configuration namespace.
*
* A more precise cache will store specific key-value pairs; this cache
* will probably contain a pointer to the parse tree node the key-value
* pair is inside.
*
* We'll of course need a "dirty" list of stuff not yet written to disk.
*
* We'll save the mod time of parse trees when we load them, so we can
* paranoia check that no one has change the file before we save.
*
* Ideally we could monitor our own process size and also free up
* cache whenever we started to use massive RAM. However, not sure
* this can be done at all portably. Could possibly have some measure
* of parse tree size.
*
* The libxml parse trees are pretty huge, so in theory we could
* "compress" them by extracting all the information we want into a
* specialized data structure, then nuking the parse tree. However,
* that would add more CPU overhead at load and save time. Anyway, as
* a first cut I'm not going to do this, we might do it later.
*
* Atomic Saving
*
* We'll want to save atomically by creating a temporary file for the
* new file version, renaming the original file, moving the temporary
* file into place, then deleting the original file, checking for
* errors and mod times along the way.
*
* Failed lookup caching
*
* If a key/directory doesn't exist, we create a cache entry anyway
* so we can rapidly re-determine that it doesn't exist.
* We also need to save "dirty" nonexistent entries, so we can delete
* the stuff off disk.
*
*/
typedef struct _XMLSource XMLSource;
/* XMLSource **/
struct _XMLSource {
GConfSource source; /* inherit from GConfSource */
Cache* cache;
gchar* root_dir;
guint timeout_id;
GConfLock* lock;
guint dir_mode;
guint file_mode;
};
static XMLSource* xs_new (const gchar* root_dir,
guint dir_mode,
guint file_mode,
GConfLock* lock);
static void xs_destroy (XMLSource* source);
/*
* VTable functions
*/
/* shutdown() is a BSD libc function */
static void x_shutdown (GError** err);
static GConfSource* resolve_address (const gchar* address,
GError** err);
static void lock (GConfSource* source,
GError** err);
static void unlock (GConfSource* source,
GError** err);
static gboolean readable (GConfSource* source,
const gchar* key,
GError** err);
static gboolean writable (GConfSource* source,
const gchar* key,
GError** err);
static GConfValue* query_value (GConfSource* source,
const gchar* key,
const gchar** locales,
gchar** schema_name,
GError** err);
static GConfMetaInfo*query_metainfo (GConfSource* source,
const gchar* key,
GError** err);
static void set_value (GConfSource* source,
const gchar* key,
const GConfValue* value,
GError** err);
static GSList* all_entries (GConfSource* source,
const gchar* dir,
const gchar** locales,
GError** err);
static GSList* all_subdirs (GConfSource* source,
const gchar* dir,
GError** err);
static void unset_value (GConfSource* source,
const gchar* key,
const gchar* locale,
GError** err);
static gboolean dir_exists (GConfSource *source,
const gchar *dir,
GError** err);
static void remove_dir (GConfSource* source,
const gchar* dir,
GError** err);
static void set_schema (GConfSource* source,
const gchar* key,
const gchar* schema_key,
GError** err);
static gboolean sync_all (GConfSource* source,
GError** err);
static void destroy_source (GConfSource* source);
static void clear_cache (GConfSource* source);
static void blow_away_locks (const char *address);
static GConfBackendVTable xml_vtable = {
sizeof (GConfBackendVTable),
x_shutdown,
resolve_address,
lock,
unlock,
readable,
writable,
query_value,
query_metainfo,
set_value,
all_entries,
all_subdirs,
unset_value,
dir_exists,
remove_dir,
set_schema,
sync_all,
destroy_source,
clear_cache,
blow_away_locks,
NULL, /* set_notify_func */
NULL, /* add_listener */
NULL /* remove_listener */
};
static void
x_shutdown (GError** err)
{
gconf_log(GCL_DEBUG, _("Unloading XML backend module."));
}
static void
lock (GConfSource* source,
GError** err)
{
}
static void
unlock (GConfSource* source,
GError** err)
{
}
static gboolean
readable (GConfSource* source,
const gchar* key,
GError** err)
{
return TRUE;
}
static gboolean
writable (GConfSource* source,
const gchar* key,
GError** err)
{
return TRUE;
}
static char*
get_dir_from_address (const char *address,
GError **err)
{
char *root_dir;
int len;
root_dir = gconf_address_resource (address);
if (root_dir == NULL)
{
gconf_set_error (err, GCONF_ERROR_BAD_ADDRESS,
_("Couldn't find the XML root directory in the address `%s'"),
address);
return NULL;
}
/* Chop trailing '/' to canonicalize */
len = strlen (root_dir);
if (root_dir[len-1] == '/')
root_dir[len-1] = '\0';
return root_dir;
}
static char*
get_lock_dir_from_root_dir (const char *root_dir)
{
gchar* lockdir;
lockdir = gconf_concat_dir_and_key (root_dir, "%gconf-xml-backend.lock");
return lockdir;
}
static GConfSource*
resolve_address (const gchar* address, GError** err)
{
struct stat statbuf;
gchar* root_dir;
XMLSource* xsource;
GConfSource* source;
gint flags = 0;
GConfLock* lock = NULL;
guint dir_mode = 0700;
guint file_mode = 0600;
gchar** address_flags;
gchar** iter;
gboolean force_readonly;
root_dir = get_dir_from_address (address, err);
if (root_dir == NULL)
return NULL;
if (g_stat (root_dir, &statbuf) == 0)
{
/* Already exists, base our dir_mode on it */
dir_mode = _gconf_mode_t_to_mode (statbuf.st_mode);
/* dir_mode without search bits */
file_mode = dir_mode & (~0111);
}
else if (g_mkdir (root_dir, dir_mode) < 0)
{
/* Error out even on EEXIST - shouldn't happen anyway */
gconf_set_error (err, GCONF_ERROR_FAILED,
_("Could not make directory `%s': %s"),
(gchar *)root_dir, g_strerror (errno));
g_free (root_dir);
return NULL;
}
force_readonly = FALSE;
address_flags = gconf_address_flags (address);
if (address_flags)
{
iter = address_flags;
while (*iter)
{
if (strcmp (*iter, "readonly") == 0)
{
force_readonly = TRUE;
break;
}
++iter;
}
}
g_strfreev (address_flags);
{
/* See if we're writable */
gboolean writable;
int fd;
gchar* testfile;
writable = FALSE;
if (!force_readonly)
{
testfile = g_strconcat(root_dir, "/.testing.writeability", NULL);
fd = g_open(testfile, O_CREAT|O_WRONLY, S_IRWXU);
if (fd >= 0)
{
writable = TRUE;
close(fd);
}
g_unlink(testfile);
g_free(testfile);
}
if (writable)
flags |= GCONF_SOURCE_ALL_WRITEABLE;
#ifdef HAVE_CORBA
/* We only do locking if it's writable,
* and if not using local locks,
* which is sort of broken but close enough
*/
if (writable && !gconf_use_local_locks ())
{
gchar* lockdir;
lockdir = get_lock_dir_from_root_dir (root_dir);
lock = gconf_get_lock(lockdir, err);
if (lock != NULL)
gconf_log(GCL_DEBUG, "Acquired lock directory `%s'", lockdir);
g_free(lockdir);
if (lock == NULL)
{
g_free(root_dir);
return NULL;
}
}
#endif
}
{
/* see if we're readable */
gboolean readable = FALSE;
GDir* d;
d = g_dir_open(root_dir, 0, NULL);
if (d != NULL)
{
readable = TRUE;
g_dir_close(d);
}
if (readable)
flags |= GCONF_SOURCE_ALL_READABLE;
}
if (!(flags & GCONF_SOURCE_ALL_READABLE) &&
!(flags & GCONF_SOURCE_ALL_WRITEABLE))
{
gconf_set_error(err, GCONF_ERROR_BAD_ADDRESS, _("Can't read from or write to the XML root directory in the address \"%s\""), address);
g_free(root_dir);
return NULL;
}
/* Create the new source */
xsource = xs_new(root_dir, dir_mode, file_mode, lock);
gconf_log(GCL_DEBUG,
_("Directory/file permissions for XML source at root %s are: %o/%o"),
root_dir, dir_mode, file_mode);
source = (GConfSource*)xsource;
source->flags = flags;
g_free(root_dir);
return source;
}
static GConfValue*
query_value (GConfSource* source,
const gchar* key,
const gchar** locales,
gchar** schema_name,
GError** err)
{
XMLSource* xs = (XMLSource*)source;
gchar* parent;
Dir* dir;
GError* error = NULL;
parent = gconf_key_directory(key);
g_assert(parent != NULL);
dir = cache_lookup(xs->cache, parent, FALSE, &error);
/* We DO NOT want to return an error unless it represents a general
problem with the backend; since we don't want to report stuff
like "this key doesn't exist yet" - however this is a maintenance
problem, since some errors may be added that need reporting. */
if (error != NULL)
{
gconf_log(GCL_WARNING, "%s", error->message);
g_error_free(error);
error = NULL;
}
g_free(parent);
parent = NULL;
if (dir != NULL)
{
const gchar* relative_key;
GConfValue* retval;
relative_key = gconf_key_key(key);
retval = dir_get_value(dir, relative_key, locales, schema_name, &error);
/* perhaps we should be reporting this error... */
if (error != NULL)
{
gconf_log(GCL_WARNING, "%s", error->message);
g_error_free(error);
error = NULL;
}
return retval;
}
else
return NULL;
}
static GConfMetaInfo*
query_metainfo (GConfSource* source, const gchar* key,
GError** err)
{
XMLSource* xs = (XMLSource*)source;
gchar* parent;
Dir* dir;
parent = gconf_key_directory(key);
if (parent != NULL)
{
dir = cache_lookup(xs->cache, parent, FALSE, err);
g_free(parent);
parent = NULL;
if (dir != NULL)
{
const gchar* relative_key;
relative_key = gconf_key_key (key);
return dir_get_metainfo (dir, relative_key, err);
}
}
/* No metainfo found */
return NULL;
}
static void
set_value (GConfSource* source, const gchar* key, const GConfValue* value,
GError** err)
{
XMLSource* xs = (XMLSource*)source;
Dir* dir;
gchar* parent;
g_return_if_fail(value != NULL);
g_return_if_fail(source != NULL);
parent = gconf_key_directory(key);
g_assert(parent != NULL);
dir = cache_lookup(xs->cache, parent, TRUE, err);
g_free(parent);
parent = NULL;
if (dir == NULL)
{
g_return_if_fail((err == NULL || *err != NULL));
return;
}
else
{
const gchar* relative_key;
relative_key = gconf_key_key(key);
dir_set_value(dir, relative_key, value, err);
}
}
static GSList*
all_entries (GConfSource* source,
const gchar* key,
const gchar** locales,
GError** err)
{
XMLSource* xs = (XMLSource*)source;
Dir* dir;
dir = cache_lookup(xs->cache, key, FALSE, err);
if (dir == NULL)
return NULL;
else
return dir_all_entries(dir, locales, err);
}
static GSList*
all_subdirs (GConfSource* source,
const gchar* key,
GError** err)
{
Dir* dir;
XMLSource* xs = (XMLSource*)source;
GError *sync_err;
/* We have to sync before we can do this, to see which
* subdirs have gone away.
*/
sync_err = NULL;
cache_sync (xs->cache, &sync_err);
if (sync_err)
{
gconf_log (GCL_WARNING, _("Error syncing the XML backend directory cache: %s"),
sync_err->message);
g_error_free (sync_err);
sync_err = NULL;
/* continue, may as well try our best. */
}
dir = cache_lookup (xs->cache, key, FALSE, err);
if (dir == NULL)
return NULL;
else
return dir_all_subdirs (dir, err);
}
static void
unset_value (GConfSource* source,
const gchar* key,
const gchar* locale,
GError** err)
{
XMLSource* xs = (XMLSource*)source;
Dir* dir;
gchar* parent;
gconf_log(GCL_DEBUG, "XML backend: unset value `%s'", key);
parent = gconf_key_directory(key);
dir = cache_lookup(xs->cache, parent, FALSE, err);
g_free(parent);
if (dir == NULL)
return;
else
{
const gchar* relative_key;
relative_key = gconf_key_key(key);
dir_unset_value(dir, relative_key, locale, err);
}
}
static gboolean
dir_exists (GConfSource*source,
const gchar* key,
GError** err)
{
XMLSource *xs = (XMLSource*)source;
Dir* dir;
dir = cache_lookup(xs->cache, key, FALSE, err);
return (dir != NULL);
}
static void
remove_dir (GConfSource* source,
const gchar* key,
GError** err)
{
g_set_error (err, GCONF_ERROR,
GCONF_ERROR_FAILED,
_("Remove directory operation is no longer supported, just remove all the values in the directory"));
}
static void
set_schema (GConfSource *source,
const gchar *key,
const gchar *schema_key,
GError **err)
{
XMLSource* xs = (XMLSource*)source;
Dir* dir;
gchar* parent;
g_return_if_fail (source != NULL);
g_return_if_fail (key != NULL);
parent = gconf_key_directory (key);
g_assert (parent != NULL);
dir = cache_lookup (xs->cache, parent, TRUE, err);
g_free (parent);
parent = NULL;
if (dir == NULL)
return; /* error should be set */
else
{
const gchar* relative_key;
relative_key = gconf_key_key (key);
dir_set_schema (dir, relative_key, schema_key, err);
}
}
static gboolean
sync_all (GConfSource* source,
GError** err)
{
XMLSource* xs = (XMLSource*)source;
return cache_sync (xs->cache, err);
}
static void
destroy_source (GConfSource* source)
{
xs_destroy((XMLSource*)source);
}
static void
clear_cache (GConfSource* source)
{
XMLSource* xs = (XMLSource*)source;
/* clean all entries older than 0 seconds */
cache_clean(xs->cache, 0);
}
static void
blow_away_locks (const char *address)
{
char *root_dir;
char *lock_dir;
GDir *dp;
const char *dent;
/* /tmp locks should never be stuck, and possible security issue to
* blow them away
*/
if (gconf_use_local_locks ())
return;
root_dir = get_dir_from_address (address, NULL);
if (root_dir == NULL)
return;
lock_dir = get_lock_dir_from_root_dir (root_dir);
dp = g_dir_open (lock_dir, 0, NULL);
if (dp == NULL)
{
g_printerr (_("Could not open lock directory for %s to remove locks: %s\n"),
address, g_strerror (errno));
goto out;
}
while ((dent = g_dir_read_name (dp)) != NULL)
{
char *path;
path = g_build_filename (lock_dir, dent, NULL);
if (g_unlink (path) < 0)
{
g_printerr (_("Could not remove file %s: %s\n"),
path, g_strerror (errno));
}
g_free (path);
}
out:
if (dp)
g_dir_close (dp);
g_free (root_dir);
g_free (lock_dir);
}
/* Initializer */
#ifndef G_PLATFORM_WIN32
/* If we use G_MODULE_EXPORT, *only* thusly marked functions will be
* exported, and xml-test uses other ones, too.
*/
G_MODULE_EXPORT
#endif
const gchar*
g_module_check_init (GModule *module)
{
gconf_log(GCL_DEBUG, _("Initializing XML backend module"));
LIBXML_TEST_VERSION;
xmlKeepBlanksDefault(1);
return NULL;
}
#ifndef G_PLATFORM_WIN32
G_MODULE_EXPORT
#endif
GConfBackendVTable*
gconf_backend_get_vtable(void)
{
return &xml_vtable;
}
/* ****************************************************/
/*
* XMLSource
*/
/* This timeout periodically cleans up
the old cruft in the cache */
static gboolean
cleanup_timeout(gpointer data)
{
XMLSource* xs = (XMLSource*)data;
cache_clean(xs->cache, 60*5 /* 5 minutes */);
return TRUE;
}
static XMLSource*
xs_new (const gchar* root_dir, guint dir_mode, guint file_mode, GConfLock* lock)
{
XMLSource* xs;
g_return_val_if_fail(root_dir != NULL, NULL);
xs = g_new0(XMLSource, 1);
xs->root_dir = g_strdup(root_dir);
xs->cache = cache_get(xs->root_dir, dir_mode, file_mode);
xs->timeout_id = g_timeout_add_seconds(60*5, /* 1 sec * 60 s/min * 5 min */
cleanup_timeout,
xs);
xs->lock = lock;
xs->dir_mode = dir_mode;
xs->file_mode = file_mode;
return xs;
}
static void
xs_destroy (XMLSource* xs)
{
#ifdef HAVE_CORBA
GError* error = NULL;
#endif
g_return_if_fail(xs != NULL);
#ifdef HAVE_CORBA
/* do this first in case we're in a "fast cleanup just before exit"
situation */
if (xs->lock != NULL && !gconf_release_lock(xs->lock, &error))
{
gconf_log (GCL_ERR, _("Failed to give up lock on XML directory \"%s\": %s"),
xs->root_dir, error->message);
g_error_free(error);
error = NULL;
}
#endif
if (!g_source_remove(xs->timeout_id))
{
/* should not happen, don't translate */
gconf_log(GCL_ERR, "timeout not found to remove?");
}
cache_unref(xs->cache);
g_free(xs->root_dir);
g_free(xs);
}
|