r4137: Make *_open_key take a registry_key instead of a hive (more efficient
authorJelmer Vernooij <jelmer@samba.org>
Fri, 10 Dec 2004 22:28:49 +0000 (22:28 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:06:30 +0000 (13:06 -0500)
in some cases)
(This used to be commit ddf7a331c58976f2c0b4a00390118f1acf60e3eb)

source4/include/registry.h
source4/lib/registry/common/reg_interface.c
source4/lib/registry/reg_backend_dir.c
source4/lib/registry/reg_backend_gconf.c
source4/lib/registry/reg_backend_ldb.c
source4/lib/registry/reg_backend_rpc.c

index 4ce95693768af256112e78e4d4b25a593c88a14d..3e38faa77216628657c4834cd4b866a8d38dbf03 100644 (file)
@@ -114,7 +114,7 @@ struct hive_operations {
        WERROR (*close_hive) (struct registry_hive *);
 
        /* Or this one */
-       WERROR (*open_key) (TALLOC_CTX *, struct registry_hive *, const char *name, struct registry_key **);
+       WERROR (*open_key) (TALLOC_CTX *, struct registry_key *, const char *name, struct registry_key **);
 
        /* Either implement these */
        WERROR (*num_subkeys) (struct registry_key *, int *count);
index a93fa3183665cf18eae7b3143c53f9fadadd0ed2..2382e4aa434f7c4f0442092c8a3d3c0a1e9539bc 100644 (file)
@@ -218,7 +218,6 @@ WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle, co
  */
 WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, struct registry_key **result)
 {
-       char *fullname;
        WERROR error;
 
        if(!parent) {
@@ -257,15 +256,12 @@ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char
                return WERR_NOT_SUPPORTED;
        }
 
-
-       fullname = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
-
-       error = parent->hive->functions->open_key(mem_ctx, parent->hive, fullname, result);
+       error = parent->hive->functions->open_key(mem_ctx, parent, name, result);
 
        if(!W_ERROR_IS_OK(error)) return error;
                
        (*result)->hive = parent->hive;
-       (*result)->path = fullname;
+       (*result)->path = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
        (*result)->hive = parent->hive;
 
        return WERR_OK;
@@ -369,7 +365,7 @@ WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx, struct registry_key *key,
        if(key->hive->functions->get_subkey_by_name) {
                error = key->hive->functions->get_subkey_by_name(mem_ctx, key,name,subkey);
        } else if(key->hive->functions->open_key) {
-               error = key->hive->functions->open_key(mem_ctx, key->hive, talloc_asprintf(mem_ctx, "%s\\%s", key->path, name), subkey);
+               error = key->hive->functions->open_key(mem_ctx, key, name, subkey);
        } else if(key->hive->functions->get_subkey_by_index) {
                for(i = 0; W_ERROR_IS_OK(error); i++) {
                        error = reg_key_get_subkey_by_index(mem_ctx, key, i, subkey);
index 5c3ed3c44ca9573b8dc0fc1e7698707125c30513..c004be5a064ebbcbae1fd8e093898de8a3b84c41 100644 (file)
@@ -39,10 +39,10 @@ static WERROR reg_dir_del_key(struct registry_key *k)
        return (rmdir((char *)k->backend_data) == 0)?WERR_OK:WERR_GENERAL_FAILURE;
 }
 
-static WERROR reg_dir_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **subkey)
+static WERROR reg_dir_open_key(TALLOC_CTX *mem_ctx, struct registry_key *p, const char *name, struct registry_key **subkey)
 {
        DIR *d;
-       char *fullpath;
+       char *fullpath, *unixpath;
        struct registry_key *ret;
        
        if(!name) {
@@ -51,18 +51,19 @@ static WERROR reg_dir_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, con
        }
 
        
-       fullpath = talloc_asprintf(mem_ctx, "%s%s", h->location, name);
-       fullpath = reg_path_win2unix(fullpath);
+       fullpath = talloc_asprintf(mem_ctx, "%s/%s", (char *)p->backend_data, name);
+       unixpath = reg_path_win2unix(fullpath);
        
-       d = opendir(fullpath);
+       d = opendir(unixpath);
        if(!d) {
-               DEBUG(3,("Unable to open '%s': %s\n", fullpath, strerror(errno)));
+               DEBUG(3,("Unable to open '%s': %s\n", unixpath, strerror(errno)));
                return WERR_BADFILE;
        }
        closedir(d);
        ret = talloc_p(mem_ctx, struct registry_key);
-       ret->hive = h;
+       ret->hive = p->hive;
        ret->path = fullpath;
+       ret->backend_data = unixpath;
        *subkey = ret;
        return WERR_OK;
 }
@@ -89,7 +90,6 @@ static WERROR reg_dir_key_by_index(TALLOC_CTX *mem_ctx, struct registry_key *k,
                        stat(thispath, &stbuf);
 
                        if(S_ISDIR(stbuf.st_mode)) {
-                               i++;
                                if(i == idx) {
                                        (*key) = talloc_p(mem_ctx, struct registry_key);
                                        (*key)->name = e->d_name;
@@ -99,6 +99,7 @@ static WERROR reg_dir_key_by_index(TALLOC_CTX *mem_ctx, struct registry_key *k,
                                        closedir(d);
                                        return WERR_OK;
                                }
+                               i++;
                        }
 
                        SAFE_FREE(thispath);
index 2fed2417b77e96919f7d7fec27fd0d0bcd6b7c97..b53d0fedf5028675ad12f0fd1fee39074afdd19a 100644 (file)
@@ -42,15 +42,18 @@ static WERROR reg_open_gconf_hive(struct registry_hive *h, struct registry_key *
        return WERR_OK;
 }
 
-static WERROR gconf_open_key (TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key) 
+static WERROR gconf_open_key (TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key) 
 {
        struct registry_key *ret;
        char *fullpath;
        
-       fullpath = talloc_asprintf(mem_ctx, "/%s", reg_path_win2unix(talloc_strdup(mem_ctx, name)));
+       fullpath = talloc_asprintf(mem_ctx, "%s%s%s", 
+                                                          (char *)h->backend_data, 
+                                                          strlen((char *)h->backend_data) == 1?"":"/",
+                                                          reg_path_win2unix(talloc_strdup(mem_ctx, name)));
 
        /* Check if key exists */
-       if(!gconf_client_dir_exists((GConfClient *)h->backend_data, fullpath, NULL)) {
+       if(!gconf_client_dir_exists((GConfClient *)h->hive->backend_data, fullpath, NULL)) {
                return WERR_DEST_NOT_FOUND;
        }
 
index e833299b7eb70943e8784d91eb28602e55459716..7804a68d3d7a504b3f472727b8b66e0a17025941 100644 (file)
@@ -35,8 +35,15 @@ static int reg_close_ldb_key (void *data)
        struct ldb_key_data *kd = key->backend_data;
        struct ldb_context *c = key->hive->backend_data;
 
-       ldb_search_free(c, kd->subkeys); kd->subkeys = NULL;
-       ldb_search_free(c, kd->values); kd->values = NULL;
+       if (kd->subkeys) {
+               ldb_search_free(c, kd->subkeys); 
+               kd->subkeys = NULL;
+       }
+
+       if (kd->values) {
+               ldb_search_free(c, kd->values); 
+               kd->values = NULL;
+       }
        return 0;
 }
 
@@ -85,7 +92,7 @@ static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k,
 
        /* Do a search if necessary */
        if (kd->subkeys == NULL) {
-               kd->subkey_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&kd->subkeys);
+               kd->subkey_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &kd->subkeys);
 
                if(kd->subkey_count < 0) {
                        DEBUG(0, ("Error getting subkeys for '%s': %s\n", kd->dn, ldb_errstring(c)));
@@ -133,14 +140,17 @@ static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, i
        return WERR_OK;
 }
 
-static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key)
+static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
 {
-       struct ldb_context *c = h->backend_data;
+       struct ldb_context *c = h->hive->backend_data;
        struct ldb_message **msg;
        char *ldap_path;
        int ret;
-       struct ldb_key_data *newkd;
-       ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
+       struct ldb_key_data *kd = h->backend_data, *newkd;
+       ldap_path = talloc_asprintf(mem_ctx, "%s%s%s", 
+                                                               reg_path_to_ldb(mem_ctx, name, NULL), 
+                                                               kd->dn?",":"",
+                                                               kd->dn?kd->dn:"");
 
        ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
 
@@ -153,7 +163,7 @@ static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const c
 
        *key = talloc_p(mem_ctx, struct registry_key);
        talloc_set_destructor(*key, reg_close_ldb_key);
-       (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\'));
+       (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
        (*key)->backend_data = newkd = talloc_zero_p(*key, struct ldb_key_data);
        newkd->dn = talloc_strdup(mem_ctx, msg[0]->dn); 
 
@@ -165,6 +175,7 @@ static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const c
 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
 {
        struct ldb_context *c;
+       struct ldb_key_data *kd;
 
        if (!hive->location) return WERR_INVALID_PARAM;
        c = ldb_connect(hive->location, 0, NULL);
@@ -179,7 +190,8 @@ static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
        hive->root = talloc_zero_p(hive, struct registry_key);
        talloc_set_destructor (hive->root, reg_close_ldb_key);
        hive->root->name = talloc_strdup(hive->root, "");
-       hive->root->backend_data = talloc_zero_p(hive->root, struct ldb_key_data);
+       hive->root->backend_data = kd = talloc_zero_p(hive->root, struct ldb_key_data);
+       kd->dn = talloc_strdup(hive->root, "key=root");
        
 
        return WERR_OK;
index 18563b2478025f89c03b0010324e1bb09edf84da..927ed7fcaac8c76b7f26cffd995cad9147d7d064 100644 (file)
@@ -155,7 +155,7 @@ static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
 }
 #endif
 
-static WERROR rpc_open_rel_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
+static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
 {
        struct rpc_key_data *mykeydata;
     struct winreg_OpenKey r;
@@ -181,11 +181,6 @@ static WERROR rpc_open_rel_key(TALLOC_CTX *mem_ctx, struct registry_key *h, cons
        return r.out.result;
 }
 
-static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key)
-{
-       return rpc_open_rel_key(mem_ctx, h->root, name, key);
-}
-
 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)  
 {
        struct rpc_key_data *mykeydata = parent->backend_data;
@@ -258,7 +253,7 @@ static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *
        r.in.key_name_len = r.out.key_name_len = 0;
        status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
        if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
-                       return rpc_open_rel_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->name), subkey);
+                       return rpc_open_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->name), subkey);
        }
 
        return r.out.result;