From: Pierangelo Masarati Date: Fri, 17 Aug 2007 09:52:31 +0000 (+0000) Subject: rework previous commit X-Git-Tag: OPENLDAP_REL_ENG_2_4_MP~190 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=ce6b685cafe6b961260bc3b0a4d1d60cca377dc8;p=openldap rework previous commit --- diff --git a/servers/slapd/back-ldif/ldif.c b/servers/slapd/back-ldif/ldif.c index 6adee351ad..44486975c9 100644 --- a/servers/slapd/back-ldif/ldif.c +++ b/servers/slapd/back-ldif/ldif.c @@ -158,6 +158,10 @@ static char * slurp_file(int fd) { return entry; } +/* + * return number of bytes written, or -1 in case of error + * do not return numbers less than -1 + */ static int spew_file(int fd, char * spew, int len) { int writeres = 0; @@ -208,11 +212,23 @@ spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop ) rdn.bv_len = tmp; } + spew_res = -2; if ( dolock ) { ldap_pvt_thread_mutex_lock(&entry2str_mutex); } entry_as_string = entry2str(e, &entry_length); + if ( entry_as_string != NULL ) { + spew_res = spew_file( openres, + entry_as_string, entry_length ); + if ( spew_res == -1 ) { + save_errno = errno; + } + } + + if ( dolock ) { + ldap_pvt_thread_mutex_unlock(&entry2str_mutex); + } /* Restore full DN */ if ( rdn.bv_len != e->e_name.bv_len ) { @@ -220,31 +236,17 @@ spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop ) e->e_name.bv_len = rdn.bv_len; } - if ( entry_as_string == NULL ) { - rs = LDAP_UNWILLING_TO_PERFORM; - close(openres); - - if ( dolock ) { - ldap_pvt_thread_mutex_unlock(&entry2str_mutex); - } - - } else { - spew_res = spew_file( openres, - entry_as_string, entry_length ); - if ( spew_res == -1 ) - save_errno = errno; - - if ( dolock ) { - ldap_pvt_thread_mutex_unlock(&entry2str_mutex); - } + res = close( openres ); + rs = LDAP_UNWILLING_TO_PERFORM; - res = close( openres ); - rs = LDAP_UNWILLING_TO_PERFORM; + if ( spew_res > -2 ) { if ( res == -1 || spew_res == -1 ) { - if ( save_errno == 0 ) + if ( save_errno == 0 ) { save_errno = errno; + } Debug( LDAP_DEBUG_ANY, "write error to tmpfile \"%s\": %s\n", tmpfname, STRERROR( save_errno ), 0 ); + } else { res = rename( tmpfname, path->bv_val ); if ( res == 0 ) { @@ -264,14 +266,16 @@ spew_entry( Entry * e, struct berval * path, int dolock, int *save_errnop ) } } - if ( rs != LDAP_SUCCESS ) + if ( rs != LDAP_SUCCESS ) { unlink( tmpfname ); + } } ch_free( tmpfname ); - if ( rs != LDAP_SUCCESS && save_errnop != NULL ) + if ( rs != LDAP_SUCCESS && save_errnop != NULL ) { *save_errnop = save_errno; + } return rs; }