X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=servers%2Fslapd%2Fslapadd.c;h=d975034313f1696bcc9b29f647347dcab4f2fc96;hb=6d1aa51604a7ae9c44287af5201afc9f43b5e91e;hp=67012911069b30d823febb9ea7006cd7865fa7be;hpb=2132aa3b16d2916f0c7a498fc6fc6b458c174d14;p=openldap diff --git a/servers/slapd/slapadd.c b/servers/slapd/slapadd.c index 6701291106..d975034313 100644 --- a/servers/slapd/slapadd.c +++ b/servers/slapd/slapadd.c @@ -1,7 +1,7 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 1998-2007 The OpenLDAP Foundation. + * Copyright 1998-2009 The OpenLDAP Foundation. * Portions Copyright 1998-2003 Kurt D. Zeilenga. * Portions Copyright 2003 IBM Corporation. * All rights reserved. @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include "slapcommon.h" @@ -52,7 +54,6 @@ slapadd( int argc, char **argv ) struct berval csn; struct berval maxcsn[ SLAP_SYNC_SID_MAX + 1 ]; - MatchingRule *mr_csnsid; unsigned long sid; struct berval bvtext; Attribute *attr; @@ -68,6 +69,14 @@ slapadd( int argc, char **argv ) int rc = EXIT_SUCCESS; int manage = 0; + int enable_meter = 0; + lutil_meter_t meter; + struct stat stat_buf; + + /* default "000" */ + csnsid = 0; + + if ( isatty (2) ) enable_meter = 1; slap_tool_init( progname, SLAPADD, argc, argv ); memset( &opbuf, 0, sizeof(opbuf) ); @@ -97,6 +106,11 @@ slapadd( int argc, char **argv ) lmax = 0; nextline = 0; + /* enforce schema checking unless not disabled */ + if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) { + SLAP_DBFLAGS(be) &= ~(SLAP_DBFLAG_NO_SCHEMA_CHECK); + } + if( !dryrun && be->be_entry_open( be, 1 ) != 0 ) { fprintf( stderr, "%s: could not open database.\n", progname ); @@ -104,8 +118,6 @@ slapadd( int argc, char **argv ) } if ( update_ctxcsn ) { - mr_csnsid = mr_find( "CSNSIDMatch" ); - assert( mr_csnsid != NULL ); maxcsn[ 0 ].bv_val = maxcsnbuf; for ( sid = 1; sid <= SLAP_SYNC_SID_MAX; sid++ ) { maxcsn[ sid ].bv_val = maxcsn[ sid - 1 ].bv_val + LDAP_LUTIL_CSNSTR_BUFSIZE; @@ -113,6 +125,22 @@ slapadd( int argc, char **argv ) } } + if ( enable_meter +#ifdef LDAP_DEBUG + /* tools default to "none" */ + && slap_debug == LDAP_DEBUG_NONE +#endif + && !fstat ( fileno ( ldiffp->fp ), &stat_buf ) + && S_ISREG(stat_buf.st_mode) ) { + enable_meter = !lutil_meter_open( + &meter, + &lutil_meter_text_display, + &lutil_meter_linear_estimator, + stat_buf.st_size); + } else { + enable_meter = 0; + } + /* nextline is the line number of the end of the current entry */ for( lineno=1; ldif_read_record( ldiffp, &nextline, &buf, &lmax ); lineno=nextline+1 ) { @@ -123,6 +151,11 @@ slapadd( int argc, char **argv ) e = str2entry2( buf, checkvals ); + if ( enable_meter ) + lutil_meter_update( &meter, + ftell( ldiffp->fp ), + 0); + /* * Initialize text buffer */ @@ -187,7 +220,7 @@ slapadd( int argc, char **argv ) op->o_bd = be; if ( (slapMode & SLAP_TOOL_NO_SCHEMA_CHECK) == 0) { - rc = entry_schema_check( op, e, NULL, manage, 1, + rc = entry_schema_check( op, e, NULL, manage, 1, NULL, &text, textbuf, textlen ); if( rc != LDAP_SUCCESS ) { @@ -198,6 +231,7 @@ slapadd( int argc, char **argv ) if( continuemode ) continue; break; } + textbuf[ 0 ] = '\0'; } } @@ -218,7 +252,7 @@ slapadd( int argc, char **argv ) nvals[1].bv_len = 0; nvals[1].bv_val = NULL; - csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 ); + csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), csnsid, 0 ); csn.bv_val = csnbuf; timestamp.bv_val = timebuf; @@ -250,54 +284,51 @@ slapadd( int argc, char **argv ) attr_merge( e, slap_schema.si_ad_creatorsName, vals, nvals ); } - if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName ) + if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp ) == NULL ) { - vals[0] = name; - nvals[0] = nname; - attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals ); + vals[0] = timestamp; + attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL ); } - if( attr_find( e->e_attrs, slap_schema.si_ad_createTimestamp ) + if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN ) == NULL ) { - vals[0] = timestamp; - attr_merge( e, slap_schema.si_ad_createTimestamp, vals, NULL ); + vals[0] = csn; + attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL ); } - if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp ) + if( attr_find( e->e_attrs, slap_schema.si_ad_modifiersName ) == NULL ) { - vals[0] = timestamp; - attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL ); + vals[0] = name; + nvals[0] = nname; + attr_merge( e, slap_schema.si_ad_modifiersName, vals, nvals ); } - if( attr_find( e->e_attrs, slap_schema.si_ad_entryCSN ) + if( attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp ) == NULL ) { - vals[0] = csn; - attr_merge( e, slap_schema.si_ad_entryCSN, vals, NULL ); + vals[0] = timestamp; + attr_merge( e, slap_schema.si_ad_modifyTimestamp, vals, NULL ); } if ( update_ctxcsn ) { - struct berval nsid; + int rc_sid; attr = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN ); assert( attr != NULL ); - - rc = mr_csnsid->smr_normalize( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, - NULL, NULL, &attr->a_nvals[ 0 ], &nsid, NULL ); - assert( rc == LDAP_SUCCESS ); - rc = lutil_atoulx( &sid, nsid.bv_val, 16 ); - ber_memfree( nsid.bv_val ); - if ( rc ) { + + rc_sid = slap_parse_csn_sid( &attr->a_nvals[ 0 ] ); + if ( rc_sid < 0 ) { Debug( LDAP_DEBUG_ANY, "%s: could not " "extract SID from entryCSN=%s\n", progname, attr->a_nvals[ 0 ].bv_val, 0 ); } else { - assert( sid <= SLAP_SYNC_SID_MAX ); + assert( rc_sid <= SLAP_SYNC_SID_MAX ); + sid = (unsigned)rc_sid; if ( maxcsn[ sid ].bv_len != 0 ) { match = 0; value_match( &match, slap_schema.si_ad_entryCSN, @@ -342,6 +373,11 @@ slapadd( int argc, char **argv ) bvtext.bv_val = textbuf; bvtext.bv_val[0] = '\0'; + if ( enable_meter ) { + lutil_meter_update( &meter, ftell( ldiffp->fp ), 1); + lutil_meter_close( &meter ); + } + if ( rc == EXIT_SUCCESS && update_ctxcsn && !dryrun && sid != SLAP_SYNC_SID_MAX + 1 ) { ctxcsn_id = be->be_dn2id_get( be, be->be_nsuffix ); if ( ctxcsn_id == NOID ) { @@ -350,24 +386,19 @@ slapadd( int argc, char **argv ) } else { ctxcsn_e = be->be_entry_get( be, ctxcsn_id ); if ( ctxcsn_e != NULL ) { - attr = attr_find( ctxcsn_e->e_attrs, slap_schema.si_ad_contextCSN ); + Entry *e = entry_dup( ctxcsn_e ); + int change; + attr = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN ); if ( attr ) { int i; - for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) { - struct berval nsid; - - rc = mr_csnsid->smr_normalize( SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX, - NULL, NULL, &attr->a_nvals[ i ], &nsid, NULL ); + change = 0; - /* must succeed, since it passed - * validation/normalization */ - assert( rc == LDAP_SUCCESS ); + for ( i = 0; !BER_BVISNULL( &attr->a_nvals[ i ] ); i++ ) { + int rc_sid; - rc = lutil_atoulx( &sid, nsid.bv_val, 16 ); - ber_memfree( nsid.bv_val ); - - if ( rc ) { + rc_sid = slap_parse_csn_sid( &attr->a_nvals[ i ] ); + if ( rc_sid < 0 ) { Debug( LDAP_DEBUG_ANY, "%s: unable to extract SID " "from #%d contextCSN=%s\n", @@ -376,6 +407,10 @@ slapadd( int argc, char **argv ) continue; } + assert( rc_sid <= SLAP_SYNC_SID_MAX ); + + sid = (unsigned)rc_sid; + if ( maxcsn[ sid ].bv_len == 0 ) { match = -1; @@ -386,7 +421,9 @@ slapadd( int argc, char **argv ) &maxcsn[ sid ], &attr->a_nvals[i], &text ); } - if ( match < 0 ) { + if ( match > 0 ) { + change = 1; + } else { AC_MEMCPY( maxcsn[ sid ].bv_val, attr->a_nvals[ i ].bv_val, attr->a_nvals[ i ].bv_len ); @@ -395,33 +432,38 @@ slapadd( int argc, char **argv ) } } -#if 0 - if ( attr->a_nvals && attr->a_nvals != attr->a_vals ) { - ber_bvarray_free( attr->a_nvals ); + if ( change ) { + if ( attr->a_nvals != attr->a_vals ) { + ber_bvarray_free( attr->a_nvals ); + } + attr->a_nvals = NULL; + ber_bvarray_free( attr->a_vals ); + attr->a_vals = NULL; + attr->a_numvals = 0; } - ber_bvarray_free( attr->a_vals ); -#endif - - attr->a_vals = NULL; - attr->a_nvals = NULL; + } else { + change = 1; } - for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) { - if ( maxcsn[ sid ].bv_len ) { - attr_merge_one( ctxcsn_e, slap_schema.si_ad_contextCSN, - &maxcsn[ sid], NULL ); + if ( change ) { + for ( sid = 0; sid <= SLAP_SYNC_SID_MAX; sid++ ) { + if ( maxcsn[ sid ].bv_len ) { + attr_merge_one( e, slap_schema.si_ad_contextCSN, + &maxcsn[ sid], NULL ); + } + } + + ctxcsn_id = be->be_entry_modify( be, e, &bvtext ); + if( ctxcsn_id == NOID ) { + fprintf( stderr, "%s: could not modify ctxcsn\n", + progname); + rc = EXIT_FAILURE; + } else if ( verbose ) { + fprintf( stderr, "modified: \"%s\" (%08lx)\n", + e->e_dn, (long) ctxcsn_id ); } } - - ctxcsn_id = be->be_entry_modify( be, ctxcsn_e, &bvtext ); - if( ctxcsn_id == NOID ) { - fprintf( stderr, "%s: could not modify ctxcsn\n", - progname); - rc = EXIT_FAILURE; - } else if ( verbose ) { - fprintf( stderr, "modified: \"%s\" (%08lx)\n", - ctxcsn_e->e_dn, (long) ctxcsn_id ); - } + entry_free( e ); } } } @@ -429,6 +471,9 @@ slapadd( int argc, char **argv ) ch_free( buf ); if ( !dryrun ) { + if ( enable_meter ) { + fprintf( stderr, "Closing DB..." ); + } if( be->be_entry_close( be ) ) { rc = EXIT_FAILURE; } @@ -436,9 +481,13 @@ slapadd( int argc, char **argv ) if( be->be_sync ) { be->be_sync( be ); } + if ( enable_meter ) { + fprintf( stderr, "\n" ); + } } - slap_tool_destroy(); + if ( slap_tool_destroy()) + rc = EXIT_FAILURE; return rc; }