reg = ((REGHANDLE*)hReg)->pReg;   err = nr_Lock( reg ); if ( err != REGERR_OK ) return err;  PR_Lock(reglist_lock); XP_STRCPY(tempfilename, reg->filename); err = nr_createTempRegName(tempfilename, sizeof(tempfilename)); if ( err != REGERR_OK ) goto safe_exit;   fh = vr_fileOpen(tempfilename, XP_FILE_WRITE_BIN); if ( !VALID_FILEHANDLE(fh) ) { err = REGERR_FAIL; goto safe_exit; } XP_FileClose(fh);  err = NR_RegOpen(tempfilename, &hRegTemp); if ( err != REGERR_OK ) goto safe_exit; bCloseTempFile = TRUE;   XP_STRCPY(oldfilename, reg->filename); err = nr_createTempRegName(oldfilename, sizeof(oldfilename)); if ( err != REGERR_OK ) goto safe_exit;  key = ROOTKEY_PRIVATE; err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn); if ( err != REGERR_OK  ) goto safe_exit; key = ROOTKEY_VERSIONS; err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn); if ( err != REGERR_OK  ) goto safe_exit; key = ROOTKEY_COMMON; err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn); if ( err != REGERR_OK  ) goto safe_exit; key = ROOTKEY_USERS; err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn); if ( err != REGERR_OK  ) goto safe_exit;  err = NR_RegClose(hRegTemp); bCloseTempFile = FALSE;   XP_FileClose(reg->fh);   err = nr_RenameFile(reg->filename, oldfilename); if ( err == -1 ) {  remove(tempfilename); reg->fh = vr_fileOpen(reg->filename, XP_FILE_UPDATE_BIN); goto safe_exit; }   err = nr_RenameFile(tempfilename, reg->filename); if ( err == -1 ) {  err = nr_RenameFile(oldfilename, reg->filename); remove(tempfilename); reg->fh = vr_fileOpen(reg->filename, XP_FILE_UPDATE_BIN); goto safe_exit;  } else { remove(oldfilename); } reg->fh = vr_fileOpen(reg->filename, XP_FILE_UPDATE_BIN);  safe_exit: if ( bCloseTempFile ) { NR_RegClose(hRegTemp); } PR_Unlock( reglist_lock ); nr_Unlock(reg); return err;
return REGERR_OK;
VR_INTERFACE(REGERR) NR_RegPack( HREG hReg, void *userData, nr_RegPackCallbackFunc fn){    XP_File  fh;    REGFILE* reg;    HREG hRegTemp;    char tempfilename[MAX_PATH] = {0};    char oldfilename[MAX_PATH] = {0};    XP_Bool bCloseTempFile = FALSE;    int err = REGERR_OK;    RKEY key;    XP_ASSERT( regStartCount > 0 );    if ( regStartCount <= 0 )        return REGERR_FAIL;    reg = ((REGHANDLE*)hReg)->pReg;    /* lock registry */    err = nr_Lock( reg );    if ( err != REGERR_OK )        return err;     PR_Lock(reglist_lock);     XP_STRCPY(tempfilename, reg->filename);    err = nr_createTempRegName(tempfilename, sizeof(tempfilename));    if ( err != REGERR_OK )        goto safe_exit;          /* force file creation */    fh = vr_fileOpen(tempfilename, XP_FILE_WRITE_BIN);    if ( !VALID_FILEHANDLE(fh) ) {        err = REGERR_FAIL;        goto safe_exit;    }    XP_FileClose(fh);    err = NR_RegOpen(tempfilename, &hRegTemp);    if ( err != REGERR_OK )        goto safe_exit;    bCloseTempFile = TRUE;    /* must open temp file first or we get the same name twice */    XP_STRCPY(oldfilename, reg->filename);    err = nr_createTempRegName(oldfilename, sizeof(oldfilename));    if ( err != REGERR_OK )        goto safe_exit;          key = ROOTKEY_PRIVATE;    err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn);    if ( err != REGERR_OK  )        goto safe_exit;    key = ROOTKEY_VERSIONS;    err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn);    if ( err != REGERR_OK  )        goto safe_exit;    key = ROOTKEY_COMMON;    err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn);    if ( err != REGERR_OK  )        goto safe_exit;    key = ROOTKEY_USERS;    err = nr_addNodesToNewReg( hReg, key, hRegTemp, userData, fn);    if ( err != REGERR_OK  )        goto safe_exit;    err = NR_RegClose(hRegTemp);    bCloseTempFile = FALSE;      /* close current reg file so we can rename it */    XP_FileClose(reg->fh);       /* rename current reg file out of the way */    err = nr_RenameFile(reg->filename, oldfilename);    if ( err == -1 ) {        /* rename failed, get rid of the new registry and reopen the old one*/        remove(tempfilename);        reg->fh = vr_fileOpen(reg->filename, XP_FILE_UPDATE_BIN);        goto safe_exit;    }    /* rename packed registry to the correct name */    err = nr_RenameFile(tempfilename, reg->filename);    if ( err == -1 ) {        /* failure, recover original registry */        err = nr_RenameFile(oldfilename, reg->filename);        remove(tempfilename);        reg->fh = vr_fileOpen(reg->filename, XP_FILE_UPDATE_BIN);        goto safe_exit;        } else {        remove(oldfilename);     }    reg->fh = vr_fileOpen(reg->filename, XP_FILE_UPDATE_BIN);safe_exit:    if ( bCloseTempFile ) {        NR_RegClose(hRegTemp);    }    PR_Unlock( reglist_lock );    nr_Unlock(reg);    return err;}