OGRErr FGdbDriver::CommitTransaction(OGRDataSource*& poDSInOut, int& bOutHasReopenedDS) { CPLMutexHolderOptionalLockD(hMutex); bOutHasReopenedDS = FALSE; OGRMutexedDataSource* poMutexedDS = (OGRMutexedDataSource*)poDSInOut; FGdbDataSource* poDS = (FGdbDataSource* )poMutexedDS->GetBaseDataSource(); FGdbDatabaseConnection* pConnection = poDS->GetConnection(); if( !pConnection->IsLocked() ) { CPLError(CE_Failure, CPLE_NotSupported, "No transaction in progress"); return OGRERR_FAILURE; } bOutHasReopenedDS = TRUE; CPLString osName(poMutexedDS->GetName()); CPLString osNameOri(osName); if( osName[osName.size()-1] == '/' || osName[osName.size()-1] == '\\' ) osName.resize(osName.size()-1); #ifndef WIN32 int bPerLayerCopyingForTransaction = poDS->HasPerLayerCopyingForTransaction(); #endif pConnection->m_nRefCount ++; delete poDSInOut; poDSInOut = NULL; poMutexedDS = NULL; poDS = NULL; pConnection->CloseGeodatabase(); CPLString osEditedName(osName); osEditedName += ".ogredited"; #ifndef WIN32 if( bPerLayerCopyingForTransaction ) { int bError = FALSE; char** papszFiles; std::vector<CPLString> aosTmpFilesToClean; // Check for files present in original copy that are not in edited copy // That is to say deleted layers papszFiles = VSIReadDir(osName); for(char** papszIter = papszFiles; !bError && *papszIter; ++papszIter) { if( strcmp(*papszIter, ".") == 0 || strcmp(*papszIter, "..") == 0 ) continue; VSIStatBufL sStat; if( (*papszIter)[0] == 'a' && VSIStatL( CPLFormFilename(osEditedName, *papszIter, NULL), &sStat ) != 0 ) { if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE1") || VSIRename( CPLFormFilename(osName, *papszIter, NULL), CPLFormFilename(osName, *papszIter, "tmp") ) != 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s", CPLFormFilename(osName, *papszIter, NULL), CPLFormFilename(osName, *papszIter, "tmp")); bError = TRUE; } else aosTmpFilesToClean.push_back(CPLFormFilename(osName, *papszIter, "tmp")); } } CSLDestroy(papszFiles); // Move modified files from edited directory to main directory papszFiles = VSIReadDir(osEditedName); for(char** papszIter = papszFiles; !bError && *papszIter; ++papszIter) { if( strcmp(*papszIter, ".") == 0 || strcmp(*papszIter, "..") == 0 ) continue; struct stat sStat; if( lstat( CPLFormFilename(osEditedName, *papszIter, NULL), &sStat ) != 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot stat %s", CPLFormFilename(osEditedName, *papszIter, NULL)); bError = TRUE; } else if( !S_ISLNK(sStat.st_mode) ) { // If there was such a file in original directory, first rename it // as a temporary file if( lstat( CPLFormFilename(osName, *papszIter, NULL), &sStat ) == 0 ) { if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE2") || VSIRename( CPLFormFilename(osName, *papszIter, NULL), CPLFormFilename(osName, *papszIter, "tmp") ) != 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s", CPLFormFilename(osName, *papszIter, NULL), CPLFormFilename(osName, *papszIter, "tmp")); bError = TRUE; } else aosTmpFilesToClean.push_back(CPLFormFilename(osName, *papszIter, "tmp")); } if( !bError ) { if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE3") || CPLMoveFile( CPLFormFilename(osName, *papszIter, NULL), CPLFormFilename(osEditedName, *papszIter, NULL) ) != 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot move %s to %s", CPLFormFilename(osEditedName, *papszIter, NULL), CPLFormFilename(osName, *papszIter, NULL)); bError = TRUE; } else CPLDebug("FileGDB", "Move %s to %s", CPLFormFilename(osEditedName, *papszIter, NULL), CPLFormFilename(osName, *papszIter, NULL)); } } } CSLDestroy(papszFiles); if( !bError ) { for(size_t i=0;i<aosTmpFilesToClean.size();i++) { if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE4") || VSIUnlink(aosTmpFilesToClean[i]) != 0 ) { CPLError(CE_Warning, CPLE_AppDefined, "Cannot remove %s. Manual cleanup required", aosTmpFilesToClean[i].c_str()); } } } if( bError ) { CPLError(CE_Failure, CPLE_AppDefined, "An error occurred while moving files from %s back to %s. " "Manual cleaning must be done and dataset should be closed", osEditedName.c_str(), osName.c_str()); pConnection->SetLocked(FALSE); Release(osName); return OGRERR_FAILURE; } else if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE5") || CPLUnlinkTree(osEditedName) != 0 ) { CPLError(CE_Warning, CPLE_AppDefined, "Cannot remove %s. Manual cleanup required", osEditedName.c_str()); } } else #endif { CPLString osTmpName(osName); osTmpName += ".ogrtmp"; /* Install the backup copy as the main database in 3 steps : */ /* first rename the main directory in .tmp */ /* then rename the edited copy under regular name */ /* and finally dispose the .tmp directory */ /* That way there's no risk definitely losing data */ if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE1") || VSIRename(osName, osTmpName) != 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s. Edited database during transaction is in %s" "Dataset should be closed", osName.c_str(), osTmpName.c_str(), osEditedName.c_str()); pConnection->SetLocked(FALSE); Release(osName); return OGRERR_FAILURE; } if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE2") || VSIRename(osEditedName, osName) != 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot rename %s to %s. The original geodatabase is in '%s'. " "Dataset should be closed", osEditedName.c_str(), osName.c_str(), osTmpName.c_str()); pConnection->SetLocked(FALSE); Release(osName); return OGRERR_FAILURE; } if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE3") || CPLUnlinkTree(osTmpName) != 0 ) { CPLError(CE_Warning, CPLE_AppDefined, "Cannot remove %s. Manual cleanup required", osTmpName.c_str()); } } pConnection->m_pGeodatabase = new Geodatabase; long hr = ::OpenGeodatabase(StringToWString(osName), *(pConnection->m_pGeodatabase)); if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE_REOPEN") || FAILED(hr)) { delete pConnection->m_pGeodatabase; pConnection->m_pGeodatabase = NULL; pConnection->SetLocked(FALSE); Release(osName); GDBErr(hr, "Failed to re-open Geodatabase. Dataset should be closed"); return OGRERR_FAILURE; } FGdbDataSource* pDS = new FGdbDataSource(this, pConnection); pDS->Open(osNameOri, TRUE, NULL); //pDS->SetPerLayerCopyingForTransaction(bPerLayerCopyingForTransaction); poDSInOut = new OGRMutexedDataSource(pDS, TRUE, hMutex, TRUE); pConnection->SetLocked(FALSE); return OGRERR_NONE; }
OGRErr FGdbDriver::RollbackTransaction(OGRDataSource*& poDSInOut, int& bOutHasReopenedDS) { CPLMutexHolderOptionalLockD(hMutex); bOutHasReopenedDS = FALSE; OGRMutexedDataSource* poMutexedDS = (OGRMutexedDataSource*)poDSInOut; FGdbDataSource* poDS = (FGdbDataSource* )poMutexedDS->GetBaseDataSource(); FGdbDatabaseConnection* pConnection = poDS->GetConnection(); if( !pConnection->IsLocked() ) { CPLError(CE_Failure, CPLE_NotSupported, "No transaction in progress"); return OGRERR_FAILURE; } bOutHasReopenedDS = TRUE; CPLString osName(poMutexedDS->GetName()); CPLString osNameOri(osName); if( osName[osName.size()-1] == '/' || osName[osName.size()-1] == '\\' ) osName.resize(osName.size()-1); //int bPerLayerCopyingForTransaction = poDS->HasPerLayerCopyingForTransaction(); pConnection->m_nRefCount ++; delete poDSInOut; poDSInOut = NULL; poMutexedDS = NULL; poDS = NULL; pConnection->CloseGeodatabase(); CPLString osEditedName(osName); osEditedName += ".ogredited"; OGRErr eErr = OGRERR_NONE; if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE1") || CPLUnlinkTree(osEditedName) != 0 ) { CPLError(CE_Warning, CPLE_AppDefined, "Cannot remove %s. Manual cleanup required", osEditedName.c_str()); eErr = OGRERR_FAILURE; } pConnection->m_pGeodatabase = new Geodatabase; long hr = ::OpenGeodatabase(StringToWString(osName), *(pConnection->m_pGeodatabase)); if (EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE2") || FAILED(hr)) { delete pConnection->m_pGeodatabase; pConnection->m_pGeodatabase = NULL; pConnection->SetLocked(FALSE); Release(osName); GDBErr(hr, "Failed to re-open Geodatabase. Dataset should be closed"); return OGRERR_FAILURE; } FGdbDataSource* pDS = new FGdbDataSource(this, pConnection); pDS->Open(osNameOri, TRUE, NULL); //pDS->SetPerLayerCopyingForTransaction(bPerLayerCopyingForTransaction); poDSInOut = new OGRMutexedDataSource(pDS, TRUE, hMutex, TRUE); pConnection->SetLocked(FALSE); return eErr; }
OGRErr FGdbDriver::StartTransaction(OGRDataSource*& poDSInOut, int& bOutHasReopenedDS) { CPLMutexHolderOptionalLockD(hMutex); bOutHasReopenedDS = FALSE; OGRMutexedDataSource* poMutexedDS = (OGRMutexedDataSource*)poDSInOut; FGdbDataSource* poDS = (FGdbDataSource* )poMutexedDS->GetBaseDataSource(); if( !poDS->GetUpdate() ) return OGRERR_FAILURE; FGdbDatabaseConnection* pConnection = poDS->GetConnection(); if( pConnection->GetRefCount() != 1 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot start transaction as database is opened in another connection"); return OGRERR_FAILURE; } if( pConnection->IsLocked() ) { CPLError(CE_Failure, CPLE_AppDefined, "Transaction is already in progress"); return OGRERR_FAILURE; } bOutHasReopenedDS = TRUE; CPLString osName(poMutexedDS->GetName()); CPLString osNameOri(osName); if( osName[osName.size()-1] == '/' || osName[osName.size()-1] == '\\' ) osName.resize(osName.size()-1); #ifndef WIN32 int bPerLayerCopyingForTransaction = poDS->HasPerLayerCopyingForTransaction(); #endif pConnection->m_nRefCount ++; delete poDSInOut; poDSInOut = NULL; poMutexedDS = NULL; poDS = NULL; pConnection->CloseGeodatabase(); CPLString osEditedName(osName); osEditedName += ".ogredited"; CPLPushErrorHandler(CPLQuietErrorHandler); CPL_IGNORE_RET_VAL(CPLUnlinkTree(osEditedName)); CPLPopErrorHandler(); OGRErr eErr = OGRERR_NONE; CPLString osDatabaseToReopen; #ifndef WIN32 if( bPerLayerCopyingForTransaction ) { int bError = FALSE; if( VSIMkdir( osEditedName, 0755 ) != 0 ) { CPLError( CE_Failure, CPLE_AppDefined, "Cannot create directory '%s'.", osEditedName.c_str() ); bError = TRUE; } // Only copy a0000000X.Y files with X >= 1 && X <= 8, gdb and timestamps // and symlink others char** papszFiles = VSIReadDir(osName); for(char** papszIter = papszFiles; !bError && *papszIter; ++papszIter) { if( strcmp(*papszIter, ".") == 0 || strcmp(*papszIter, "..") == 0 ) continue; if( ((*papszIter)[0] == 'a' && atoi((*papszIter)+1) >= 1 && atoi((*papszIter)+1) <= 8) || EQUAL(*papszIter, "gdb") || EQUAL(*papszIter, "timestamps") ) { if( CPLCopyFile(CPLFormFilename(osEditedName, *papszIter, NULL), CPLFormFilename(osName, *papszIter, NULL)) != 0 ) { bError = TRUE; CPLError(CE_Failure, CPLE_AppDefined, "Cannot copy %s", *papszIter); } } else { CPLString osSourceFile; if( CPLIsFilenameRelative(osName) ) osSourceFile = CPLFormFilename(CPLSPrintf("../%s", CPLGetFilename(osName.c_str())), *papszIter, NULL); else osSourceFile = osName; if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE1") || CPLSymlink( osSourceFile, CPLFormFilename(osEditedName.c_str(), *papszIter, NULL), NULL ) != 0 ) { bError = TRUE; CPLError(CE_Failure, CPLE_AppDefined, "Cannot symlink %s", *papszIter); } } } CSLDestroy(papszFiles); if( bError ) { eErr = OGRERR_FAILURE; osDatabaseToReopen = osName; } else osDatabaseToReopen = osEditedName; } else #endif { if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE1") || CPLCopyTree( osEditedName, osName ) != 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot backup geodatabase"); eErr = OGRERR_FAILURE; osDatabaseToReopen = osName; } else osDatabaseToReopen = osEditedName; } pConnection->m_pGeodatabase = new Geodatabase; long hr = ::OpenGeodatabase(StringToWString(osDatabaseToReopen), *(pConnection->m_pGeodatabase)); if( EQUAL(CPLGetConfigOption("FGDB_SIMUL_FAIL", ""), "CASE2") || FAILED(hr)) { delete pConnection->m_pGeodatabase; pConnection->m_pGeodatabase = NULL; Release(osName); GDBErr(hr, CPLSPrintf("Failed to open %s. Dataset should be closed", osDatabaseToReopen.c_str())); return OGRERR_FAILURE; } FGdbDataSource* pDS = new FGdbDataSource(this, pConnection); pDS->Open(osDatabaseToReopen, TRUE, osNameOri); #ifndef WIN32 if( eErr == OGRERR_NONE && bPerLayerCopyingForTransaction ) { pDS->SetPerLayerCopyingForTransaction(bPerLayerCopyingForTransaction); pDS->SetSymlinkFlagOnAllLayers(); } #endif poDSInOut = new OGRMutexedDataSource(pDS, TRUE, hMutex, TRUE); if( eErr == OGRERR_NONE ) pConnection->SetLocked(TRUE); return eErr; }