registry: refactor creation of subkeys out of init_registry_data().
authorMichael Adam <obnox@samba.org>
Thu, 20 Mar 2008 12:59:09 +0000 (13:59 +0100)
committerMichael Adam <obnox@samba.org>
Thu, 20 Mar 2008 14:01:51 +0000 (15:01 +0100)
Michael

source/registry/reg_backend_db.c

index 07aaf68d50af4b5afff7b439ac29eecd90556899..fc7d2bc2cf07d925f203673b6f1d0b9793690d25 100644 (file)
@@ -87,115 +87,128 @@ static struct builtin_regkey_value builtin_registry_values[] = {
        { NULL, NULL, 0, { NULL } }
 };
 
-/***********************************************************************
- Open the registry data in the tdb
- ***********************************************************************/
-
-static bool init_registry_data(void)
+/**
+ * Initialize a key in the registry:
+ * create each component key of the specified path.
+ */
+static bool init_registry_key_internal(const char *add_path)
 {
+       bool ret = false;
+       TALLOC_CTX *frame = talloc_stackframe();
        char *path = NULL;
        char *base = NULL;
        char *remaining = NULL;
-       TALLOC_CTX *frame = NULL;
        char *keyname;
        char *subkeyname;
        REGSUBKEY_CTR *subkeys;
-       REGVAL_CTR *values;
-       int i;
        const char *p, *p2;
-       UNISTR2 data;
 
-       /*
-        * There are potentially quite a few store operations which are all
-        * indiviually wrapped in tdb transactions. Wrapping them in a single
-        * transaction gives just a single transaction_commit() to actually do
-        * its fsync()s. See tdb/common/transaction.c for info about nested
-        * transaction behaviour.
-        */
+       DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
 
-       if (regdb->transaction_start(regdb) == -1) {
-               DEBUG(0, ("init_registry_data: tdb_transaction_start "
-                         "failed\n"));
-               return false;
+       path = talloc_strdup(frame, add_path);
+       base = talloc_strdup(frame, "");
+       if (!path || !base) {
+               goto fail;
        }
+       p = path;
 
-       /* loop over all of the predefined paths and add each component */
-
-       for (i=0; builtin_registry_paths[i] != NULL; i++) {
-
-               frame = talloc_stackframe();
+       while (next_token_talloc(frame, &p, &keyname, "\\")) {
 
-               DEBUG(6, ("init_registry_data: Adding [%s]\n",
-                         builtin_registry_paths[i]));
+               /* build up the registry path from the components */
 
-               path = talloc_strdup(frame, builtin_registry_paths[i]);
-               base = talloc_strdup(frame, "");
-               if (!path || !base) {
-                       goto fail;
-               }
-               p = path;
-
-               while (next_token_talloc(frame, &p, &keyname, "\\")) {
-
-                       /* build up the registry path from the components */
-
-                       if (*base) {
-                               base = talloc_asprintf(frame, "%s\\", base);
-                               if (!base) {
-                                       goto fail;
-                               }
-                       }
-                       base = talloc_asprintf_append(base, "%s", keyname);
+               if (*base) {
+                       base = talloc_asprintf(frame, "%s\\", base);
                        if (!base) {
                                goto fail;
                        }
+               }
+               base = talloc_asprintf_append(base, "%s", keyname);
+               if (!base) {
+                       goto fail;
+               }
 
-                       /* get the immediate subkeyname (if we have one ) */
+               /* get the immediate subkeyname (if we have one ) */
 
-                       subkeyname = talloc_strdup(frame, "");
-                       if (!subkeyname) {
+               subkeyname = talloc_strdup(frame, "");
+               if (!subkeyname) {
+                       goto fail;
+               }
+               if (*p) {
+                       remaining = talloc_strdup(frame, p);
+                       if (!remaining) {
                                goto fail;
                        }
-                       if (*p) {
-                               remaining = talloc_strdup(frame, p);
-                               if (!remaining) {
+                       p2 = remaining;
+
+                       if (!next_token_talloc(frame, &p2,
+                                               &subkeyname, "\\"))
+                       {
+                               subkeyname = talloc_strdup(frame,p2);
+                               if (!subkeyname) {
                                        goto fail;
                                }
-                               p2 = remaining;
-
-                               if (!next_token_talloc(frame, &p2,
-                                                       &subkeyname, "\\"))
-                               {
-                                       subkeyname = talloc_strdup(frame,p2);
-                                       if (!subkeyname) {
-                                               goto fail;
-                                       }
-                               }
                        }
+               }
 
-                       DEBUG(10,("init_registry_data: Storing key [%s] with "
-                                 "subkey [%s]\n", base,
-                                 *subkeyname ? subkeyname : "NULL"));
+               DEBUG(10,("init_registry_key: Storing key [%s] with "
+                         "subkey [%s]\n", base,
+                         *subkeyname ? subkeyname : "NULL"));
 
-                       /* we don't really care if the lookup succeeds or not
-                        * since we are about to update the record.
-                        * We just want any subkeys already present */
+               /* we don't really care if the lookup succeeds or not
+                * since we are about to update the record.
+                * We just want any subkeys already present */
 
-                       if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
-                               DEBUG(0,("talloc() failure!\n"));
-                               goto fail;
-                       }
+               if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
+                       DEBUG(0,("talloc() failure!\n"));
+                       goto fail;
+               }
 
-                       regdb_fetch_keys(base, subkeys);
-                       if (*subkeyname) {
-                               regsubkey_ctr_addkey( subkeys, subkeyname);
-                       }
-                       if (!regdb_store_keys( base, subkeys)) {
-                               goto fail;
-                       }
+               regdb_fetch_keys(base, subkeys);
+               if (*subkeyname) {
+                       regsubkey_ctr_addkey( subkeys, subkeyname);
                }
+               if (!regdb_store_keys( base, subkeys)) {
+                       goto fail;
+               }
+       }
+
+       ret = true;
+fail:
+       TALLOC_FREE(frame);
+       return ret;
+}
+
+/***********************************************************************
+ Open the registry data in the tdb
+ ***********************************************************************/
+
+static bool init_registry_data(void)
+{
+       TALLOC_CTX *frame = NULL;
+       REGVAL_CTR *values;
+       int i;
+       UNISTR2 data;
+
+       /*
+        * There are potentially quite a few store operations which are all
+        * indiviually wrapped in tdb transactions. Wrapping them in a single
+        * transaction gives just a single transaction_commit() to actually do
+        * its fsync()s. See tdb/common/transaction.c for info about nested
+        * transaction behaviour.
+        */
 
-               TALLOC_FREE(frame);
+       if (regdb->transaction_start(regdb) == -1) {
+               DEBUG(0, ("init_registry_data: tdb_transaction_start "
+                         "failed\n"));
+               return false;
+       }
+
+       /* loop over all of the predefined paths and add each component */
+
+       for (i=0; builtin_registry_paths[i] != NULL; i++) {
+               if (!init_registry_key_internal(builtin_registry_paths[i])) {
+                       goto fail;
+               }
        }
 
        /* loop over all of the predefined values and add each component */