Browse Source

Trac Ticket #453 - db2index with -tattrname:type,type fails

https://fedorahosted.org/389/ticket/453

Fix Description:
1) db2index and db2index.pl did not handle options with no space
between the option type and the value.  This patch properly parses
them.
2) db2index_add_indexed_attr now converts the comma delimited list
of index types into multiple values for the nsIndexType and
nsMatchingRule.
Noriko Hosoi 13 years ago
parent
commit
3c6756f9fa

+ 1 - 1
ldap/admin/src/scripts/template-db2index.in

@@ -23,7 +23,7 @@ if [ $# -eq 0 ]
 then
 	bak_dir={{BAK-DIR}}/reindex_`date +%Y_%m_%d_%H_%M_%S`
 	./ns-slapd upgradedb -D {{CONFIG-DIR}} -f -a "$bak_dir"
-elif [ $# -lt 4 ]
+elif [ $# -lt 2 ]
 then
 	echo "Usage: db2index [-n backend_instance | {-s includesuffix}* -t attribute[:indextypes[:matchingrules]] -T vlvattribute]"
 	exit 1

+ 35 - 54
ldap/admin/src/scripts/template-db2index.pl.in

@@ -39,6 +39,13 @@
 # END COPYRIGHT BLOCK
 #
 
+use Getopt::Std;
+
+$opt_D = "{{ROOT-DN}}";
+$opt_p = "{{SERVER-PORT}}";
+$opt_h = "{{SERVER-NAME}}";
+$opt_v = 0;
+
 sub usage {
     print(STDERR "Usage: $0 [-v] -D rootdn { -w password | -w - | -j filename } \n");
     print(STDERR "        -n instance [-t attributeName[:indextypes[:matchingrules]]]\n");
@@ -56,6 +63,8 @@ sub usage {
     print(STDERR "                           Example: -t foo:eq,pres\n");
     print(STDERR "     : -T vlvAttributeName - vlvAttributeName: name of the vlv attribute to be indexed\n");
     print(STDERR "     : -v                  - verbose\n");
+
+    exit 1;
 }
 
 $instance = "";
@@ -77,51 +86,25 @@ libpath_add("/usr/lib");
 
 $ENV{'SHLIB_PATH'} = "$ENV{'LD_LIBRARY_PATH'}";
 
-$i = 0;
-while ($i <= $#ARGV) 
-{
-    if ("$ARGV[$i]" eq "-n")
-    {    
-        # instance
-        $i++; $instance = $ARGV[$i];
-    }
-    elsif ("$ARGV[$i]" eq "-D") 
-    {    
-        # Directory Manager
-        $i++; $rootdn = $ARGV[$i];
-    }
-    elsif ("$ARGV[$i]" eq "-w") 
-    {    
-        # Directory Manager's password
-        $i++; $passwd = $ARGV[$i];
-    } 
-    elsif ("$ARGV[$i]" eq "-j")
-    {
-         # Read Directory Manager's password from a file
-        $i++; $passwdfile = $ARGV[$i];
-    }
-    elsif ("$ARGV[$i]" eq "-t") 
-    {    
-        # Attribute to index
-        $i++; $attribute_arg = $ARGV[$i];
-    }
-    elsif ("$ARGV[$i]" eq "-T") 
-    {    
-        # Vlvattribute to index
-        $i++; $vlvattribute_arg = $ARGV[$i];
-    }
-    elsif ("$ARGV[$i]" eq "-v") 
-    {    
-        # verbose
-        $verbose = 1;
-    }
-    else
-    {
-        &usage; exit(1);
-    }
-    $i++;
+usage() if (!getopts('n:D:w:T:t:j:v'));
+
+if ($opt_j ne ""){
+    die "Error, cannot open password file $opt_j\n" unless (open (RPASS, $opt_j));
+    $opt_w = <RPASS>;
+    chomp($opt_w);
+    close(RPASS);
 }
 
+usage() if( $opt_w eq "" );
+usage() if( $#ARGV ge 0 );
+
+$instance = $opt_n;
+$rootdn = $opt_D;
+$passwd = $opt_w;
+$attribute_arg = $opt_t;
+$vlvattribute_arg = $opt_T;
+$verbose = $opt_v;
+
 if ($passwdfile ne ""){
 # Open file and get the password
     unless (open (RPASS, $passwdfile)) {
@@ -132,21 +115,20 @@ if ($passwdfile ne ""){
     close(RPASS);
 } elsif ($passwd eq "-"){
 # Read the password from terminal
-	print "Bind Password: ";
-	# Disable console echo
-	system("@sttyexec@ -echo") if -t STDIN;
-	# read the answer
-	$passwd = <STDIN>;
-	# Enable console echo
-	system("@sttyexec@ echo") if -t STDIN;
-	print "\n";
-	chop($passwd); # trim trailing newline
+    print "Bind Password: ";
+    # Disable console echo
+    system("@sttyexec@ -echo") if -t STDIN;
+    # read the answer
+    $passwd = <STDIN>;
+    # Enable console echo
+    system("@sttyexec@ echo") if -t STDIN;
+    print "\n";
+    chop($passwd); # trim trailing newline
 }
 
 if ( $rootdn eq "" || $passwd eq "" ) 
 { 
     &usage; 
-    exit(1); 
 }
 
 $vstr = "";
@@ -162,7 +144,6 @@ $taskname = "db2index_${yr}_${mn}_${dy}_${h}_${m}_${s}";
 if ( $instance eq "" )
 {
     &usage;
-    exit(1);
 }
 
 # No attribute name has been specified: let's get them from the configuration

+ 19 - 7
ldap/servers/slapd/back-ldbm/ldif2ldbm.c

@@ -2494,6 +2494,8 @@ db2index_add_indexed_attr(backend *be, char *attrString)
     Slapi_Entry *e;
     struct berval *vals[2];
     struct berval val;
+    char *ptr;
+    char *next;
 
     vals[0] = &val;
     vals[1] = NULL;
@@ -2508,6 +2510,7 @@ db2index_add_indexed_attr(backend *be, char *attrString)
     /* set the index name */
     val.bv_val = attrString+1;
     val.bv_len = strlen(attrString);
+    /* bv_val is copied into the entry. */
     slapi_entry_add_values(e,"cn",vals);
 
     if (NULL != (mptr = strchr(iptr, ':'))) {
@@ -2516,15 +2519,24 @@ db2index_add_indexed_attr(backend *be, char *attrString)
     }
 
     /* set the index type */
-    val.bv_val = iptr;
-    val.bv_len = strlen(iptr);
-    slapi_entry_add_values(e,"nsIndexType",vals);
+    for (ptr = strtok_r(iptr, ", ", &next);
+         ptr;
+         ptr = strtok_r(NULL, ", ", &next)) {
+        val.bv_val = ptr;
+        val.bv_len = strlen(ptr);
+        /* bv_val is copied into the entry. */
+        slapi_entry_add_values(e,"nsIndexType",vals);
+    }
 
     if (NULL != mptr) {
-        /* set the matching rule */
-        val.bv_val = mptr;
-        val.bv_len = strlen(mptr);
-        slapi_entry_add_values(e,"nsMatchingRule",vals);
+        for (ptr = strtok_r(mptr, ", ", &next);
+             ptr;
+             ptr = strtok_r(NULL, ", ", &next)) {
+            val.bv_val = ptr;
+            val.bv_len = strlen(ptr);
+            /* bv_val is copied into the entry. */
+            slapi_entry_add_values(e,"nsMatchingRule",vals);
+        }
     }
 
     attr_index_config(be, "from db2index()", 0, e, 0, 0);