Author: mkhl
Date: 2006-06-26 23:46:15 +0000 (Mon, 26 Jun 2006)
New Revision: 16538

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=16538

Log:
Add select_unmappable_msg_attrs func, which I'll need for mapping
parse-trees in search.
Fix some wrong talloc memory contexts in select_*.

Martin

Modified:
   branches/SOC/mkhl/ldb-map/modules/ldb_map.c


Changeset:
Modified: branches/SOC/mkhl/ldb-map/modules/ldb_map.c
===================================================================
--- branches/SOC/mkhl/ldb-map/modules/ldb_map.c 2006-06-26 23:36:03 UTC (rev 
16537)
+++ branches/SOC/mkhl/ldb-map/modules/ldb_map.c 2006-06-26 23:46:15 UTC (rev 
16538)
@@ -456,7 +456,10 @@
                return NULL;
 
        last = 0;
-       result = NULL;
+       result = talloc_array(mem_ctx, const char *, 1);
+       if (result == NULL)
+               return NULL;
+       result[0] = NULL;
 
        for (i = 0; attrs[i]; i++) {
                /* keep "*" and ignored attrs */
@@ -467,7 +470,7 @@
                        if (result == NULL)
                                goto failed;
 
-                       result[last] = talloc_strdup(mem_ctx, attrs[i]);
+                       result[last] = talloc_strdup(result, attrs[i]);
                        result[last+1] = NULL;
                        last++;
                }
@@ -497,12 +500,15 @@
                return NULL;
 
        last = 0;
-       result = NULL;
+       result = talloc_array(mem_ctx, const char *, 1);
+       if (result == NULL)
+               return NULL;
+       result[0] = NULL;
 
        for (i = 0; attrs[i]; i++) {
                /* keep "*" as is */
                if (ldb_attr_cmp(attrs[i], "*") == 0) {
-                       name = talloc_strdup(mem_ctx, attrs[i]);
+                       name = talloc_strdup(result, attrs[i]);
                        goto named;
                }
 
@@ -529,7 +535,7 @@
                        for (j = 0; map->u.generate.remote_names[j]; j++) {
                                result = talloc_realloc(mem_ctx, result,
                                                        const char *, last+2);
-                               result[last] = talloc_strdup(mem_ctx, map->u 
.generate.remote_names[j]);
+                               result[last] = talloc_strdup(result, map->u 
.generate.remote_names[j]);
                                result[last+1] = NULL;
                                last++;
                        }
@@ -542,7 +548,7 @@
                if (result == NULL)
                        goto failed;
 
-               result[last] = name;
+               result[last] = talloc_strdup(result, name);
                result[last+1] = NULL;
                last++;
        }
@@ -554,6 +560,68 @@
        return NULL;
 }
 
+/* select only local attrs that can be unmapped from msg */
+static
+const char **
+select_unmappable_msg_attrs(struct ldb_module *module,
+                           void *mem_ctx,
+                           const struct ldb_message *msg)
+{
+       struct ldb_map_context *data = map_get_context(module);
+       const struct ldb_map_attribute *map;
+       const char **result;
+       int i, j, last;
+
+       if (msg == NULL)
+               return NULL;
+
+       last = 0;
+       result = talloc_array(mem_ctx, const char *, 1);
+       if (result == NULL)
+               return NULL;
+       result[0] = NULL;
+
+       /* for each mapping, check if all remote attributes are present
+          if they are, add the local one to the result */
+       /* TODO: Alternatively, walk over msg->elements and use 
find_remote_attr. */
+       for (i = 0; data->attribute_maps[i].local_name; i++) {
+               BOOL avail = False;
+               map = &data->attribute_maps[i];
+
+               switch (map->type) {
+               case MAP_IGNORE:
+                       break;
+
+               case MAP_KEEP:
+                       avail = (ldb_msg_find_element(msg, map->local_name) != 
NULL);
+                       break;
+                               
+               case MAP_RENAME:
+               case MAP_CONVERT:
+                       avail = (ldb_msg_find_element(msg, 
map->u.rename.remote_name) != NULL);
+                       break;
+
+               case MAP_GENERATE:
+                       /* look for *all* remote names */
+                       avail = True;
+                       for (j = 0; map->u.generate.remote_names[j]; j++)
+                               avail &= (ldb_msg_find_element(msg, 
map->u.generate.remote_names[j]) != NULL);
+                       break;
+               }
+
+               if (!avail)
+                       continue;
+
+               result = talloc_realloc(mem_ctx, result, const char *, last+2);
+               result[last] = talloc_strdup(result, map->local_name);
+               result[last+1] = NULL;
+               last++;
+       }
+
+       return result;
+}
+
+
 /* Check whether the given objectClass is contained in the specified
  * message */
 /*
@@ -909,69 +977,7 @@
        return ldb_val_dup(mem_ctx, val); 
 }
 
-/* /\* XXX *\/ */
-/* /\* Remote message -> List of local attribute names of which all remote */
-/*  * attributes are present in the message *\/ */
-/* static const char **mop_msg_available_local_attrs(struct ldb_module 
*module, */
-/*                                               const struct ldb_message 
*msg) */
-/* { */
-/*     struct ldb_map_context *map = map_get_context(module); */
-/*     const char **names; */
-/*     const struct ldb_map_attribute *attr; */
-/*     int count = 0; */
-/*     int i, j; */
 
