rv = UpdateV4DB(handle, updatedb);
if (isV4DB(updatedb)) { rv = UpdateV4DB(handle, updatedb); } else { rv = UpdateV5DB(handle, updatedb); }
SEC_OpenPermCertDB(CERTCertDBHandle *handle, PRBool readOnly,		   CERTDBNameFunc namecb, void *cbarg){    SECStatus rv;    int openflags;    certDBEntryVersion *versionEntry = NULL;    DB *updatedb = NULL;    char *tmpname;    char *certdbname;    PRBool updated = PR_FALSE;    PRBool forceUpdate = PR_FALSE;        certdbname = (* namecb)(cbarg, CERT_DB_FILE_VERSION);    if ( certdbname == NULL ) {	return(SECFailure);    }        if ( readOnly ) {	openflags = O_RDONLY;    } else {	openflags = O_RDWR;    }        /*     * first open the permanent file based database.     */    handle->permCertDB = dbopen( certdbname, openflags, 0600, DB_HASH, 0 );    /* check for correct version number */    if ( handle->permCertDB ) {	versionEntry = ReadDBVersionEntry(handle);	if ( versionEntry == NULL ) {	    /* no version number */	    certdb_Close(handle->permCertDB);	    handle->permCertDB = 0;	} else if ( versionEntry->common.version != CERT_DB_FILE_VERSION ) {	    /* wrong version number, can't update in place */	    DestroyDBEntry((certDBEntry *)versionEntry);	    PORT_Free(certdbname);	    return(SECFailure);	}    }    /* if first open fails, try to create a new DB */    if ( handle->permCertDB == NULL ) {	/* don't create if readonly */	if ( readOnly ) {	    goto loser;	}		handle->permCertDB = dbopen(certdbname,				    O_RDWR | O_CREAT | O_TRUNC,				    0600, DB_HASH, 0);	/* if create fails then we lose */	if ( handle->permCertDB == 0 ) {	    goto loser;	}	versionEntry = NewDBVersionEntry(0);	if ( versionEntry == NULL ) {	    goto loser;	}		rv = WriteDBVersionEntry(handle, versionEntry);	DestroyDBEntry((certDBEntry *)versionEntry);	if ( rv != SECSuccess ) {	    goto loser;	}	/* try to upgrade old db here */	tmpname = (* namecb)(cbarg, 6);	/* get v6 db name */	if ( tmpname ) {	    updatedb = dbopen( tmpname, O_RDONLY, 0600, DB_HASH, 0 );	    PORT_Free(tmpname);	    if ( updatedb ) {		rv = UpdateV6DB(handle, updatedb);		if ( rv != SECSuccess ) {		    goto loser;		}		updated = PR_TRUE;	    } else { /* no v6 db, so try v5 db */		tmpname = (* namecb)(cbarg, 5);	/* get v5 db name */		if ( tmpname ) {		    updatedb = dbopen( tmpname, O_RDONLY, 0600, DB_HASH, 0 );		    PORT_Free(tmpname);		    if ( updatedb ) {			rv = UpdateV5DB(handle, updatedb);			if ( rv != SECSuccess ) {			    goto loser;			}			updated = PR_TRUE;		    } else { /* no v5 db, so try v4 db */			/* try to upgrade v4 db */			tmpname = (* namecb)(cbarg, 4);	/* get v4 db name */			if ( tmpname ) {			    updatedb = dbopen( tmpname, O_RDONLY, 0600,					      DB_HASH, 0 );			    PORT_Free(tmpname);			    if ( updatedb ) {				rv = UpdateV4DB(handle, updatedb);				if ( rv != SECSuccess ) {				    goto loser;				}				forceUpdate = PR_TRUE;				updated = PR_TRUE;			    }			}		    }		}	    }	}	/* initialize the database with our well known certificates	 * or in the case of update, just fall down to CERT_AddNewCerts()	 * below.	 * Note - if we are updating a really old database, then we try	 * to push all of the certs into it.	 */	if ( ( !updated ) || forceUpdate ) {	    rv = CERT_InitCertDB(handle);	    if ( rv != SECSuccess ) {		goto loser;	    }	}    }    rv = CERT_AddNewCerts(handle);    PORT_Free(certdbname);        return (SECSuccess);    loser:    PORT_SetError(SEC_ERROR_BAD_DATABASE);        if ( handle->permCertDB ) {	certdb_Close(handle->permCertDB);	handle->permCertDB = 0;    }    PORT_Free(certdbname);    return(SECFailure);}