/src/gdal/gcore/gdaldrivermanager.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: GDAL Core |
4 | | * Purpose: Implementation of GDALDriverManager class. |
5 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
6 | | * |
7 | | ****************************************************************************** |
8 | | * Copyright (c) 1998, Frank Warmerdam |
9 | | * Copyright (c) 2009-2013, Even Rouault <even dot rouault at spatialys.com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #include "cpl_port.h" |
15 | | #include "gdal_priv.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cstring> |
19 | | #include <map> |
20 | | #include <set> |
21 | | |
22 | | #include "cpl_conv.h" |
23 | | #include "cpl_error.h" |
24 | | #include "cpl_http.h" |
25 | | #include "cpl_multiproc.h" |
26 | | #include "cpl_port.h" |
27 | | #include "cpl_string.h" |
28 | | #include "cpl_vsi.h" |
29 | | #include "cpl_compressor.h" |
30 | | #include "gdal_alg.h" |
31 | | #include "gdal_alg_priv.h" |
32 | | #include "gdal.h" |
33 | | #include "gdal_pam.h" |
34 | | #include "gdalplugindriverproxy.h" |
35 | | #include "gdal_version_full/gdal_version.h" |
36 | | #include "gdal_thread_pool.h" |
37 | | #include "ogr_srs_api.h" |
38 | | #include "ograpispy.h" |
39 | | #ifdef HAVE_XERCES |
40 | | #include "ogr_xerces.h" |
41 | | #endif // HAVE_XERCES |
42 | | |
43 | | #ifdef _MSC_VER |
44 | | #ifdef MSVC_USE_VLD |
45 | | #include <wchar.h> |
46 | | #include <vld.h> |
47 | | #endif |
48 | | #endif |
49 | | |
50 | | // FIXME: Disabled following code as it crashed on OSX CI test. |
51 | | // #include <mutex> |
52 | | |
53 | | /************************************************************************/ |
54 | | /* ==================================================================== */ |
55 | | /* GDALDriverManager */ |
56 | | /* ==================================================================== */ |
57 | | /************************************************************************/ |
58 | | |
59 | | static volatile GDALDriverManager *poDM = nullptr; |
60 | | static CPLMutex *hDMMutex = nullptr; |
61 | | |
62 | | // FIXME: Disabled following code as it crashed on OSX CI test. |
63 | | // static std::mutex oDeleteMutex; |
64 | | |
65 | | CPLMutex **GDALGetphDMMutex() |
66 | 0 | { |
67 | 0 | return &hDMMutex; |
68 | 0 | } |
69 | | |
70 | | /************************************************************************/ |
71 | | /* GetGDALDriverManager() */ |
72 | | /* */ |
73 | | /* A freestanding function to get the only instance of the */ |
74 | | /* GDALDriverManager. */ |
75 | | /************************************************************************/ |
76 | | |
77 | | /** |
78 | | * \brief Fetch the global GDAL driver manager. |
79 | | * |
80 | | * This function fetches the pointer to the singleton global driver manager. |
81 | | * If the driver manager doesn't exist it is automatically created. |
82 | | * |
83 | | * @return pointer to the global driver manager. This should not be able |
84 | | * to fail. |
85 | | */ |
86 | | |
87 | | GDALDriverManager *GetGDALDriverManager() |
88 | | |
89 | 0 | { |
90 | 0 | if (poDM == nullptr) |
91 | 0 | { |
92 | 0 | CPLMutexHolderD(&hDMMutex); |
93 | | // cppcheck-suppress identicalInnerCondition |
94 | 0 | if (poDM == nullptr) |
95 | 0 | poDM = new GDALDriverManager(); |
96 | 0 | } |
97 | |
|
98 | 0 | CPLAssert(nullptr != poDM); |
99 | | |
100 | 0 | return const_cast<GDALDriverManager *>(poDM); |
101 | 0 | } |
102 | | |
103 | | /************************************************************************/ |
104 | | /* GDALDriverManager() */ |
105 | | /************************************************************************/ |
106 | | |
107 | 0 | #define XSTRINGIFY(x) #x |
108 | 0 | #define STRINGIFY(x) XSTRINGIFY(x) |
109 | | |
110 | | GDALDriverManager::GDALDriverManager() |
111 | 0 | { |
112 | 0 | CPLAssert(poDM == nullptr); |
113 | | |
114 | 0 | CPLLoadConfigOptionsFromPredefinedFiles(); |
115 | |
|
116 | 0 | CPLHTTPSetDefaultUserAgent( |
117 | 0 | "GDAL/" STRINGIFY(GDAL_VERSION_MAJOR) "." STRINGIFY( |
118 | 0 | GDAL_VERSION_MINOR) "." STRINGIFY(GDAL_VERSION_REV)); |
119 | | |
120 | | /* -------------------------------------------------------------------- */ |
121 | | /* We want to push a location to search for data files */ |
122 | | /* supporting GDAL/OGR such as EPSG csv files, S-57 definition */ |
123 | | /* files, and so forth. Use the INST_DATA macro (setup at */ |
124 | | /* configure time) if available. Otherwise we don't push anything */ |
125 | | /* and we hope other mechanisms such as environment variables will */ |
126 | | /* have been employed. */ |
127 | | /* -------------------------------------------------------------------- */ |
128 | 0 | #ifdef INST_DATA |
129 | 0 | if (CPLGetConfigOption("GDAL_DATA", nullptr) != nullptr) |
130 | 0 | { |
131 | | // This one is picked up automatically by finder initialization. |
132 | 0 | } |
133 | 0 | else |
134 | 0 | { |
135 | 0 | CPLPushFinderLocation(INST_DATA); |
136 | 0 | } |
137 | 0 | #endif |
138 | 0 | } |
139 | | |
140 | | /************************************************************************/ |
141 | | /* ~GDALDriverManager() */ |
142 | | /************************************************************************/ |
143 | | |
144 | | // Keep these two in sync with gdalproxypool.cpp. |
145 | | void GDALDatasetPoolPreventDestroy(); |
146 | | void GDALDatasetPoolForceDestroy(); |
147 | | |
148 | | GDALDriverManager::~GDALDriverManager() |
149 | | |
150 | 0 | { |
151 | | /* -------------------------------------------------------------------- */ |
152 | | /* Cleanup any open datasets. */ |
153 | | /* -------------------------------------------------------------------- */ |
154 | | |
155 | | // We have to prevent the destroying of the dataset pool during this first |
156 | | // phase, otherwise it cause crashes with a VRT B referencing a VRT A, and |
157 | | // if CloseDependentDatasets() is called first on VRT A. |
158 | | // If we didn't do this nasty trick, due to the refCountOfDisableRefCount |
159 | | // mechanism that cheats the real refcount of the dataset pool, we might |
160 | | // destroy the dataset pool too early, leading the VRT A to |
161 | | // destroy itself indirectly ... Ok, I am aware this explanation does |
162 | | // not make any sense unless you try it under a debugger ... |
163 | | // When people just manipulate "top-level" dataset handles, we luckily |
164 | | // don't need this horrible hack, but GetOpenDatasets() expose "low-level" |
165 | | // datasets, which defeat some "design" of the proxy pool. |
166 | 0 | GDALDatasetPoolPreventDestroy(); |
167 | | |
168 | | // First begin by requesting each remaining dataset to drop any reference |
169 | | // to other datasets. |
170 | 0 | bool bHasDroppedRef = false; |
171 | |
|
172 | 0 | do |
173 | 0 | { |
174 | 0 | int nDSCount = 0; |
175 | 0 | GDALDataset **papoDSList = GDALDataset::GetOpenDatasets(&nDSCount); |
176 | | |
177 | | // If a dataset has dropped a reference, the list might have become |
178 | | // invalid, so go out of the loop and try again with the new valid |
179 | | // list. |
180 | 0 | bHasDroppedRef = false; |
181 | 0 | for (int i = 0; i < nDSCount && !bHasDroppedRef; ++i) |
182 | 0 | { |
183 | | #if DEBUG_VERBOSE |
184 | | CPLDebug("GDAL", "Call CloseDependentDatasets() on %s", |
185 | | papoDSList[i]->GetDescription()); |
186 | | #endif // DEBUG_VERBOSE |
187 | 0 | bHasDroppedRef = |
188 | 0 | CPL_TO_BOOL(papoDSList[i]->CloseDependentDatasets()); |
189 | 0 | } |
190 | 0 | } while (bHasDroppedRef); |
191 | | |
192 | | // Now let's destroy the dataset pool. Nobody should use it afterwards |
193 | | // if people have well released their dependent datasets above. |
194 | 0 | GDALDatasetPoolForceDestroy(); |
195 | | |
196 | | // Now close the stand-alone datasets. |
197 | 0 | int nDSCount = 0; |
198 | 0 | GDALDataset **papoDSList = GDALDataset::GetOpenDatasets(&nDSCount); |
199 | 0 | for (int i = 0; i < nDSCount; ++i) |
200 | 0 | { |
201 | 0 | CPLDebug("GDAL", "Force close of %s (%p) in GDALDriverManager cleanup.", |
202 | 0 | papoDSList[i]->GetDescription(), papoDSList[i]); |
203 | | // Destroy with delete operator rather than GDALClose() to force |
204 | | // deletion of datasets with multiple reference count. |
205 | | // We could also iterate while GetOpenDatasets() returns a non NULL |
206 | | // list. |
207 | 0 | delete papoDSList[i]; |
208 | 0 | } |
209 | | |
210 | | /* -------------------------------------------------------------------- */ |
211 | | /* Destroy the existing drivers. */ |
212 | | /* -------------------------------------------------------------------- */ |
213 | 0 | while (GetDriverCount() > 0) |
214 | 0 | { |
215 | 0 | GDALDriver *poDriver = GetDriver(0); |
216 | |
|
217 | 0 | DeregisterDriver(poDriver); |
218 | 0 | delete poDriver; |
219 | 0 | } |
220 | |
|
221 | 0 | { |
222 | 0 | auto oIter = oMapNameToDrivers.find("MEMORY"); |
223 | 0 | if (oIter != oMapNameToDrivers.end()) |
224 | 0 | delete oIter->second; |
225 | 0 | } |
226 | |
|
227 | 0 | CleanupPythonDrivers(); |
228 | |
|
229 | 0 | GDALDestroyGlobalThreadPool(); |
230 | | |
231 | | /* -------------------------------------------------------------------- */ |
232 | | /* Cleanup local memory. */ |
233 | | /* -------------------------------------------------------------------- */ |
234 | 0 | VSIFree(papoDrivers); |
235 | | |
236 | | /* -------------------------------------------------------------------- */ |
237 | | /* Cleanup any Proxy related memory. */ |
238 | | /* -------------------------------------------------------------------- */ |
239 | 0 | PamCleanProxyDB(); |
240 | | |
241 | | /* -------------------------------------------------------------------- */ |
242 | | /* Cleanup any memory allocated by the OGRSpatialReference */ |
243 | | /* related subsystem. */ |
244 | | /* -------------------------------------------------------------------- */ |
245 | 0 | OSRCleanup(); |
246 | | |
247 | | /* -------------------------------------------------------------------- */ |
248 | | /* Blow away all the finder hints paths. We really should not */ |
249 | | /* be doing all of them, but it is currently hard to keep track */ |
250 | | /* of those that actually belong to us. */ |
251 | | /* -------------------------------------------------------------------- */ |
252 | 0 | CPLFinderClean(); |
253 | 0 | CPLFreeConfig(); |
254 | 0 | CPLCleanupSharedFileMutex(); |
255 | |
|
256 | | #ifdef HAVE_XERCES |
257 | | OGRCleanupXercesMutex(); |
258 | | #endif |
259 | |
|
260 | 0 | #ifdef OGRAPISPY_ENABLED |
261 | 0 | OGRAPISpyDestroyMutex(); |
262 | 0 | #endif |
263 | | |
264 | | /* -------------------------------------------------------------------- */ |
265 | | /* Cleanup VSIFileManager. */ |
266 | | /* -------------------------------------------------------------------- */ |
267 | 0 | VSICleanupFileManager(); |
268 | 0 | CPLDestroyCompressorRegistry(); |
269 | | |
270 | | /* -------------------------------------------------------------------- */ |
271 | | /* Cleanup thread local storage ... I hope the program is all */ |
272 | | /* done with GDAL/OGR! */ |
273 | | /* -------------------------------------------------------------------- */ |
274 | 0 | CPLCleanupTLS(); |
275 | | |
276 | | /* -------------------------------------------------------------------- */ |
277 | | /* Cleanup our mutex. */ |
278 | | /* -------------------------------------------------------------------- */ |
279 | 0 | if (hDMMutex) |
280 | 0 | { |
281 | 0 | CPLDestroyMutex(hDMMutex); |
282 | 0 | hDMMutex = nullptr; |
283 | 0 | } |
284 | | |
285 | | /* -------------------------------------------------------------------- */ |
286 | | /* Cleanup dataset list mutex. */ |
287 | | /* -------------------------------------------------------------------- */ |
288 | 0 | if (*GDALGetphDLMutex() != nullptr) |
289 | 0 | { |
290 | 0 | CPLDestroyMutex(*GDALGetphDLMutex()); |
291 | 0 | *GDALGetphDLMutex() = nullptr; |
292 | 0 | } |
293 | | |
294 | | /* -------------------------------------------------------------------- */ |
295 | | /* Cleanup raster block mutex. */ |
296 | | /* -------------------------------------------------------------------- */ |
297 | 0 | GDALRasterBlock::DestroyRBMutex(); |
298 | | |
299 | | /* -------------------------------------------------------------------- */ |
300 | | /* Cleanup gdaltransformer.cpp mutex. */ |
301 | | /* -------------------------------------------------------------------- */ |
302 | 0 | GDALCleanupTransformDeserializerMutex(); |
303 | | |
304 | | /* -------------------------------------------------------------------- */ |
305 | | /* Cleanup cpl_error.cpp mutex. */ |
306 | | /* -------------------------------------------------------------------- */ |
307 | 0 | CPLCleanupErrorMutex(); |
308 | | |
309 | | /* -------------------------------------------------------------------- */ |
310 | | /* Cleanup CPLsetlocale mutex. */ |
311 | | /* -------------------------------------------------------------------- */ |
312 | 0 | CPLCleanupSetlocaleMutex(); |
313 | | |
314 | | /* -------------------------------------------------------------------- */ |
315 | | /* Cleanup curl related stuff. */ |
316 | | /* -------------------------------------------------------------------- */ |
317 | 0 | CPLHTTPCleanup(); |
318 | | |
319 | | /* -------------------------------------------------------------------- */ |
320 | | /* Cleanup the master CPL mutex, which governs the creation */ |
321 | | /* of all other mutexes. */ |
322 | | /* -------------------------------------------------------------------- */ |
323 | 0 | CPLCleanupMasterMutex(); |
324 | | |
325 | | /* -------------------------------------------------------------------- */ |
326 | | /* Ensure the global driver manager pointer is NULLed out. */ |
327 | | /* -------------------------------------------------------------------- */ |
328 | 0 | if (poDM == this) |
329 | 0 | poDM = nullptr; |
330 | 0 | } |
331 | | |
332 | | /************************************************************************/ |
333 | | /* GetDriverCount() */ |
334 | | /************************************************************************/ |
335 | | |
336 | | /** |
337 | | * \brief Fetch the number of registered drivers. |
338 | | * |
339 | | * This C analog to this is GDALGetDriverCount(). |
340 | | * |
341 | | * @return the number of registered drivers. |
342 | | */ |
343 | | |
344 | | int GDALDriverManager::GetDriverCount() const |
345 | | |
346 | 0 | { |
347 | 0 | return nDrivers; |
348 | 0 | } |
349 | | |
350 | | //! @cond Doxygen_Suppress |
351 | | int GDALDriverManager::GetDriverCount(bool bIncludeHidden) const |
352 | | |
353 | 0 | { |
354 | 0 | if (!bIncludeHidden) |
355 | 0 | return nDrivers; |
356 | 0 | return nDrivers + static_cast<int>(m_aoHiddenDrivers.size()); |
357 | 0 | } |
358 | | |
359 | | //! @endcond |
360 | | |
361 | | /************************************************************************/ |
362 | | /* IsKnownDriver() */ |
363 | | /************************************************************************/ |
364 | | |
365 | | //! @cond Doxygen_Suppress |
366 | | bool GDALDriverManager::IsKnownDriver(const char *pszDriverName) const |
367 | 0 | { |
368 | 0 | CPLMutexHolderD(&hDMMutex); |
369 | 0 | if (cpl::contains(oMapNameToDrivers, CPLString(pszDriverName).toupper())) |
370 | 0 | return true; |
371 | 0 | for (const auto &poDriver : m_aoHiddenDrivers) |
372 | 0 | { |
373 | 0 | if (EQUAL(poDriver->GetDescription(), pszDriverName)) |
374 | 0 | return true; |
375 | 0 | } |
376 | 0 | return false; |
377 | 0 | } |
378 | | |
379 | | //! @endcond |
380 | | |
381 | | /************************************************************************/ |
382 | | /* GetHiddenDriverByName() */ |
383 | | /************************************************************************/ |
384 | | |
385 | | //! @cond Doxygen_Suppress |
386 | | GDALDriver *GDALDriverManager::GetHiddenDriverByName(const char *pszName) |
387 | 0 | { |
388 | 0 | CPLMutexHolderD(&hDMMutex); |
389 | 0 | for (const auto &poDriver : m_aoHiddenDrivers) |
390 | 0 | { |
391 | 0 | if (EQUAL(poDriver->GetDescription(), pszName)) |
392 | 0 | return poDriver.get(); |
393 | 0 | } |
394 | 0 | return nullptr; |
395 | 0 | } |
396 | | |
397 | | //! @endcond |
398 | | |
399 | | /************************************************************************/ |
400 | | /* GDALGetDriverCount() */ |
401 | | /************************************************************************/ |
402 | | |
403 | | /** |
404 | | * \brief Fetch the number of registered drivers. |
405 | | * |
406 | | * @see GDALDriverManager::GetDriverCount() |
407 | | */ |
408 | | |
409 | | int CPL_STDCALL GDALGetDriverCount() |
410 | | |
411 | 0 | { |
412 | 0 | return GetGDALDriverManager()->GetDriverCount(); |
413 | 0 | } |
414 | | |
415 | | /************************************************************************/ |
416 | | /* GetDriver() */ |
417 | | /************************************************************************/ |
418 | | |
419 | | /** |
420 | | * \brief Fetch driver by index. |
421 | | * |
422 | | * This C analog to this is GDALGetDriver(). |
423 | | * |
424 | | * @param iDriver the driver index from 0 to GetDriverCount()-1. |
425 | | * |
426 | | * @return the driver identified by the index or NULL if the index is invalid |
427 | | */ |
428 | | |
429 | | GDALDriver *GDALDriverManager::GetDriver(int iDriver) |
430 | | |
431 | 0 | { |
432 | 0 | CPLMutexHolderD(&hDMMutex); |
433 | |
|
434 | 0 | return GetDriver_unlocked(iDriver); |
435 | 0 | } |
436 | | |
437 | | //! @cond Doxygen_Suppress |
438 | | GDALDriver *GDALDriverManager::GetDriver(int iDriver, bool bIncludeHidden) |
439 | | |
440 | 0 | { |
441 | 0 | CPLMutexHolderD(&hDMMutex); |
442 | 0 | if (!bIncludeHidden || iDriver < nDrivers) |
443 | 0 | return GetDriver_unlocked(iDriver); |
444 | 0 | if (iDriver - nDrivers < static_cast<int>(m_aoHiddenDrivers.size())) |
445 | 0 | return m_aoHiddenDrivers[iDriver - nDrivers].get(); |
446 | 0 | return nullptr; |
447 | 0 | } |
448 | | |
449 | | //! @endcond |
450 | | |
451 | | /************************************************************************/ |
452 | | /* GDALGetDriver() */ |
453 | | /************************************************************************/ |
454 | | |
455 | | /** |
456 | | * \brief Fetch driver by index. |
457 | | * |
458 | | * @see GDALDriverManager::GetDriver() |
459 | | */ |
460 | | |
461 | | GDALDriverH CPL_STDCALL GDALGetDriver(int iDriver) |
462 | | |
463 | 0 | { |
464 | 0 | return /* (GDALDriverH) */ GetGDALDriverManager()->GetDriver(iDriver); |
465 | 0 | } |
466 | | |
467 | | /************************************************************************/ |
468 | | /* RegisterDriver() */ |
469 | | /************************************************************************/ |
470 | | |
471 | | /** |
472 | | * \brief Register a driver for use. |
473 | | * |
474 | | * The C analog is GDALRegisterDriver(). |
475 | | * |
476 | | * Normally this method is used by format specific C callable registration |
477 | | * entry points such as GDALRegister_GTiff() rather than being called |
478 | | * directly by application level code. |
479 | | * |
480 | | * If this driver (based on the object pointer, not short name) is already |
481 | | * registered, then no change is made, and the index of the existing driver |
482 | | * is returned. Otherwise the driver list is extended, and the new driver |
483 | | * is added at the end. |
484 | | * |
485 | | * @param poDriver the driver to register. |
486 | | * |
487 | | * @return the index of the new installed driver. |
488 | | */ |
489 | | |
490 | | int GDALDriverManager::RegisterDriver(GDALDriver *poDriver) |
491 | 0 | { |
492 | 0 | return RegisterDriver(poDriver, /*bHidden=*/false); |
493 | 0 | } |
494 | | |
495 | | int GDALDriverManager::RegisterDriver(GDALDriver *poDriver, bool bHidden) |
496 | 0 | { |
497 | 0 | CPLMutexHolderD(&hDMMutex); |
498 | | |
499 | | /* -------------------------------------------------------------------- */ |
500 | | /* If it is already registered, just return the existing */ |
501 | | /* index. */ |
502 | | /* -------------------------------------------------------------------- */ |
503 | 0 | if (!m_bInDeferredDriverLoading && |
504 | 0 | GetDriverByName_unlocked(poDriver->GetDescription()) != nullptr) |
505 | 0 | { |
506 | 0 | for (int i = 0; i < nDrivers; ++i) |
507 | 0 | { |
508 | 0 | if (papoDrivers[i] == poDriver) |
509 | 0 | { |
510 | 0 | return i; |
511 | 0 | } |
512 | 0 | } |
513 | | |
514 | 0 | CPLAssert(false); |
515 | 0 | } |
516 | | |
517 | 0 | if (poDriver->pfnOpen != nullptr || |
518 | 0 | poDriver->pfnOpenWithDriverArg != nullptr) |
519 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES"); |
520 | |
|
521 | 0 | if (poDriver->pfnCreate != nullptr || poDriver->pfnCreateEx != nullptr) |
522 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_CREATE, "YES"); |
523 | |
|
524 | 0 | if (poDriver->pfnCreateCopy != nullptr) |
525 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES"); |
526 | |
|
527 | 0 | if (poDriver->pfnCreateMultiDimensional != nullptr) |
528 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_CREATE_MULTIDIMENSIONAL, "YES"); |
529 | | |
530 | | // Backward compatibility for GDAL raster out-of-tree drivers: |
531 | | // If a driver hasn't explicitly set a vector capability, assume it is |
532 | | // a raster-only driver (legacy OGR drivers will have DCAP_VECTOR set before |
533 | | // calling RegisterDriver()). |
534 | 0 | if (poDriver->GetMetadataItem(GDAL_DCAP_RASTER) == nullptr && |
535 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr && |
536 | 0 | poDriver->GetMetadataItem(GDAL_DCAP_GNM) == nullptr) |
537 | 0 | { |
538 | 0 | CPLDebug("GDAL", "Assuming DCAP_RASTER for driver %s. Please fix it.", |
539 | 0 | poDriver->GetDescription()); |
540 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); |
541 | 0 | } |
542 | |
|
543 | 0 | if (poDriver->GetMetadataItem(GDAL_DMD_OPENOPTIONLIST) != nullptr && |
544 | 0 | poDriver->pfnIdentify == nullptr && |
545 | 0 | poDriver->pfnIdentifyEx == nullptr && |
546 | 0 | !STARTS_WITH_CI(poDriver->GetDescription(), "Interlis")) |
547 | 0 | { |
548 | 0 | CPLDebug("GDAL", |
549 | 0 | "Driver %s that defines GDAL_DMD_OPENOPTIONLIST must also " |
550 | 0 | "implement Identify(), so that it can be used", |
551 | 0 | poDriver->GetDescription()); |
552 | 0 | } |
553 | |
|
554 | 0 | if (poDriver->pfnVectorTranslateFrom != nullptr) |
555 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_VECTOR_TRANSLATE_FROM, "YES"); |
556 | |
|
557 | 0 | if (m_bInDeferredDriverLoading && |
558 | 0 | cpl::contains(oMapNameToDrivers, |
559 | 0 | CPLString(poDriver->GetDescription()).toupper())) |
560 | 0 | { |
561 | 0 | if (cpl::contains(m_oMapRealDrivers, poDriver->GetDescription())) |
562 | 0 | { |
563 | 0 | CPLError( |
564 | 0 | CE_Failure, CPLE_AppDefined, |
565 | 0 | "RegisterDriver() in m_bInDeferredDriverLoading: %s already " |
566 | 0 | "registered!", |
567 | 0 | poDriver->GetDescription()); |
568 | 0 | delete poDriver; |
569 | 0 | return -1; |
570 | 0 | } |
571 | 0 | m_oMapRealDrivers[poDriver->GetDescription()] = |
572 | 0 | std::unique_ptr<GDALDriver>(poDriver); |
573 | 0 | return -1; |
574 | 0 | } |
575 | | |
576 | | /* -------------------------------------------------------------------- */ |
577 | | /* Otherwise grow the list to hold the new entry. */ |
578 | | /* -------------------------------------------------------------------- */ |
579 | 0 | if (bHidden) |
580 | 0 | { |
581 | 0 | m_aoHiddenDrivers.push_back(std::unique_ptr<GDALDriver>(poDriver)); |
582 | 0 | return -1; |
583 | 0 | } |
584 | | |
585 | 0 | GDALDriver **papoNewDrivers = |
586 | 0 | static_cast<GDALDriver **>(VSI_REALLOC_VERBOSE( |
587 | 0 | papoDrivers, sizeof(GDALDriver *) * (nDrivers + 1))); |
588 | 0 | if (papoNewDrivers == nullptr) |
589 | 0 | return -1; |
590 | 0 | papoDrivers = papoNewDrivers; |
591 | |
|
592 | 0 | papoDrivers[nDrivers] = poDriver; |
593 | 0 | ++nDrivers; |
594 | |
|
595 | 0 | oMapNameToDrivers[CPLString(poDriver->GetDescription()).toupper()] = |
596 | 0 | poDriver; |
597 | |
|
598 | 0 | if (EQUAL(poDriver->GetDescription(), "MEM") && |
599 | 0 | oMapNameToDrivers.find("MEMORY") == oMapNameToDrivers.end()) |
600 | 0 | { |
601 | | // Instantiate a Memory driver, that is the same as the MEM one, |
602 | | // for legacy purposes. It can be queried through GetDriverByName() |
603 | | // but doesn't appear in the driver list. |
604 | 0 | auto poMemoryDriver = new GDALDriver(); |
605 | 0 | poMemoryDriver->SetDescription("Memory"); |
606 | 0 | poMemoryDriver->SetMetadata(poDriver->GetMetadata()); |
607 | 0 | poMemoryDriver->pfnOpen = poDriver->pfnOpen; |
608 | 0 | poMemoryDriver->pfnIdentify = poDriver->pfnIdentify; |
609 | 0 | poMemoryDriver->pfnCreate = poDriver->pfnCreate; |
610 | 0 | poMemoryDriver->pfnCreateMultiDimensional = |
611 | 0 | poDriver->pfnCreateMultiDimensional; |
612 | 0 | poMemoryDriver->pfnDelete = poDriver->pfnDelete; |
613 | 0 | oMapNameToDrivers[CPLString(poMemoryDriver->GetDescription()) |
614 | 0 | .toupper()] = poMemoryDriver; |
615 | 0 | } |
616 | |
|
617 | 0 | int iResult = nDrivers - 1; |
618 | |
|
619 | 0 | return iResult; |
620 | 0 | } |
621 | | |
622 | | /************************************************************************/ |
623 | | /* GetDriverByName_unlocked() */ |
624 | | /************************************************************************/ |
625 | | |
626 | | GDALDriver * |
627 | | GDALDriverManager::GetDriverByName_unlocked(const char *pszName) const |
628 | 0 | { |
629 | 0 | const CPLString osName = CPLString(pszName).toupper(); |
630 | 0 | if (osName == "MEMORY") |
631 | 0 | { |
632 | 0 | CPLErrorOnce(CE_Warning, CPLE_AppDefined, |
633 | 0 | "DeprecationWarning: 'Memory' driver is deprecated since " |
634 | 0 | "GDAL 3.11. Use 'MEM' onwards"); |
635 | 0 | } |
636 | 0 | auto oIter = oMapNameToDrivers.find(osName); |
637 | 0 | return oIter == oMapNameToDrivers.end() ? nullptr : oIter->second; |
638 | 0 | } |
639 | | |
640 | | /************************************************************************/ |
641 | | /* GDALRegisterDriver() */ |
642 | | /************************************************************************/ |
643 | | |
644 | | /** |
645 | | * \brief Register a driver for use. |
646 | | * |
647 | | * @see GDALDriverManager::GetRegisterDriver() |
648 | | */ |
649 | | |
650 | | int CPL_STDCALL GDALRegisterDriver(GDALDriverH hDriver) |
651 | | |
652 | 0 | { |
653 | 0 | VALIDATE_POINTER1(hDriver, "GDALRegisterDriver", 0); |
654 | | |
655 | 0 | return GetGDALDriverManager()->RegisterDriver( |
656 | 0 | static_cast<GDALDriver *>(hDriver)); |
657 | 0 | } |
658 | | |
659 | | /************************************************************************/ |
660 | | /* DeregisterDriver() */ |
661 | | /************************************************************************/ |
662 | | |
663 | | /** |
664 | | * \brief Deregister the passed driver. |
665 | | * |
666 | | * If the driver isn't found no change is made. |
667 | | * |
668 | | * The C analog is GDALDeregisterDriver(). |
669 | | * |
670 | | * @param poDriver the driver to deregister. |
671 | | */ |
672 | | |
673 | | void GDALDriverManager::DeregisterDriver(GDALDriver *poDriver) |
674 | | |
675 | 0 | { |
676 | 0 | CPLMutexHolderD(&hDMMutex); |
677 | |
|
678 | 0 | int i = 0; // Used after for. |
679 | 0 | for (; i < nDrivers; ++i) |
680 | 0 | { |
681 | 0 | if (papoDrivers[i] == poDriver) |
682 | 0 | break; |
683 | 0 | } |
684 | |
|
685 | 0 | if (i == nDrivers) |
686 | 0 | return; |
687 | | |
688 | 0 | oMapNameToDrivers.erase(CPLString(poDriver->GetDescription()).toupper()); |
689 | 0 | --nDrivers; |
690 | | // Move all following drivers down by one to pack the list. |
691 | 0 | while (i < nDrivers) |
692 | 0 | { |
693 | 0 | papoDrivers[i] = papoDrivers[i + 1]; |
694 | 0 | ++i; |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | | /************************************************************************/ |
699 | | /* GDALDeregisterDriver() */ |
700 | | /************************************************************************/ |
701 | | |
702 | | /** |
703 | | * \brief Deregister the passed driver. |
704 | | * |
705 | | * @see GDALDriverManager::GetDeregisterDriver() |
706 | | */ |
707 | | |
708 | | void CPL_STDCALL GDALDeregisterDriver(GDALDriverH hDriver) |
709 | | |
710 | 0 | { |
711 | 0 | VALIDATE_POINTER0(hDriver, "GDALDeregisterDriver"); |
712 | | |
713 | 0 | GetGDALDriverManager()->DeregisterDriver( |
714 | 0 | static_cast<GDALDriver *>(hDriver)); |
715 | 0 | } |
716 | | |
717 | | /************************************************************************/ |
718 | | /* GetDriverByName() */ |
719 | | /************************************************************************/ |
720 | | |
721 | | /** |
722 | | * \brief Fetch a driver based on the short name. |
723 | | * |
724 | | * The C analog is the GDALGetDriverByName() function. |
725 | | * |
726 | | * @param pszName the short name, such as GTiff, being searched for. |
727 | | * |
728 | | * @return the identified driver, or NULL if no match is found. |
729 | | */ |
730 | | |
731 | | GDALDriver *GDALDriverManager::GetDriverByName(const char *pszName) |
732 | | |
733 | 0 | { |
734 | 0 | CPLMutexHolderD(&hDMMutex); |
735 | |
|
736 | 0 | if (m_bInDeferredDriverLoading) |
737 | 0 | { |
738 | 0 | return nullptr; |
739 | 0 | } |
740 | | |
741 | | // Alias old name to new name |
742 | 0 | if (EQUAL(pszName, "CartoDB")) |
743 | 0 | pszName = "Carto"; |
744 | |
|
745 | 0 | return GetDriverByName_unlocked(pszName); |
746 | 0 | } |
747 | | |
748 | | /************************************************************************/ |
749 | | /* GDALGetDriverByName() */ |
750 | | /************************************************************************/ |
751 | | |
752 | | /** |
753 | | * \brief Fetch a driver based on the short name. |
754 | | * |
755 | | * @see GDALDriverManager::GetDriverByName() |
756 | | */ |
757 | | |
758 | | GDALDriverH CPL_STDCALL GDALGetDriverByName(const char *pszName) |
759 | | |
760 | 0 | { |
761 | 0 | VALIDATE_POINTER1(pszName, "GDALGetDriverByName", nullptr); |
762 | | |
763 | 0 | return GetGDALDriverManager()->GetDriverByName(pszName); |
764 | 0 | } |
765 | | |
766 | | /************************************************************************/ |
767 | | /* AutoSkipDrivers() */ |
768 | | /************************************************************************/ |
769 | | |
770 | | /** |
771 | | * \brief This method unload undesirable drivers. |
772 | | * |
773 | | * All drivers specified in the comma delimited list in the GDAL_SKIP |
774 | | * environment variable) will be deregistered and destroyed. This method |
775 | | * should normally be called after registration of standard drivers to allow |
776 | | * the user a way of unloading undesired drivers. The GDALAllRegister() |
777 | | * function already invokes AutoSkipDrivers() at the end, so if that functions |
778 | | * is called, it should not be necessary to call this method from application |
779 | | * code. |
780 | | * |
781 | | * Note: space separator is also accepted for backward compatibility, but some |
782 | | * vector formats have spaces in their names, so it is encouraged to use comma |
783 | | * to avoid issues. |
784 | | */ |
785 | | |
786 | | void GDALDriverManager::AutoSkipDrivers() |
787 | | |
788 | 0 | { |
789 | 0 | char **apapszList[2] = {nullptr, nullptr}; |
790 | 0 | const char *pszGDAL_SKIP = CPLGetConfigOption("GDAL_SKIP", nullptr); |
791 | 0 | if (pszGDAL_SKIP != nullptr) |
792 | 0 | { |
793 | | // Favor comma as a separator. If not found, then use space. |
794 | 0 | const char *pszSep = (strchr(pszGDAL_SKIP, ',') != nullptr) ? "," : " "; |
795 | 0 | apapszList[0] = |
796 | 0 | CSLTokenizeStringComplex(pszGDAL_SKIP, pszSep, FALSE, FALSE); |
797 | 0 | } |
798 | 0 | const char *pszOGR_SKIP = CPLGetConfigOption("OGR_SKIP", nullptr); |
799 | 0 | if (pszOGR_SKIP != nullptr) |
800 | 0 | { |
801 | | // OGR has always used comma as a separator. |
802 | 0 | apapszList[1] = |
803 | 0 | CSLTokenizeStringComplex(pszOGR_SKIP, ",", FALSE, FALSE); |
804 | 0 | } |
805 | |
|
806 | 0 | for (auto j : {0, 1}) |
807 | 0 | { |
808 | 0 | for (int i = 0; apapszList[j] != nullptr && apapszList[j][i] != nullptr; |
809 | 0 | ++i) |
810 | 0 | { |
811 | 0 | GDALDriver *const poDriver = GetDriverByName(apapszList[j][i]); |
812 | |
|
813 | 0 | if (poDriver == nullptr) |
814 | 0 | { |
815 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
816 | 0 | "Unable to find driver %s to unload from GDAL_SKIP " |
817 | 0 | "environment variable.", |
818 | 0 | apapszList[j][i]); |
819 | 0 | } |
820 | 0 | else |
821 | 0 | { |
822 | 0 | CPLDebug("GDAL", "AutoSkipDriver(%s)", apapszList[j][i]); |
823 | 0 | DeregisterDriver(poDriver); |
824 | 0 | delete poDriver; |
825 | 0 | } |
826 | 0 | } |
827 | 0 | } |
828 | |
|
829 | 0 | CSLDestroy(apapszList[0]); |
830 | 0 | CSLDestroy(apapszList[1]); |
831 | 0 | } |
832 | | |
833 | | /************************************************************************/ |
834 | | /* GetSearchPaths() */ |
835 | | /************************************************************************/ |
836 | | |
837 | | //! @cond Doxygen_Suppress |
838 | | char **GDALDriverManager::GetSearchPaths(const char *pszGDAL_DRIVER_PATH) |
839 | 0 | { |
840 | 0 | char **papszSearchPaths = nullptr; |
841 | 0 | CPL_IGNORE_RET_VAL(pszGDAL_DRIVER_PATH); |
842 | 0 | #ifndef GDAL_NO_AUTOLOAD |
843 | 0 | if (pszGDAL_DRIVER_PATH != nullptr) |
844 | 0 | { |
845 | | #ifdef _WIN32 |
846 | | papszSearchPaths = |
847 | | CSLTokenizeStringComplex(pszGDAL_DRIVER_PATH, ";", TRUE, FALSE); |
848 | | #else |
849 | 0 | papszSearchPaths = |
850 | 0 | CSLTokenizeStringComplex(pszGDAL_DRIVER_PATH, ":", TRUE, FALSE); |
851 | 0 | #endif |
852 | 0 | } |
853 | 0 | else |
854 | 0 | { |
855 | 0 | #ifdef INSTALL_PLUGIN_FULL_DIR |
856 | | // CMake way |
857 | 0 | papszSearchPaths = |
858 | 0 | CSLAddString(papszSearchPaths, INSTALL_PLUGIN_FULL_DIR); |
859 | | #elif defined(GDAL_PREFIX) |
860 | | papszSearchPaths = CSLAddString(papszSearchPaths, |
861 | | #ifdef MACOSX_FRAMEWORK |
862 | | GDAL_PREFIX "/PlugIns"); |
863 | | #else |
864 | | GDAL_PREFIX "/lib/gdalplugins"); |
865 | | #endif |
866 | | #else |
867 | | char szExecPath[1024]; |
868 | | |
869 | | if (CPLGetExecPath(szExecPath, sizeof(szExecPath))) |
870 | | { |
871 | | papszSearchPaths = CSLAddString( |
872 | | papszSearchPaths, |
873 | | (CPLGetDirnameSafe(szExecPath) + "\\gdalplugins").c_str()); |
874 | | } |
875 | | else |
876 | | { |
877 | | papszSearchPaths = |
878 | | CSLAddString(papszSearchPaths, "/usr/local/lib/gdalplugins"); |
879 | | } |
880 | | #endif |
881 | |
|
882 | | #ifdef MACOSX_FRAMEWORK |
883 | | #define num2str(x) str(x) |
884 | | #define str(x) #x |
885 | | papszSearchPaths = CSLAddString( |
886 | | papszSearchPaths, |
887 | | "/Library/Application Support/GDAL/" num2str( |
888 | | GDAL_VERSION_MAJOR) "." num2str(GDAL_VERSION_MINOR) "/PlugIns"); |
889 | | #endif |
890 | 0 | } |
891 | 0 | #endif // GDAL_NO_AUTOLOAD |
892 | 0 | return papszSearchPaths; |
893 | 0 | } |
894 | | |
895 | | //! @endcond |
896 | | |
897 | | /************************************************************************/ |
898 | | /* LoadPlugin() */ |
899 | | /************************************************************************/ |
900 | | |
901 | | /** |
902 | | * \brief Load a single GDAL driver/plugin from shared libraries. |
903 | | * |
904 | | * This function will load a single named driver/plugin from shared libraries. |
905 | | * It searches the "driver path" for .so (or .dll) files named |
906 | | * "gdal_{name}.[so|dll|dylib]" or "ogr_{name}.[so|dll|dylib]", then tries to |
907 | | * call a function within them called GDALRegister_{name}(), or failing that |
908 | | * called GDALRegisterMe(). |
909 | | * |
910 | | * \see GDALDriverManager::AutoLoadDrivers() for the rules used to determine |
911 | | * which paths are searched for plugin library files. |
912 | | */ |
913 | | |
914 | | CPLErr GDALDriverManager::LoadPlugin(const char *name) |
915 | 0 | { |
916 | | #ifdef GDAL_NO_AUTOLOAD |
917 | | CPLDebug("GDAL", "GDALDriverManager::LoadPlugin() not compiled in."); |
918 | | return CE_Failure; |
919 | | #else |
920 | 0 | const char *pszGDAL_DRIVER_PATH = |
921 | 0 | CPLGetConfigOption("GDAL_DRIVER_PATH", nullptr); |
922 | 0 | if (pszGDAL_DRIVER_PATH == nullptr) |
923 | 0 | pszGDAL_DRIVER_PATH = CPLGetConfigOption("OGR_DRIVER_PATH", nullptr); |
924 | | |
925 | | /* -------------------------------------------------------------------- */ |
926 | | /* Where should we look for stuff? */ |
927 | | /* -------------------------------------------------------------------- */ |
928 | 0 | const CPLStringList aosSearchPaths(GetSearchPaths(pszGDAL_DRIVER_PATH)); |
929 | | |
930 | | /* -------------------------------------------------------------------- */ |
931 | | /* Format the ABI version specific subdirectory to look in. */ |
932 | | /* -------------------------------------------------------------------- */ |
933 | 0 | CPLString osABIVersion; |
934 | |
|
935 | 0 | osABIVersion.Printf("%d.%d", GDAL_VERSION_MAJOR, GDAL_VERSION_MINOR); |
936 | | |
937 | | /* -------------------------------------------------------------------- */ |
938 | | /* Scan each directory looking for files matching */ |
939 | | /* gdal_{name}.[so|dll|dylib] or ogr_{name}.[so|dll|dylib] */ |
940 | | /* -------------------------------------------------------------------- */ |
941 | 0 | const int nSearchPaths = aosSearchPaths.size(); |
942 | 0 | for (int iDir = 0; iDir < nSearchPaths; ++iDir) |
943 | 0 | { |
944 | 0 | std::string osABISpecificDir = |
945 | 0 | CPLFormFilenameSafe(aosSearchPaths[iDir], osABIVersion, nullptr); |
946 | |
|
947 | 0 | VSIStatBufL sStatBuf; |
948 | 0 | if (VSIStatL(osABISpecificDir.c_str(), &sStatBuf) != 0) |
949 | 0 | osABISpecificDir = aosSearchPaths[iDir]; |
950 | |
|
951 | 0 | CPLString gdal_or_ogr[2] = {"gdal_", "ogr_"}; |
952 | 0 | CPLString platformExtensions[3] = {"so", "dll", "dylib"}; |
953 | |
|
954 | 0 | for (const CPLString &prefix : gdal_or_ogr) |
955 | 0 | { |
956 | 0 | for (const CPLString &extension : platformExtensions) |
957 | 0 | { |
958 | 0 | const std::string osFilename = CPLFormFilenameSafe( |
959 | 0 | osABISpecificDir.c_str(), |
960 | 0 | CPLSPrintf("%s%s", prefix.c_str(), name), extension); |
961 | 0 | if (VSIStatL(osFilename.c_str(), &sStatBuf) != 0) |
962 | 0 | continue; |
963 | | |
964 | 0 | CPLString osFuncName; |
965 | 0 | if (EQUAL(prefix, "gdal_")) |
966 | 0 | { |
967 | 0 | osFuncName.Printf("GDALRegister_%s", name); |
968 | 0 | } |
969 | 0 | else |
970 | 0 | { |
971 | 0 | osFuncName.Printf("RegisterOGR%s", name); |
972 | 0 | } |
973 | 0 | CPLErrorReset(); |
974 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
975 | 0 | void *pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
976 | 0 | CPLPopErrorHandler(); |
977 | 0 | if (pRegister == nullptr) |
978 | 0 | { |
979 | 0 | CPLString osLastErrorMsg(CPLGetLastErrorMsg()); |
980 | 0 | osFuncName = "GDALRegisterMe"; |
981 | 0 | pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
982 | 0 | if (pRegister == nullptr) |
983 | 0 | { |
984 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s", |
985 | 0 | osLastErrorMsg.c_str()); |
986 | 0 | return CE_Failure; |
987 | 0 | } |
988 | 0 | } |
989 | 0 | CPLDebug("GDAL", "Registering %s using %s in %s", name, |
990 | 0 | osFuncName.c_str(), osFilename.c_str()); |
991 | 0 | CPLErrorReset(); |
992 | 0 | reinterpret_cast<void (*)()>(pRegister)(); |
993 | 0 | if (CPLGetErrorCounter() > 0) |
994 | 0 | { |
995 | 0 | return CE_Failure; |
996 | 0 | } |
997 | 0 | return CE_None; |
998 | 0 | } |
999 | 0 | } |
1000 | 0 | } |
1001 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1002 | 0 | "Failed to find driver %s in configured driver paths.", name); |
1003 | 0 | return CE_Failure; |
1004 | 0 | #endif // GDAL_NO_AUTOLOAD |
1005 | 0 | } |
1006 | | |
1007 | | /************************************************************************/ |
1008 | | /* AutoLoadDrivers() */ |
1009 | | /************************************************************************/ |
1010 | | |
1011 | | /** |
1012 | | * \brief Auto-load GDAL drivers from shared libraries. |
1013 | | * |
1014 | | * This function will automatically load drivers from shared libraries. It |
1015 | | * searches the "driver path" for .so (or .dll) files that start with the |
1016 | | * prefix "gdal_X.so". It then tries to load them and then tries to call a |
1017 | | * function within them called GDALRegister_X() where the 'X' is the same as |
1018 | | * the remainder of the shared library basename ('X' is case sensitive), or |
1019 | | * failing that to call GDALRegisterMe(). |
1020 | | * |
1021 | | * There are a few rules for the driver path. If the GDAL_DRIVER_PATH |
1022 | | * environment variable it set, it is taken to be a list of directories to |
1023 | | * search separated by colons on UNIX, or semi-colons on Windows. Otherwise |
1024 | | * the /usr/local/lib/gdalplugins directory, and (if known) the |
1025 | | * lib/gdalplugins subdirectory of the gdal home directory are searched on |
1026 | | * UNIX and \$(BINDIR)\\gdalplugins on Windows. |
1027 | | * |
1028 | | * Auto loading can be completely disabled by setting the GDAL_DRIVER_PATH |
1029 | | * config option to "disable". |
1030 | | * |
1031 | | * Starting with gdal 3.5, the default search path \$(prefix)/lib/gdalplugins |
1032 | | * can be overridden at compile time by passing |
1033 | | * -DINSTALL_PLUGIN_DIR=/another/path to cmake. |
1034 | | */ |
1035 | | |
1036 | | void GDALDriverManager::AutoLoadDrivers() |
1037 | | |
1038 | 0 | { |
1039 | | #ifdef GDAL_NO_AUTOLOAD |
1040 | | CPLDebug("GDAL", "GDALDriverManager::AutoLoadDrivers() not compiled in."); |
1041 | | #else |
1042 | 0 | const char *pszGDAL_DRIVER_PATH = |
1043 | 0 | CPLGetConfigOption("GDAL_DRIVER_PATH", nullptr); |
1044 | 0 | if (pszGDAL_DRIVER_PATH == nullptr) |
1045 | 0 | pszGDAL_DRIVER_PATH = CPLGetConfigOption("OGR_DRIVER_PATH", nullptr); |
1046 | | |
1047 | | /* -------------------------------------------------------------------- */ |
1048 | | /* Allow applications to completely disable this search by */ |
1049 | | /* setting the driver path to the special string "disable". */ |
1050 | | /* -------------------------------------------------------------------- */ |
1051 | 0 | if (pszGDAL_DRIVER_PATH != nullptr && EQUAL(pszGDAL_DRIVER_PATH, "disable")) |
1052 | 0 | { |
1053 | 0 | CPLDebug("GDAL", "GDALDriverManager::AutoLoadDrivers() disabled."); |
1054 | 0 | return; |
1055 | 0 | } |
1056 | | |
1057 | | /* -------------------------------------------------------------------- */ |
1058 | | /* Where should we look for stuff? */ |
1059 | | /* -------------------------------------------------------------------- */ |
1060 | 0 | char **papszSearchPaths = GetSearchPaths(pszGDAL_DRIVER_PATH); |
1061 | | |
1062 | | /* -------------------------------------------------------------------- */ |
1063 | | /* Format the ABI version specific subdirectory to look in. */ |
1064 | | /* -------------------------------------------------------------------- */ |
1065 | 0 | CPLString osABIVersion; |
1066 | |
|
1067 | 0 | osABIVersion.Printf("%d.%d", GDAL_VERSION_MAJOR, GDAL_VERSION_MINOR); |
1068 | | |
1069 | | /* -------------------------------------------------------------------- */ |
1070 | | /* Scan each directory looking for files starting with gdal_ */ |
1071 | | /* -------------------------------------------------------------------- */ |
1072 | 0 | const int nSearchPaths = CSLCount(papszSearchPaths); |
1073 | 0 | bool bFoundOnePlugin = false; |
1074 | 0 | for (int iDir = 0; iDir < nSearchPaths; ++iDir) |
1075 | 0 | { |
1076 | 0 | std::string osABISpecificDir = |
1077 | 0 | CPLFormFilenameSafe(papszSearchPaths[iDir], osABIVersion, nullptr); |
1078 | |
|
1079 | 0 | VSIStatBufL sStatBuf; |
1080 | 0 | if (VSIStatL(osABISpecificDir.c_str(), &sStatBuf) != 0) |
1081 | 0 | osABISpecificDir = papszSearchPaths[iDir]; |
1082 | |
|
1083 | 0 | char **papszFiles = VSIReadDir(osABISpecificDir.c_str()); |
1084 | 0 | const int nFileCount = CSLCount(papszFiles); |
1085 | |
|
1086 | 0 | for (int iFile = 0; iFile < nFileCount; ++iFile) |
1087 | 0 | { |
1088 | 0 | const CPLString osExtension = |
1089 | 0 | CPLGetExtensionSafe(papszFiles[iFile]); |
1090 | |
|
1091 | 0 | if (!EQUAL(osExtension, "dll") && !EQUAL(osExtension, "so") && |
1092 | 0 | !EQUAL(osExtension, "dylib")) |
1093 | 0 | { |
1094 | 0 | if (strcmp(papszFiles[iFile], "drivers.ini") == 0) |
1095 | 0 | { |
1096 | 0 | m_osDriversIniPath = CPLFormFilenameSafe( |
1097 | 0 | osABISpecificDir.c_str(), papszFiles[iFile], nullptr); |
1098 | 0 | } |
1099 | 0 | continue; |
1100 | 0 | } |
1101 | | |
1102 | 0 | if (cpl::contains(m_oSetPluginFileNames, papszFiles[iFile])) |
1103 | 0 | { |
1104 | 0 | continue; |
1105 | 0 | } |
1106 | | |
1107 | 0 | CPLString osFuncName; |
1108 | 0 | if (STARTS_WITH_CI(papszFiles[iFile], "gdal_")) |
1109 | 0 | { |
1110 | 0 | osFuncName.Printf( |
1111 | 0 | "GDALRegister_%s", |
1112 | 0 | CPLGetBasenameSafe(papszFiles[iFile]).c_str() + |
1113 | 0 | strlen("gdal_")); |
1114 | 0 | } |
1115 | 0 | else if (STARTS_WITH_CI(papszFiles[iFile], "ogr_")) |
1116 | 0 | { |
1117 | 0 | osFuncName.Printf( |
1118 | 0 | "RegisterOGR%s", |
1119 | 0 | CPLGetBasenameSafe(papszFiles[iFile]).c_str() + |
1120 | 0 | strlen("ogr_")); |
1121 | 0 | } |
1122 | 0 | else |
1123 | 0 | continue; |
1124 | | |
1125 | 0 | const std::string osFilename = CPLFormFilenameSafe( |
1126 | 0 | osABISpecificDir.c_str(), papszFiles[iFile], nullptr); |
1127 | |
|
1128 | 0 | CPLErrorReset(); |
1129 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1130 | 0 | void *pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
1131 | 0 | CPLPopErrorHandler(); |
1132 | 0 | if (pRegister == nullptr) |
1133 | 0 | { |
1134 | 0 | CPLString osLastErrorMsg(CPLGetLastErrorMsg()); |
1135 | 0 | osFuncName = "GDALRegisterMe"; |
1136 | 0 | pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
1137 | 0 | if (pRegister == nullptr) |
1138 | 0 | { |
1139 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s", |
1140 | 0 | osLastErrorMsg.c_str()); |
1141 | 0 | } |
1142 | 0 | } |
1143 | |
|
1144 | 0 | if (pRegister != nullptr) |
1145 | 0 | { |
1146 | 0 | bFoundOnePlugin = true; |
1147 | 0 | CPLDebug("GDAL", "Auto register %s using %s.", |
1148 | 0 | osFilename.c_str(), osFuncName.c_str()); |
1149 | |
|
1150 | 0 | reinterpret_cast<void (*)()>(pRegister)(); |
1151 | 0 | } |
1152 | 0 | } |
1153 | |
|
1154 | 0 | CSLDestroy(papszFiles); |
1155 | 0 | } |
1156 | |
|
1157 | 0 | CSLDestroy(papszSearchPaths); |
1158 | | |
1159 | | // No need to reorder drivers if there are no plugins |
1160 | 0 | if (!bFoundOnePlugin) |
1161 | 0 | m_osDriversIniPath.clear(); |
1162 | |
|
1163 | 0 | #endif // GDAL_NO_AUTOLOAD |
1164 | 0 | } |
1165 | | |
1166 | | /************************************************************************/ |
1167 | | /* ReorderDrivers() */ |
1168 | | /************************************************************************/ |
1169 | | |
1170 | | /** |
1171 | | * \brief Reorder drivers according to the order of the drivers.ini file. |
1172 | | * |
1173 | | * This function is called by GDALAllRegister(), at the end of driver loading, |
1174 | | * in particular after plugin loading. |
1175 | | * It will load the drivers.ini configuration file located next to plugins and |
1176 | | * will use it to reorder the registration order of drivers. This can be |
1177 | | * important in some situations where multiple drivers could open the same |
1178 | | * dataset. |
1179 | | */ |
1180 | | |
1181 | | void GDALDriverManager::ReorderDrivers() |
1182 | 0 | { |
1183 | 0 | #ifndef GDAL_NO_AUTOLOAD |
1184 | 0 | if (m_osDriversIniPath.empty()) |
1185 | 0 | { |
1186 | 0 | if (m_oSetPluginFileNames.empty()) |
1187 | 0 | return; |
1188 | | |
1189 | 0 | m_osDriversIniPath = GetPluginFullPath("drivers.ini"); |
1190 | 0 | if (m_osDriversIniPath.empty()) |
1191 | 0 | return; |
1192 | 0 | } |
1193 | | |
1194 | 0 | CPLMutexHolderD(&hDMMutex); |
1195 | |
|
1196 | 0 | VSILFILE *fp = VSIFOpenL(m_osDriversIniPath.c_str(), "rb"); |
1197 | 0 | if (fp == nullptr) |
1198 | 0 | return; |
1199 | | |
1200 | | // Parse drivers.ini |
1201 | 0 | bool bInOrderSection = false; |
1202 | 0 | std::vector<std::string> aosOrderedDrivers; |
1203 | 0 | std::set<std::string> oSetOrderedDrivers; |
1204 | 0 | while (const char *pszLine = CPLReadLine2L(fp, 1024, nullptr)) |
1205 | 0 | { |
1206 | 0 | if (pszLine[0] == '#') |
1207 | 0 | continue; |
1208 | 0 | int i = 0; |
1209 | 0 | while (pszLine[i] != 0 && |
1210 | 0 | isspace(static_cast<unsigned char>(pszLine[i]))) |
1211 | 0 | i++; |
1212 | 0 | if (pszLine[i] == 0) |
1213 | 0 | continue; |
1214 | 0 | if (strcmp(pszLine, "[order]") == 0) |
1215 | 0 | { |
1216 | 0 | bInOrderSection = true; |
1217 | 0 | } |
1218 | 0 | else if (pszLine[0] == '[') |
1219 | 0 | { |
1220 | 0 | bInOrderSection = false; |
1221 | 0 | } |
1222 | 0 | else if (bInOrderSection) |
1223 | 0 | { |
1224 | 0 | CPLString osUCDriverName(pszLine); |
1225 | 0 | osUCDriverName.toupper(); |
1226 | 0 | if (osUCDriverName != "MEMORY") |
1227 | 0 | { |
1228 | 0 | if (cpl::contains(oSetOrderedDrivers, osUCDriverName)) |
1229 | 0 | { |
1230 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1231 | 0 | "Duplicated name %s in [order] section", pszLine); |
1232 | 0 | } |
1233 | 0 | else if (cpl::contains(oMapNameToDrivers, osUCDriverName)) |
1234 | 0 | { |
1235 | 0 | aosOrderedDrivers.emplace_back(pszLine); |
1236 | 0 | oSetOrderedDrivers.insert(std::move(osUCDriverName)); |
1237 | 0 | } |
1238 | | #ifdef DEBUG_VERBOSE |
1239 | | else |
1240 | | { |
1241 | | // Completely expected situation for "non-maximal" builds, |
1242 | | // but can help diagnose bad entries in drivers.ini |
1243 | | CPLDebug("GDAL", |
1244 | | "Driver %s is listed in %s but not registered.", |
1245 | | pszLine, m_osDriversIniPath.c_str()); |
1246 | | } |
1247 | | #endif |
1248 | 0 | } |
1249 | 0 | } |
1250 | 0 | } |
1251 | 0 | VSIFCloseL(fp); |
1252 | | |
1253 | | // Find potential registered drivers not in drivers.ini, and put them in |
1254 | | // their registration order in aosUnorderedDrivers |
1255 | 0 | std::vector<std::string> aosUnorderedDrivers; |
1256 | 0 | for (int i = 0; i < nDrivers; ++i) |
1257 | 0 | { |
1258 | 0 | const char *pszName = papoDrivers[i]->GetDescription(); |
1259 | 0 | if (!cpl::contains(oSetOrderedDrivers, CPLString(pszName).toupper())) |
1260 | 0 | { |
1261 | | // Could happen for a private plugin |
1262 | 0 | CPLDebug("GDAL", |
1263 | 0 | "Driver %s is registered but not listed in %s. " |
1264 | 0 | "It will be registered before other drivers.", |
1265 | 0 | pszName, m_osDriversIniPath.c_str()); |
1266 | 0 | aosUnorderedDrivers.emplace_back(pszName); |
1267 | 0 | } |
1268 | 0 | } |
1269 | | |
1270 | | // Put aosUnorderedDrivers in front of existing aosOrderedDrivers |
1271 | 0 | if (!aosUnorderedDrivers.empty()) |
1272 | 0 | { |
1273 | 0 | aosUnorderedDrivers.insert(aosUnorderedDrivers.end(), |
1274 | 0 | aosOrderedDrivers.begin(), |
1275 | 0 | aosOrderedDrivers.end()); |
1276 | 0 | std::swap(aosOrderedDrivers, aosUnorderedDrivers); |
1277 | 0 | } |
1278 | | |
1279 | | // Update papoDrivers[] to reflect aosOrderedDrivers order. |
1280 | 0 | CPLAssert(static_cast<int>(aosOrderedDrivers.size()) == nDrivers); |
1281 | 0 | for (int i = 0; i < nDrivers; ++i) |
1282 | 0 | { |
1283 | 0 | const auto oIter = |
1284 | 0 | oMapNameToDrivers.find(CPLString(aosOrderedDrivers[i]).toupper()); |
1285 | 0 | CPLAssert(oIter != oMapNameToDrivers.end()); |
1286 | 0 | papoDrivers[i] = oIter->second; |
1287 | 0 | } |
1288 | 0 | #endif |
1289 | 0 | } |
1290 | | |
1291 | | /************************************************************************/ |
1292 | | /* GDALPluginDriverProxy */ |
1293 | | /************************************************************************/ |
1294 | | |
1295 | | /** Constructor for a plugin driver proxy. |
1296 | | * |
1297 | | * @param osPluginFileName Plugin filename. e.g "ogr_Parquet.so" |
1298 | | */ |
1299 | | GDALPluginDriverProxy::GDALPluginDriverProxy( |
1300 | | const std::string &osPluginFileName) |
1301 | 0 | : m_osPluginFileName(osPluginFileName) |
1302 | 0 | { |
1303 | 0 | } |
1304 | | |
1305 | | //! @cond Doxygen_Suppress |
1306 | | #define DEFINE_DRIVER_METHOD_GET_CALLBACK(method_name, output_type) \ |
1307 | | GDALDriver::output_type GDALPluginDriverProxy::method_name() \ |
1308 | 0 | { \ |
1309 | 0 | auto poRealDriver = GetRealDriver(); \ |
1310 | 0 | if (!poRealDriver) \ |
1311 | 0 | return nullptr; \ |
1312 | 0 | return poRealDriver->method_name(); \ |
1313 | 0 | } Unexecuted instantiation: GDALPluginDriverProxy::GetOpenCallback() Unexecuted instantiation: GDALPluginDriverProxy::GetCreateCallback() Unexecuted instantiation: GDALPluginDriverProxy::GetCreateMultiDimensionalCallback() Unexecuted instantiation: GDALPluginDriverProxy::GetCreateCopyCallback() Unexecuted instantiation: GDALPluginDriverProxy::GetDeleteCallback() Unexecuted instantiation: GDALPluginDriverProxy::GetRenameCallback() Unexecuted instantiation: GDALPluginDriverProxy::GetCopyFilesCallback() Unexecuted instantiation: GDALPluginDriverProxy::GetInstantiateAlgorithmCallback() |
1314 | | |
1315 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetOpenCallback, OpenCallback) |
1316 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCreateCallback, CreateCallback) |
1317 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCreateMultiDimensionalCallback, |
1318 | | CreateMultiDimensionalCallback) |
1319 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCreateCopyCallback, CreateCopyCallback) |
1320 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetDeleteCallback, DeleteCallback) |
1321 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetRenameCallback, RenameCallback) |
1322 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCopyFilesCallback, CopyFilesCallback) |
1323 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetInstantiateAlgorithmCallback, |
1324 | | InstantiateAlgorithmCallback) |
1325 | | |
1326 | | //! @endcond |
1327 | | |
1328 | | char **GDALPluginDriverProxy::GetMetadata(const char *pszDomain) |
1329 | 0 | { |
1330 | 0 | auto poRealDriver = GetRealDriver(); |
1331 | 0 | if (!poRealDriver) |
1332 | 0 | return nullptr; |
1333 | 0 | return poRealDriver->GetMetadata(pszDomain); |
1334 | 0 | } |
1335 | | |
1336 | | CPLErr GDALPluginDriverProxy::SetMetadataItem(const char *pszName, |
1337 | | const char *pszValue, |
1338 | | const char *pszDomain) |
1339 | 0 | { |
1340 | 0 | if (!pszDomain || pszDomain[0] == 0) |
1341 | 0 | { |
1342 | 0 | if (!EQUAL(pszName, GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE)) |
1343 | 0 | { |
1344 | 0 | m_oSetMetadataItems.insert(pszName); |
1345 | 0 | } |
1346 | 0 | } |
1347 | 0 | return GDALDriver::SetMetadataItem(pszName, pszValue, pszDomain); |
1348 | 0 | } |
1349 | | |
1350 | | static const char *const apszProxyMetadataItems[] = { |
1351 | | GDAL_DMD_LONGNAME, |
1352 | | GDAL_DMD_EXTENSIONS, |
1353 | | GDAL_DMD_EXTENSION, |
1354 | | GDAL_DCAP_RASTER, |
1355 | | GDAL_DCAP_MULTIDIM_RASTER, |
1356 | | GDAL_DCAP_VECTOR, |
1357 | | GDAL_DCAP_GNM, |
1358 | | GDAL_DMD_OPENOPTIONLIST, |
1359 | | GDAL_DCAP_OPEN, |
1360 | | GDAL_DCAP_CREATE, |
1361 | | GDAL_DCAP_CREATE_MULTIDIMENSIONAL, |
1362 | | GDAL_DCAP_CREATECOPY, |
1363 | | GDAL_DMD_SUBDATASETS, |
1364 | | GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, |
1365 | | GDAL_DCAP_NONSPATIAL, |
1366 | | GDAL_DMD_CONNECTION_PREFIX, |
1367 | | GDAL_DCAP_VECTOR_TRANSLATE_FROM, |
1368 | | GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE, |
1369 | | }; |
1370 | | |
1371 | | const char *GDALPluginDriverProxy::GetMetadataItem(const char *pszName, |
1372 | | const char *pszDomain) |
1373 | 0 | { |
1374 | 0 | const auto IsListedProxyMetadataItem = [](const char *pszItem) |
1375 | 0 | { |
1376 | 0 | for (const char *pszListedItem : apszProxyMetadataItems) |
1377 | 0 | { |
1378 | 0 | if (EQUAL(pszItem, pszListedItem)) |
1379 | 0 | return true; |
1380 | 0 | } |
1381 | 0 | return false; |
1382 | 0 | }; |
1383 | |
|
1384 | 0 | if (!pszDomain || pszDomain[0] == 0) |
1385 | 0 | { |
1386 | 0 | if (EQUAL(pszName, "IS_NON_LOADED_PLUGIN")) |
1387 | 0 | { |
1388 | 0 | return !m_poRealDriver ? "YES" : nullptr; |
1389 | 0 | } |
1390 | 0 | else if (EQUAL(pszName, "MISSING_PLUGIN_FILENAME")) |
1391 | 0 | { |
1392 | 0 | return m_osPluginFullPath.empty() ? m_osPluginFileName.c_str() |
1393 | 0 | : nullptr; |
1394 | 0 | } |
1395 | 0 | else if (IsListedProxyMetadataItem(pszName)) |
1396 | 0 | { |
1397 | 0 | const char *pszValue = |
1398 | 0 | GDALDriver::GetMetadataItem(pszName, pszDomain); |
1399 | 0 | if (!pszValue && EQUAL(pszName, GDAL_DMD_EXTENSION)) |
1400 | 0 | { |
1401 | 0 | const char *pszOtherValue = |
1402 | 0 | GDALDriver::GetMetadataItem(GDAL_DMD_EXTENSIONS, pszDomain); |
1403 | 0 | if (pszOtherValue && strchr(pszOtherValue, ' ')) |
1404 | 0 | return pszOtherValue; |
1405 | 0 | } |
1406 | 0 | else if (!pszValue && EQUAL(pszName, GDAL_DMD_EXTENSIONS)) |
1407 | 0 | { |
1408 | 0 | return GDALDriver::GetMetadataItem(GDAL_DMD_EXTENSION, |
1409 | 0 | pszDomain); |
1410 | 0 | } |
1411 | 0 | return pszValue; |
1412 | 0 | } |
1413 | 0 | else if (cpl::contains(m_oSetMetadataItems, pszName)) |
1414 | 0 | { |
1415 | 0 | return GDALDriver::GetMetadataItem(pszName, pszDomain); |
1416 | 0 | } |
1417 | 0 | } |
1418 | | |
1419 | 0 | auto poRealDriver = GetRealDriver(); |
1420 | 0 | if (!poRealDriver) |
1421 | 0 | return nullptr; |
1422 | 0 | return poRealDriver->GetMetadataItem(pszName, pszDomain); |
1423 | 0 | } |
1424 | | |
1425 | | /************************************************************************/ |
1426 | | /* GetRealDriver() */ |
1427 | | /************************************************************************/ |
1428 | | |
1429 | | GDALDriver *GDALPluginDriverProxy::GetRealDriver() |
1430 | 0 | { |
1431 | | // No need to take the mutex has this member variable is not modified |
1432 | | // under the mutex. |
1433 | 0 | if (m_osPluginFullPath.empty()) |
1434 | 0 | return nullptr; |
1435 | | |
1436 | 0 | CPLMutexHolderD(&hDMMutex); |
1437 | |
|
1438 | 0 | if (m_poRealDriver) |
1439 | 0 | return m_poRealDriver.get(); |
1440 | | |
1441 | 0 | auto poDriverManager = GetGDALDriverManager(); |
1442 | 0 | auto oIter = poDriverManager->m_oMapRealDrivers.find(GetDescription()); |
1443 | 0 | if (oIter != poDriverManager->m_oMapRealDrivers.end()) |
1444 | 0 | { |
1445 | 0 | m_poRealDriver = std::move(oIter->second); |
1446 | 0 | poDriverManager->m_oMapRealDrivers.erase(oIter); |
1447 | 0 | } |
1448 | 0 | else |
1449 | 0 | { |
1450 | | #ifdef GDAL_NO_AUTOLOAD |
1451 | | return nullptr; |
1452 | | #else |
1453 | 0 | CPLString osFuncName; |
1454 | 0 | if (STARTS_WITH(m_osPluginFileName.c_str(), "gdal_")) |
1455 | 0 | { |
1456 | 0 | osFuncName = "GDALRegister_"; |
1457 | 0 | osFuncName += m_osPluginFileName.substr( |
1458 | 0 | strlen("gdal_"), |
1459 | 0 | m_osPluginFileName.find('.') - strlen("gdal_")); |
1460 | 0 | } |
1461 | 0 | else |
1462 | 0 | { |
1463 | 0 | CPLAssert(STARTS_WITH(m_osPluginFileName.c_str(), "ogr_")); |
1464 | 0 | osFuncName = "RegisterOGR"; |
1465 | 0 | osFuncName += m_osPluginFileName.substr( |
1466 | 0 | strlen("ogr_"), m_osPluginFileName.find('.') - strlen("ogr_")); |
1467 | 0 | } |
1468 | | |
1469 | 0 | CPLErrorReset(); |
1470 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1471 | 0 | void *pRegister = CPLGetSymbol(m_osPluginFullPath.c_str(), osFuncName); |
1472 | 0 | CPLPopErrorHandler(); |
1473 | 0 | if (pRegister == nullptr) |
1474 | 0 | { |
1475 | 0 | CPLString osLastErrorMsg(CPLGetLastErrorMsg()); |
1476 | 0 | osFuncName = "GDALRegisterMe"; |
1477 | 0 | pRegister = CPLGetSymbol(m_osPluginFullPath.c_str(), osFuncName); |
1478 | 0 | if (pRegister == nullptr) |
1479 | 0 | { |
1480 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s", |
1481 | 0 | osLastErrorMsg.c_str()); |
1482 | 0 | } |
1483 | 0 | } |
1484 | |
|
1485 | 0 | if (pRegister != nullptr) |
1486 | 0 | { |
1487 | 0 | CPLDebug("GDAL", "On-demand registering %s using %s.", |
1488 | 0 | m_osPluginFullPath.c_str(), osFuncName.c_str()); |
1489 | |
|
1490 | 0 | poDriverManager->m_bInDeferredDriverLoading = true; |
1491 | 0 | try |
1492 | 0 | { |
1493 | 0 | reinterpret_cast<void (*)()>(pRegister)(); |
1494 | 0 | } |
1495 | 0 | catch (...) |
1496 | 0 | { |
1497 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s threw an exception", |
1498 | 0 | osFuncName.c_str()); |
1499 | 0 | } |
1500 | 0 | poDriverManager->m_bInDeferredDriverLoading = false; |
1501 | |
|
1502 | 0 | oIter = poDriverManager->m_oMapRealDrivers.find(GetDescription()); |
1503 | 0 | if (oIter == poDriverManager->m_oMapRealDrivers.end()) |
1504 | 0 | { |
1505 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1506 | 0 | "Function %s of %s did not register a driver %s", |
1507 | 0 | osFuncName.c_str(), m_osPluginFullPath.c_str(), |
1508 | 0 | GetDescription()); |
1509 | 0 | } |
1510 | 0 | else |
1511 | 0 | { |
1512 | 0 | m_poRealDriver = std::move(oIter->second); |
1513 | 0 | poDriverManager->m_oMapRealDrivers.erase(oIter); |
1514 | 0 | } |
1515 | 0 | } |
1516 | 0 | #endif // GDAL_NO_AUTOLOAD |
1517 | 0 | } |
1518 | | |
1519 | 0 | if (m_poRealDriver) |
1520 | 0 | { |
1521 | 0 | pfnDelete = m_poRealDriver->pfnDelete; |
1522 | 0 | pfnRename = m_poRealDriver->pfnRename; |
1523 | 0 | pfnCopyFiles = m_poRealDriver->pfnCopyFiles; |
1524 | |
|
1525 | 0 | if (strcmp(GetDescription(), m_poRealDriver->GetDescription()) != 0) |
1526 | 0 | { |
1527 | 0 | CPLError( |
1528 | 0 | CE_Warning, CPLE_AppDefined, |
1529 | 0 | "Driver %s has not the same name as its underlying driver (%s)", |
1530 | 0 | GetDescription(), m_poRealDriver->GetDescription()); |
1531 | 0 | } |
1532 | |
|
1533 | 0 | for (const auto &osItem : m_oSetMetadataItems) |
1534 | 0 | { |
1535 | 0 | const char *pszProxyValue = GetMetadataItem(osItem.c_str()); |
1536 | 0 | const char *pszRealValue = |
1537 | 0 | m_poRealDriver->GetMetadataItem(osItem.c_str()); |
1538 | 0 | if (pszProxyValue && |
1539 | 0 | (!pszRealValue || strcmp(pszProxyValue, pszRealValue) != 0)) |
1540 | 0 | { |
1541 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1542 | 0 | "Proxy driver %s declares %s whereas its real driver " |
1543 | 0 | "doesn't declare it or with a different value", |
1544 | 0 | GetDescription(), osItem.c_str()); |
1545 | 0 | } |
1546 | 0 | } |
1547 | 0 | for (const char *pszListedItem : apszProxyMetadataItems) |
1548 | 0 | { |
1549 | 0 | const char *pszRealValue = |
1550 | 0 | m_poRealDriver->GetMetadataItem(pszListedItem); |
1551 | 0 | if (pszRealValue) |
1552 | 0 | { |
1553 | 0 | const char *pszProxyValue = GetMetadataItem(pszListedItem); |
1554 | 0 | if (!pszProxyValue || strcmp(pszProxyValue, pszRealValue) != 0) |
1555 | 0 | { |
1556 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1557 | 0 | "Driver %s declares %s whereas its proxy " |
1558 | 0 | "doesn't declare it or with a different value", |
1559 | 0 | GetDescription(), pszListedItem); |
1560 | 0 | } |
1561 | 0 | } |
1562 | 0 | } |
1563 | |
|
1564 | 0 | const auto CheckFunctionPointer = |
1565 | 0 | [this](void *pfnFuncProxy, void *pfnFuncReal, const char *pszFunc) |
1566 | 0 | { |
1567 | 0 | if (pfnFuncReal && !pfnFuncProxy) |
1568 | 0 | { |
1569 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1570 | 0 | "Driver %s declares a %s callback whereas its proxy " |
1571 | 0 | "does not declare it", |
1572 | 0 | GetDescription(), pszFunc); |
1573 | 0 | } |
1574 | 0 | else if (!pfnFuncReal && pfnFuncProxy) |
1575 | 0 | { |
1576 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1577 | 0 | "Proxy driver %s declares a %s callback whereas the " |
1578 | 0 | "real driver does not.", |
1579 | 0 | GetDescription(), pszFunc); |
1580 | 0 | } |
1581 | 0 | }; |
1582 | |
|
1583 | 0 | CheckFunctionPointer( |
1584 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnIdentify), |
1585 | 0 | reinterpret_cast<void *>(pfnIdentify), "pfnIdentify"); |
1586 | | |
1587 | | // The real driver might provide a more accurate identification method |
1588 | 0 | if (m_poRealDriver->pfnIdentify) |
1589 | 0 | pfnIdentify = m_poRealDriver->pfnIdentify; |
1590 | |
|
1591 | 0 | CheckFunctionPointer( |
1592 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnGetSubdatasetInfoFunc), |
1593 | 0 | reinterpret_cast<void *>(pfnGetSubdatasetInfoFunc), |
1594 | 0 | "pfnGetSubdatasetInfoFunc"); |
1595 | |
|
1596 | 0 | const auto CheckFunctionPointerVersusCap = |
1597 | 0 | [this](void *pfnFunc, const char *pszFunc, const char *pszItemName) |
1598 | 0 | { |
1599 | 0 | if (pfnFunc && !GetMetadataItem(pszItemName)) |
1600 | 0 | { |
1601 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1602 | 0 | "Driver %s declares a %s callback whereas its proxy " |
1603 | 0 | "doest not declare %s", |
1604 | 0 | GetDescription(), pszFunc, pszItemName); |
1605 | 0 | } |
1606 | 0 | else if (!pfnFunc && GetMetadataItem(pszItemName)) |
1607 | 0 | { |
1608 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1609 | 0 | "Proxy driver %s declares %s whereas the real " |
1610 | 0 | "driver does not declare a %s callback", |
1611 | 0 | GetDescription(), pszItemName, pszFunc); |
1612 | 0 | } |
1613 | 0 | }; |
1614 | |
|
1615 | 0 | CheckFunctionPointerVersusCap( |
1616 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnOpen), "pfnOpen", |
1617 | 0 | GDAL_DCAP_OPEN); |
1618 | 0 | CheckFunctionPointerVersusCap( |
1619 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnCreate), "pfnCreate", |
1620 | 0 | GDAL_DCAP_CREATE); |
1621 | 0 | CheckFunctionPointerVersusCap( |
1622 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnCreateCopy), |
1623 | 0 | "pfnCreateCopy", GDAL_DCAP_CREATECOPY); |
1624 | 0 | CheckFunctionPointerVersusCap( |
1625 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnCreateMultiDimensional), |
1626 | 0 | "pfnCreateMultiDimensional", GDAL_DCAP_CREATE_MULTIDIMENSIONAL); |
1627 | 0 | } |
1628 | |
|
1629 | 0 | return m_poRealDriver.get(); |
1630 | 0 | } |
1631 | | |
1632 | | /************************************************************************/ |
1633 | | /* GetPluginFullPath() */ |
1634 | | /************************************************************************/ |
1635 | | |
1636 | | std::string GDALDriverManager::GetPluginFullPath(const char *pszFilename) const |
1637 | 0 | { |
1638 | 0 | if (!m_osLastTriedDirectory.empty()) |
1639 | 0 | { |
1640 | 0 | std::string osFullFilename = CPLFormFilenameSafe( |
1641 | 0 | m_osLastTriedDirectory.c_str(), pszFilename, nullptr); |
1642 | 0 | VSIStatBufL sStatBuf; |
1643 | 0 | if (VSIStatL(osFullFilename.c_str(), &sStatBuf) == 0) |
1644 | 0 | { |
1645 | 0 | return osFullFilename; |
1646 | 0 | } |
1647 | 0 | } |
1648 | | |
1649 | 0 | const char *pszGDAL_DRIVER_PATH = |
1650 | 0 | CPLGetConfigOption("GDAL_DRIVER_PATH", nullptr); |
1651 | 0 | if (pszGDAL_DRIVER_PATH == nullptr) |
1652 | 0 | pszGDAL_DRIVER_PATH = CPLGetConfigOption("OGR_DRIVER_PATH", nullptr); |
1653 | | |
1654 | | /* ---------------------------------------------------------------- */ |
1655 | | /* Allow applications to completely disable this search by */ |
1656 | | /* setting the driver path to the special string "disable". */ |
1657 | | /* ---------------------------------------------------------------- */ |
1658 | 0 | if (pszGDAL_DRIVER_PATH != nullptr && EQUAL(pszGDAL_DRIVER_PATH, "disable")) |
1659 | 0 | { |
1660 | 0 | CPLDebug("GDAL", "GDALDriverManager::GetPluginFullPath() disabled."); |
1661 | 0 | return std::string(); |
1662 | 0 | } |
1663 | | |
1664 | | /* ---------------------------------------------------------------- */ |
1665 | | /* Where should we look for stuff? */ |
1666 | | /* ---------------------------------------------------------------- */ |
1667 | 0 | const CPLStringList aosSearchPaths( |
1668 | 0 | GDALDriverManager::GetSearchPaths(pszGDAL_DRIVER_PATH)); |
1669 | | |
1670 | | /* ---------------------------------------------------------------- */ |
1671 | | /* Format the ABI version specific subdirectory to look in. */ |
1672 | | /* ---------------------------------------------------------------- */ |
1673 | 0 | CPLString osABIVersion; |
1674 | |
|
1675 | 0 | osABIVersion.Printf("%d.%d", GDAL_VERSION_MAJOR, GDAL_VERSION_MINOR); |
1676 | | |
1677 | | /* ---------------------------------------------------------------- */ |
1678 | | /* Scan each directory looking for the file of interest. */ |
1679 | | /* ---------------------------------------------------------------- */ |
1680 | 0 | const int nSearchPaths = aosSearchPaths.size(); |
1681 | 0 | for (int iDir = 0; iDir < nSearchPaths; ++iDir) |
1682 | 0 | { |
1683 | 0 | std::string osABISpecificDir = |
1684 | 0 | CPLFormFilenameSafe(aosSearchPaths[iDir], osABIVersion, nullptr); |
1685 | |
|
1686 | 0 | VSIStatBufL sStatBuf; |
1687 | 0 | if (VSIStatL(osABISpecificDir.c_str(), &sStatBuf) != 0) |
1688 | 0 | osABISpecificDir = aosSearchPaths[iDir]; |
1689 | |
|
1690 | 0 | std::string osFullFilename = |
1691 | 0 | CPLFormFilenameSafe(osABISpecificDir.c_str(), pszFilename, nullptr); |
1692 | 0 | if (VSIStatL(osFullFilename.c_str(), &sStatBuf) == 0) |
1693 | 0 | { |
1694 | 0 | m_osLastTriedDirectory = std::move(osABISpecificDir); |
1695 | 0 | return osFullFilename; |
1696 | 0 | } |
1697 | 0 | } |
1698 | | |
1699 | 0 | return std::string(); |
1700 | 0 | } |
1701 | | |
1702 | | /************************************************************************/ |
1703 | | /* DeclareDeferredPluginDriver() */ |
1704 | | /************************************************************************/ |
1705 | | |
1706 | | /** Declare a driver that will be loaded as a plugin, when actually needed. |
1707 | | * |
1708 | | * @param poProxyDriver Plugin driver proxy |
1709 | | * |
1710 | | * @since 3.9 |
1711 | | */ |
1712 | | void GDALDriverManager::DeclareDeferredPluginDriver( |
1713 | | GDALPluginDriverProxy *poProxyDriver) |
1714 | 0 | { |
1715 | 0 | CPLMutexHolderD(&hDMMutex); |
1716 | |
|
1717 | 0 | const auto &osPluginFileName = poProxyDriver->GetPluginFileName(); |
1718 | 0 | const char *pszPluginFileName = osPluginFileName.c_str(); |
1719 | 0 | if ((!STARTS_WITH(pszPluginFileName, "gdal_") && |
1720 | 0 | !STARTS_WITH(pszPluginFileName, "ogr_")) || |
1721 | 0 | !strchr(pszPluginFileName, '.')) |
1722 | 0 | { |
1723 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid plugin filename: %s", |
1724 | 0 | pszPluginFileName); |
1725 | 0 | return; |
1726 | 0 | } |
1727 | | |
1728 | 0 | if (GDALGetDriverByName(poProxyDriver->GetDescription())) |
1729 | 0 | { |
1730 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1731 | 0 | "DeclarePluginDriver(): trying to register %s several times", |
1732 | 0 | poProxyDriver->GetDescription()); |
1733 | 0 | delete poProxyDriver; |
1734 | 0 | return; |
1735 | 0 | } |
1736 | | |
1737 | 0 | const std::string osFullPath = GetPluginFullPath(pszPluginFileName); |
1738 | 0 | poProxyDriver->SetPluginFullPath(osFullPath); |
1739 | |
|
1740 | 0 | if (osFullPath.empty()) |
1741 | 0 | { |
1742 | | // Do not try to re-register a non-existent deferred plugin |
1743 | | // This would cause memory leaks in case of repeated calls to GDALAllRegister() |
1744 | | // Cf https://github.com/rasterio/rasterio/issues/3250 |
1745 | 0 | for (const auto &poDriver : m_aoHiddenDrivers) |
1746 | 0 | { |
1747 | 0 | if (EQUAL(poDriver->GetDescription(), |
1748 | 0 | poProxyDriver->GetDescription())) |
1749 | 0 | { |
1750 | 0 | delete poProxyDriver; |
1751 | 0 | return; |
1752 | 0 | } |
1753 | 0 | } |
1754 | | |
1755 | 0 | CPLDebug("GDAL", |
1756 | 0 | "Proxy driver %s *not* registered due to %s not being found", |
1757 | 0 | poProxyDriver->GetDescription(), pszPluginFileName); |
1758 | 0 | RegisterDriver(poProxyDriver, /*bHidden=*/true); |
1759 | 0 | } |
1760 | 0 | else |
1761 | 0 | { |
1762 | | //CPLDebugOnly("GDAL", "Registering proxy driver %s", |
1763 | | // poProxyDriver->GetDescription()); |
1764 | 0 | RegisterDriver(poProxyDriver); |
1765 | 0 | m_oSetPluginFileNames.insert(pszPluginFileName); |
1766 | 0 | } |
1767 | 0 | } |
1768 | | |
1769 | | /************************************************************************/ |
1770 | | /* GDALDestroyDriverManager() */ |
1771 | | /************************************************************************/ |
1772 | | |
1773 | | /** |
1774 | | * \brief Destroy the driver manager. |
1775 | | * |
1776 | | * Incidentally unloads all managed drivers. |
1777 | | * |
1778 | | * NOTE: This function is not thread safe. It should not be called while |
1779 | | * other threads are actively using GDAL. |
1780 | | * |
1781 | | * \see GDALDestroy() |
1782 | | * \deprecated Use GDALDestroy() instead |
1783 | | */ |
1784 | | |
1785 | | void CPL_STDCALL GDALDestroyDriverManager(void) |
1786 | | |
1787 | 0 | { |
1788 | | // THREADSAFETY: We would like to lock the mutex here, but it |
1789 | | // needs to be reacquired within the destructor during driver |
1790 | | // deregistration. |
1791 | | |
1792 | | // FIXME: Disable following code as it crashed on OSX CI test. |
1793 | | // std::lock_guard<std::mutex> oLock(oDeleteMutex); |
1794 | |
|
1795 | 0 | if (poDM != nullptr) |
1796 | 0 | { |
1797 | 0 | delete poDM; |
1798 | 0 | poDM = nullptr; |
1799 | 0 | } |
1800 | 0 | } |