]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldif/ldif.c
More ldapbis cleanup
[openldap] / servers / slapd / back-ldif / ldif.c
index 57898f8b9d4771cdf12ca079852a118c19dcb92d..bec6a02b648fca54e44fc2dd450e86d905c5ed36 100644 (file)
@@ -86,6 +86,7 @@ dn2path(struct berval * dn, struct berval * suffixdn, struct berval * base_path,
        struct berval *res)
 {
        char *ptr, *sep, *end;
+       struct berval bv;
 
        assert( dn != NULL );
        assert( !BER_BVISNULL( dn ) );
@@ -107,14 +108,19 @@ dn2path(struct berval * dn, struct berval * suffixdn, struct berval * base_path,
        }
        strcpy(ptr, LDIF);
 #if IX_FSL != IX_DNL
-       ptr = res->bv_val;
-       while( ptr=strchr(ptr, IX_DNL) ) {
+       bv = *res;
+       while ( ptr = ber_bvchr( &bv, IX_DNL ) ) {
                *ptr++ = IX_FSL;
-               ptr = strchr(ptr, IX_DNR);
-               if ( ptr )
-                       *ptr++ = IX_FSR;
-               else
+               assert( ( ptr - bv.bv_val ) <= bv.bv_len );
+               bv.bv_len -= ( ptr - bv.bv_val );
+               bv.bv_val = ptr;
+               ptr = ber_bvchr( &bv, IX_DNR );
+               if ( !ptr )
                        break;
+               *ptr++ = IX_FSR;
+               assert( ( ptr - bv.bv_val ) <= bv.bv_len );
+               bv.bv_len -= ( ptr - bv.bv_val );
+               bv.bv_val = ptr;
        }
 #endif
 }
@@ -157,7 +163,7 @@ static int spew_file(int fd, char * spew, int len) {
                writeres = write(fd, spew, len);
                if(writeres == -1) {
                        Debug( LDAP_DEBUG_ANY, "could not spew write: %s\n",
-                               strerror( errno ), 0, 0 );
+                               STRERROR( errno ), 0, 0 );
                        return -1;
                }
                else {
@@ -175,14 +181,14 @@ static int spew_entry(Entry * e, struct berval * path) {
        int entry_length;
        char * entry_as_string;
 
-       openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR | S_IWUSR);
+       openres = open(path->bv_val, O_WRONLY|O_CREAT|O_TRUNC, S_IREAD | S_IWRITE);
        if(openres == -1) {
                if(errno == ENOENT)
                        rs = LDAP_NO_SUCH_OBJECT;
                else
                        rs = LDAP_UNWILLING_TO_PERFORM;
                Debug( LDAP_DEBUG_ANY, "could not open \"%s\": %s\n",
-                       path->bv_val, strerror( errno ), 0 );
+                       path->bv_val, STRERROR( errno ), 0 );
        }
        else {
                struct berval rdn;
@@ -266,7 +272,7 @@ static Entry * get_entry(Operation *op, struct berval *base_path) {
        /* error opening file (mebbe should log error) */
        if ( fd == -1 && ( errno != ENOENT || op->o_tag != LDAP_REQ_ADD ) ) {
                Debug( LDAP_DEBUG_ANY, "failed to open file \"%s\": %s\n",
-                       path.bv_val, strerror(errno), 0 );
+                       path.bv_val, STRERROR(errno), 0 );
        }
 
        if(path.bv_val != NULL)
@@ -308,7 +314,7 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
        if ( fd < 0 ) {
                Debug( LDAP_DEBUG_ANY,
                        "=> ldif_enum_tree: failed to open %s: %s\n",
-                       path->bv_val, strerror(errno), 0 );
+                       path->bv_val, STRERROR(errno), 0 );
                return LDAP_NO_SUCH_OBJECT;
        }
 
@@ -412,17 +418,17 @@ static int r_enum_tree(enumCookie *ck, struct berval *path,
                        bvl = ch_malloc( sizeof(bvlist) );
                        ber_dupbv( &bvl->bv, &fname );
                        BER_BVZERO( &bvl->num );
-                       itmp.bv_val = strchr( bvl->bv.bv_val, IX_FSL );
+                       itmp.bv_val = ber_bvchr( &bvl->bv, IX_FSL );
                        if ( itmp.bv_val ) {
                                char *ptr;
                                itmp.bv_val++;
-                               ptr = strchr( itmp.bv_val, IX_FSR );
+                               itmp.bv_len = bvl->bv.bv_len
+                                       - ( itmp.bv_val - bvl->bv.bv_val );
+                               ptr = ber_bvchr( &itmp, IX_FSR );
                                if ( ptr ) {
                                        itmp.bv_len = ptr - itmp.bv_val;
                                        ber_dupbv( &bvl->num, &itmp );
-                                       /* FIXME: handle error? */
-                                       assert( itmp.bv_val[ 0 ] != '-' );
-                                       bvl->inum = strtoul( itmp.bv_val, NULL, 0 );
+                                       bvl->inum = strtol( itmp.bv_val, NULL, 0 );
                                        itmp.bv_val[0] = '\0';
                                        bvl->off = itmp.bv_val - bvl->bv.bv_val;
                                }
@@ -785,7 +791,10 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
        char textbuf[SLAP_TEXT_BUFLEN];
 
        Debug( LDAP_DEBUG_TRACE, "ldif_back_add: \"%s\"\n", dn.bv_val, 0, 0);
-       slap_add_opattrs( op, &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
+
+       rs->sr_err = slap_add_opattrs( op,
+               &rs->sr_text, textbuf, sizeof( textbuf ), 1 );
+       if ( rs->sr_err != LDAP_SUCCESS ) goto send_res;
 
        rs->sr_err = entry_schema_check(op, e, NULL, 0,
                &rs->sr_text, textbuf, sizeof( textbuf ) );
@@ -815,7 +824,7 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
                                        rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                                        rs->sr_text = "Could not create parent folder";
                                        Debug( LDAP_DEBUG_ANY, "could not create folder \"%s\": %s\n",
-                                               base.bv_val, strerror( errno ), 0 );
+                                               base.bv_val, STRERROR( errno ), 0 );
                                }
                        }
                        else
@@ -831,7 +840,7 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
                        else if ( statres == -1 ) {
                                rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                                Debug( LDAP_DEBUG_ANY, "could not stat file \"%s\": %s\n",
-                                       leaf_path.bv_val, strerror( errno ), 0 );
+                                       leaf_path.bv_val, STRERROR( errno ), 0 );
                        }
                        else /* it already exists */
                                rs->sr_err = LDAP_ALREADY_EXISTS;
@@ -844,7 +853,8 @@ static int ldif_back_add(Operation *op, SlapReply *rs) {
 
 send_res:
        Debug( LDAP_DEBUG_TRACE, 
-                       "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text, 0);
+                       "ldif_back_add: err: %d text: %s\n", rs->sr_err, rs->sr_text ?
+                               rs->sr_text : "", 0);
        send_ldap_result(op, rs);
        slap_graduate_commit_csn( op );
        return 0;
@@ -875,7 +885,7 @@ static int ldif_back_modify(Operation *op, SlapReply *rs) {
                        if(spew_res == -1) {
                                Debug( LDAP_DEBUG_ANY,
                                        "%s ldif_back_modify: could not output entry \"%s\": %s\n",
-                                       op->o_log_prefix, entry->e_name.bv_val, strerror( save_errno ) );
+                                       op->o_log_prefix, entry->e_name.bv_val, STRERROR( save_errno ) );
                                rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
                        }
                }