| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: OpenGIS Simple Features Reference Implementation |
| 4 |
* Purpose: Implements FileGDB OGR driver. |
| 5 |
* Author: Ragi Yaser Burhum, [email protected] |
| 6 |
* Paul Ramsey, pramsey at cleverelephant.ca |
| 7 |
* |
| 8 |
****************************************************************************** |
| 9 |
* Copyright (c) 2010, Ragi Yaser Burhum |
| 10 |
* Copyright (c) 2011, Paul Ramsey <pramsey at cleverelephant.ca> |
| 11 |
* Copyright (c) 2011-2013, Even Rouault <even dot rouault at spatialys.com> |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: MIT |
| 14 |
****************************************************************************/ |
| 15 |
|
| 16 |
#include "ogr_fgdb.h" |
| 17 |
#include "cpl_conv.h" |
| 18 |
#include "FGdbUtils.h" |
| 19 |
#include "cpl_multiproc.h" |
| 20 |
#include "ogrmutexeddatasource.h" |
| 21 |
#include "FGdbDriverCore.h" |
| 22 |
|
| 23 |
extern "C" void RegisterOGRFileGDB(); |
| 24 |
|
| 25 |
static std::map<CPLString, FGdbDatabaseConnection *> *poMapConnections = |
| 26 |
nullptr; |
| 27 |
CPLMutex *FGdbDriver::hMutex = nullptr; |
| 28 |
|
| 29 |
/************************************************************************/ |
| 30 |
/* OGRFileGDBDriverUnload() */ |
| 31 |
/************************************************************************/ |
| 32 |
|
| 33 |
static void OGRFileGDBDriverUnload(GDALDriver *) |
| 34 |
{ |
| 35 |
if (poMapConnections && !poMapConnections->empty()) |
| 36 |
CPLDebug("FileGDB", "Remaining %d connections. Bug?", |
| 37 |
(int)poMapConnections->size()); |
| 38 |
if (FGdbDriver::hMutex != nullptr) |
| 39 |
CPLDestroyMutex(FGdbDriver::hMutex); |
| 40 |
FGdbDriver::hMutex = nullptr; |
| 41 |
delete poMapConnections; |
| 42 |
poMapConnections = nullptr; |
| 43 |
} |
| 44 |
|
| 45 |
/************************************************************************/ |
| 46 |
/* OGRFileGDBDriverOpen() */ |
| 47 |
/************************************************************************/ |
| 48 |
|
| 49 |
static GDALDataset *OGRFileGDBDriverOpen(GDALOpenInfo *poOpenInfo) |
| 50 |
{ |
| 51 |
if (poOpenInfo->eAccess == GA_Update) |
| 52 |
return nullptr; |
| 53 |
|
| 54 |
const char *pszFilename = poOpenInfo->pszFilename; |
| 55 |
// @MAY_USE_OPENFILEGDB may be set to NO by the OpenFileGDB driver in its |
| 56 |
// Open() method when it detects that a dataset includes compressed tables |
| 57 |
// (.cdf), and thus calls the FileGDB driver to make it handle such |
| 58 |
// datasets. As the FileGDB driver would call, by default, OpenFileGDB for |
| 59 |
// assistance to get some information, OpenFileGDB needs to instruct it not |
| 60 |
// to do so to avoid a OpenFileGDB -> FileGDB -> OpenFileGDB cycle. |
| 61 |
const bool bUseOpenFileGDB = CPLTestBool(CSLFetchNameValueDef( |
| 62 |
poOpenInfo->papszOpenOptions, "@MAY_USE_OPENFILEGDB", "YES")); |
| 63 |
|
| 64 |
if (bUseOpenFileGDB) |
| 65 |
{ |
| 66 |
if (OGRFileGDBDriverIdentifyInternal(poOpenInfo, pszFilename) == |
| 67 |
GDAL_IDENTIFY_FALSE) |
| 68 |
return nullptr; |
| 69 |
|
| 70 |
// If this is a raster-only GDB, do not try to open it, to be consistent |
| 71 |
// with OpenFileGDB behavior. |
| 72 |
const char *const apszOpenFileGDBDriver[] = {"OpenFileGDB", nullptr}; |
| 73 |
auto poOpenFileGDBDS = std::unique_ptr<GDALDataset>( |
| 74 |
GDALDataset::Open(pszFilename, GDAL_OF_RASTER, |
| 75 |
apszOpenFileGDBDriver, nullptr, nullptr)); |
| 76 |
if (poOpenFileGDBDS) |
| 77 |
{ |
| 78 |
poOpenFileGDBDS.reset(); |
| 79 |
poOpenFileGDBDS = std::unique_ptr<GDALDataset>( |
| 80 |
GDALDataset::Open(pszFilename, GDAL_OF_VECTOR, |
| 81 |
apszOpenFileGDBDriver, nullptr, nullptr)); |
| 82 |
if (!poOpenFileGDBDS) |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
long hr; |
| 88 |
|
| 89 |
CPLMutexHolderD(&FGdbDriver::hMutex); |
| 90 |
if (poMapConnections == nullptr) |
| 91 |
poMapConnections = new std::map<CPLString, FGdbDatabaseConnection *>(); |
| 92 |
|
| 93 |
FGdbDatabaseConnection *pConnection = (*poMapConnections)[pszFilename]; |
| 94 |
if (pConnection != nullptr) |
| 95 |
{ |
| 96 |
pConnection->m_nRefCount++; |
| 97 |
CPLDebug("FileGDB", "ref_count of %s = %d now", pszFilename, |
| 98 |
pConnection->m_nRefCount); |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
Geodatabase *pGeoDatabase = new Geodatabase; |
| 103 |
hr = ::OpenGeodatabase(StringToWString(pszFilename), *pGeoDatabase); |
| 104 |
|
| 105 |
if (FAILED(hr)) |
| 106 |
{ |
| 107 |
delete pGeoDatabase; |
| 108 |
|
| 109 |
if (OGRGetDriverByName("OpenFileGDB") != nullptr) |
| 110 |
{ |
| 111 |
std::wstring fgdb_error_desc_w; |
| 112 |
std::string fgdb_error_desc("Unknown error"); |
| 113 |
fgdbError er; |
| 114 |
er = FileGDBAPI::ErrorInfo::GetErrorDescription( |
| 115 |
static_cast<fgdbError>(hr), fgdb_error_desc_w); |
| 116 |
if (er == S_OK) |
| 117 |
{ |
| 118 |
fgdb_error_desc = WStringToString(fgdb_error_desc_w); |
| 119 |
} |
| 120 |
CPLDebug("FileGDB", |
| 121 |
"Cannot open %s with FileGDB driver: %s. Failing " |
| 122 |
"silently so OpenFileGDB can be tried", |
| 123 |
pszFilename, fgdb_error_desc.c_str()); |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
GDBErr(hr, "Failed to open Geodatabase"); |
| 128 |
} |
| 129 |
poMapConnections->erase(pszFilename); |
| 130 |
return nullptr; |
| 131 |
} |
| 132 |
|
| 133 |
CPLDebug("FileGDB", "Really opening %s", pszFilename); |
| 134 |
pConnection = new FGdbDatabaseConnection(pszFilename, pGeoDatabase); |
| 135 |
(*poMapConnections)[pszFilename] = pConnection; |
| 136 |
} |
| 137 |
|
| 138 |
FGdbDataSource *pDS; |
| 139 |
|
| 140 |
pDS = new FGdbDataSource(true, pConnection, bUseOpenFileGDB); |
| 141 |
|
| 142 |
if (!pDS->Open(pszFilename, /*bUpdate = */ FALSE, nullptr)) |
| 143 |
{ |
| 144 |
delete pDS; |
| 145 |
return nullptr; |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
auto poMutexedDS = |
| 150 |
new OGRMutexedDataSource(pDS, TRUE, FGdbDriver::hMutex, TRUE); |
| 151 |
return poMutexedDS; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/************************************************************************/ |
| 156 |
/* OGRFileGDBDriverCreate() */ |
| 157 |
/************************************************************************/ |
| 158 |
|
| 159 |
static GDALDataset * |
| 160 |
OGRFileGDBDriverCreate(const char *pszName, CPL_UNUSED int nBands, |
| 161 |
CPL_UNUSED int nXSize, CPL_UNUSED int nYSize, |
| 162 |
CPL_UNUSED GDALDataType eDT, CSLConstList papszOptions) |
| 163 |
{ |
| 164 |
auto poOpenFileGDBDriver = |
| 165 |
GetGDALDriverManager()->GetDriverByName("OpenFileGDB"); |
| 166 |
if (poOpenFileGDBDriver) |
| 167 |
{ |
| 168 |
CPLError( |
| 169 |
CE_Warning, CPLE_AppDefined, |
| 170 |
"FileGDB driver has no longer any direct creation capabilities. " |
| 171 |
"Forwarding to OpenFileGDB driver"); |
| 172 |
return poOpenFileGDBDriver->Create(pszName, 0, 0, 0, GDT_Unknown, |
| 173 |
papszOptions); |
| 174 |
} |
| 175 |
return nullptr; |
| 176 |
} |
| 177 |
|
| 178 |
/************************************************************************/ |
| 179 |
/* Release() */ |
| 180 |
/************************************************************************/ |
| 181 |
|
| 182 |
void FGdbDriver::Release(const char *pszName) |
| 183 |
{ |
| 184 |
CPLMutexHolderOptionalLockD(FGdbDriver::hMutex); |
| 185 |
|
| 186 |
if (poMapConnections == nullptr) |
| 187 |
poMapConnections = new std::map<CPLString, FGdbDatabaseConnection *>(); |
| 188 |
|
| 189 |
FGdbDatabaseConnection *pConnection = (*poMapConnections)[pszName]; |
| 190 |
if (pConnection != nullptr) |
| 191 |
{ |
| 192 |
pConnection->m_nRefCount--; |
| 193 |
CPLDebug("FileGDB", "ref_count of %s = %d now", pszName, |
| 194 |
pConnection->m_nRefCount); |
| 195 |
if (pConnection->m_nRefCount == 0) |
| 196 |
{ |
| 197 |
pConnection->CloseGeodatabase(); |
| 198 |
delete pConnection; |
| 199 |
poMapConnections->erase(pszName); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/************************************************************************/ |
| 205 |
/* CloseGeodatabase() */ |
| 206 |
/************************************************************************/ |
| 207 |
|
| 208 |
void FGdbDatabaseConnection::CloseGeodatabase() |
| 209 |
{ |
| 210 |
if (m_pGeodatabase != nullptr) |
| 211 |
{ |
| 212 |
CPLDebug("FileGDB", "Really closing %s now", m_osName.c_str()); |
| 213 |
::CloseGeodatabase(*m_pGeodatabase); |
| 214 |
delete m_pGeodatabase; |
| 215 |
m_pGeodatabase = nullptr; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/************************************************************************/ |
| 220 |
/* OpenGeodatabase() */ |
| 221 |
/************************************************************************/ |
| 222 |
|
| 223 |
int FGdbDatabaseConnection::OpenGeodatabase(const char *pszFSName) |
| 224 |
{ |
| 225 |
m_pGeodatabase = new Geodatabase; |
| 226 |
long hr = ::OpenGeodatabase(StringToWString(CPLString(pszFSName)), |
| 227 |
*m_pGeodatabase); |
| 228 |
if (FAILED(hr)) |
| 229 |
{ |
| 230 |
delete m_pGeodatabase; |
| 231 |
m_pGeodatabase = nullptr; |
| 232 |
return FALSE; |
| 233 |
} |
| 234 |
return TRUE; |
| 235 |
} |
| 236 |
|
| 237 |
/************************************************************************/ |
| 238 |
/* OGRFileGDBDeleteDataSource() */ |
| 239 |
/************************************************************************/ |
| 240 |
|
| 241 |
static CPLErr OGRFileGDBDeleteDataSource(const char *pszDataSource) |
| 242 |
{ |
| 243 |
CPLMutexHolderD(&FGdbDriver::hMutex); |
| 244 |
|
| 245 |
std::wstring wstr = StringToWString(pszDataSource); |
| 246 |
|
| 247 |
long hr = 0; |
| 248 |
|
| 249 |
if (S_OK != (hr = ::DeleteGeodatabase(wstr))) |
| 250 |
{ |
| 251 |
GDBErr(hr, "Failed to delete Geodatabase"); |
| 252 |
return CE_Failure; |
| 253 |
} |
| 254 |
|
| 255 |
return CE_None; |
| 256 |
} |
| 257 |
|
| 258 |
/************************************************************************/ |
| 259 |
/* RegisterOGRFileGDB() */ |
| 260 |
/************************************************************************/ |
| 261 |
|
| 262 |
void RegisterOGRFileGDB() |
| 263 |
|
| 264 |
{ |
| 265 |
if (GDALGetDriverByName(DRIVER_NAME) != nullptr) |
| 266 |
return; |
| 267 |
|
| 268 |
if (!GDAL_CHECK_VERSION("OGR FGDB")) |
| 269 |
return; |
| 270 |
|
| 271 |
GDALDriver *poDriver = new GDALDriver(); |
| 272 |
OGRFileGDBDriverSetCommonMetadata(poDriver); |
| 273 |
|
| 274 |
poDriver->pfnOpen = OGRFileGDBDriverOpen; |
| 275 |
poDriver->pfnCreate = OGRFileGDBDriverCreate; |
| 276 |
poDriver->pfnDelete = OGRFileGDBDeleteDataSource; |
| 277 |
poDriver->pfnUnloadDriver = OGRFileGDBDriverUnload; |
| 278 |
|
| 279 |
GetGDALDriverManager()->RegisterDriver(poDriver); |
| 280 |
} |