-/*     names = talloc_array(module, const char *, 1); */
-/*     if (names == NULL) */
-/*             return NULL; */
-
-/*     names[0] = NULL; */
-
-/*     for (i = 0; map->attribute_maps[i].local_name; i++) { */
-/*             BOOL avail = False; */
-/*             attr = &map->attribute_maps[i]; */
-/*             if (attr == NULL) { */
-/*                     ldb_debug(module->ldb, LDB_DEBUG_TRACE, "ldb_map: " */
-/*                               "NULL-Pointer found in attribute maps!\n"); */
-/*                     /\* Can't ever happen, right? *\/ */
-/*                     continue; */
-/*             } */
-
-/*             /\* If all remote attributes for this attribute are */
-/*              * present, add the local one to the list *\/ */
-/*             switch (attr->type) { */
-/*             case MAP_IGNORE: */
-/*                     break; */
-
-/*             case MAP_KEEP:  */
-/*                     avail = (ldb_msg_find_ldb_val(msg, attr->local_name) != 
NULL); */
-/*                     break; */
-                               
-/*             case MAP_RENAME: */
-/*             case MAP_CONVERT: */
-/*                     avail = (ldb_msg_find_ldb_val(msg, 
attr->u.rename.remote_name) != NULL); */
-/*                     break; */
-
-/*             case MAP_GENERATE: */
-/*                     avail = True; */
-/*                     for (j = 0; attr->u.generate.remote_names[j]; j++) */
-/*                             avail &= (BOOL)(ldb_msg_find_ldb_val(msg, 
attr->u.generate.remote_names[j]) != NULL); */
-/*                     break; */
-/*             } */
-
-/*             if (!avail) */
-/*                     continue; */
-
-/*             names = talloc_realloc(module, names, const char *, count+2); */
-/*             names[count] = attr->local_name; */
-/*             names[count+1] = NULL; */
-/*             count++; */
-/*     } */
-
-/*     return names; */
-/* } */
-
-
 /* Mapping data structures */
 
 /* /\* XXX *\/ */
@@ -1150,62 +1156,6 @@
 /*     return NULL; */
 /* } */
 
-/* /\* XXX *\/ */
-/* /\* Remote message -> Local message *\/ */
-/* static struct ldb_message *mop_ldb_message_incoming(struct ldb_module 
*module, */
-/*                                                 const char *const names[], 
*/
-/*                                                 const struct ldb_message 
*inmsg) */
-/* { */
-/*     struct ldb_map_context *map = map_get_context(module); */
-/*     struct ldb_message *msg; */
-/*     struct ldb_message_element *el; */
-/*     const struct ldb_map_attribute *attr; */
-/*     int i; */
-
-/*     if (names == NULL) */
-/*             /\* Generate list of the local attributes that *can* be */
-/*              * generated using the specific remote attributes *\/ */
-/*             return map_ldb_message_incoming(module, 
map_msg_available_local_attrs(module, inmsg), inmsg); */
-
-
-/*     msg = talloc_zero(module, struct ldb_message); */
-/*     if (msg == NULL) */
-/*             return NULL; */
-
-/*     msg->dn = map_remote_dn(module, module, inmsg->dn); */
-/*     if (msg->dn == NULL) */
-/*             goto failed; */
-
-/*     /\* Map each of the specified attributes *\/ */
-/*     for (i = 0; names[i]; i++) { */
-/*             attr = find_attr_local(map, names[i]); */
-/*             if (attr == NULL) { */
-/*                     ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: " */
-/*                               "Unable to find local attribute '%s' " */
-/*                               "when generating incoming message\n", */
-/*                               names[i]); */
-/*                     continue; */
-/*             } */
-
-/*             el = map_remote_msg_element(module, msg, inmsg, attr); */
-/*             if (el == NULL) { */
-/*                     ldb_debug(module->ldb, LDB_DEBUG_WARNING, "ldb_map: " */
-/*                               "Unable to convert local attribute '%s' " */
-/*                               "when generating incoming message\n", */
-/*                               names[i]); */
-/*                     continue; */
-/*             } */
-
-/*             ldb_msg_add(msg, el, el->flags); */
-/*     } */
-
-/*     return msg; */
-
-/* failed: */
-/*     talloc_free(msg); */
-/*     return NULL; */
-/* } */
-
 /* add element to message, overwriting old elements of the same name */
 static
 int
@@ -1478,8 +1428,17 @@
 
        return 0;
 }
+/* TODO: from map_ldb_message_incoming:
+   (names was passed in as the list of local names to generate)
 
+       if (names == NULL)
+               /\* Generate list of the local attributes that *can* be
+                * generated using the specific remote attributes *\/
+               return map_ldb_message_incoming(module, 
map_msg_available_local_attrs(module, inmsg), inmsg);
 
+ */
+
+
 /* store single search result in async context */
 static
 int

Reply via email to