/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 | 2.28M | { |
90 | 2.28M | if (poDM == nullptr) |
91 | 82 | { |
92 | 82 | CPLMutexHolderD(&hDMMutex); |
93 | | // cppcheck-suppress identicalInnerCondition |
94 | 82 | if (poDM == nullptr) |
95 | 82 | poDM = new GDALDriverManager(); |
96 | 82 | } |
97 | | |
98 | 2.28M | CPLAssert(nullptr != poDM); |
99 | | |
100 | 2.28M | return const_cast<GDALDriverManager *>(poDM); |
101 | 2.28M | } |
102 | | |
103 | | /************************************************************************/ |
104 | | /* GDALDriverManager() */ |
105 | | /************************************************************************/ |
106 | | |
107 | 82 | #define XSTRINGIFY(x) #x |
108 | 82 | #define STRINGIFY(x) XSTRINGIFY(x) |
109 | | |
110 | | GDALDriverManager::GDALDriverManager() |
111 | 82 | { |
112 | 82 | CPLAssert(poDM == nullptr); |
113 | | |
114 | 82 | CPLLoadConfigOptionsFromPredefinedFiles(); |
115 | | |
116 | 82 | CPLHTTPSetDefaultUserAgent( |
117 | 82 | "GDAL/" STRINGIFY(GDAL_VERSION_MAJOR) "." STRINGIFY( |
118 | 82 | 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 | 82 | #ifdef INST_DATA |
129 | 82 | if (CPLGetConfigOption("GDAL_DATA", nullptr) != nullptr) |
130 | 81 | { |
131 | | // This one is picked up automatically by finder initialization. |
132 | 81 | } |
133 | 1 | else |
134 | 1 | { |
135 | 1 | CPLPushFinderLocation(INST_DATA); |
136 | 1 | } |
137 | 82 | #endif |
138 | 82 | } |
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 | 0 | #ifdef HAVE_XERCES |
257 | 0 | OGRCleanupXercesMutex(); |
258 | 0 | #endif |
259 | |
|
260 | | #ifdef OGRAPISPY_ENABLED |
261 | | OGRAPISpyDestroyMutex(); |
262 | | #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 | 80.4k | { |
347 | 80.4k | return nDrivers; |
348 | 80.4k | } |
349 | | |
350 | | //! @cond Doxygen_Suppress |
351 | | int GDALDriverManager::GetDriverCount(bool bIncludeHidden) const |
352 | | |
353 | 1.79M | { |
354 | 1.79M | if (!bIncludeHidden) |
355 | 0 | return nDrivers; |
356 | 1.79M | return nDrivers + static_cast<int>(m_aoHiddenDrivers.size()); |
357 | 1.79M | } |
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 | 4.88M | { |
432 | 4.88M | CPLMutexHolderD(&hDMMutex); |
433 | | |
434 | 4.88M | return GetDriver_unlocked(iDriver); |
435 | 4.88M | } |
436 | | |
437 | | //! @cond Doxygen_Suppress |
438 | | GDALDriver *GDALDriverManager::GetDriver(int iDriver, bool bIncludeHidden) |
439 | | |
440 | 146M | { |
441 | 146M | CPLMutexHolderD(&hDMMutex); |
442 | 146M | if (!bIncludeHidden || iDriver < nDrivers) |
443 | 146M | 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, or -1 in case of error |
488 | | */ |
489 | | |
490 | | int GDALDriverManager::RegisterDriver(GDALDriver *poDriver) |
491 | 4.31k | { |
492 | 4.31k | return RegisterDriver(poDriver, /*bHidden=*/false); |
493 | 4.31k | } |
494 | | |
495 | | int GDALDriverManager::RegisterDriver(GDALDriver *poDriver, bool bHidden) |
496 | 4.31k | { |
497 | 4.31k | CPLMutexHolderD(&hDMMutex); |
498 | | |
499 | 4.31k | const char *pszDriverName = poDriver->GetDescription(); |
500 | 4.31k | if (pszDriverName[0] == 0) |
501 | 0 | { |
502 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
503 | 0 | "Cannot register driver with empty name"); |
504 | 0 | return -1; |
505 | 0 | } |
506 | 4.31k | if (pszDriverName[0] == '-') |
507 | 0 | { |
508 | | // Because GDALDataset::Open() considers that strings in the allowed |
509 | | // drivers list starting with dash mean to exclude a driver. |
510 | 0 | CPLError( |
511 | 0 | CE_Failure, CPLE_AppDefined, |
512 | 0 | "Cannot register driver whose name starts with a dash character"); |
513 | 0 | return -1; |
514 | 0 | } |
515 | | |
516 | | /* -------------------------------------------------------------------- */ |
517 | | /* If it is already registered, just return the existing */ |
518 | | /* index. */ |
519 | | /* -------------------------------------------------------------------- */ |
520 | 4.31k | if (!m_bInDeferredDriverLoading && |
521 | 4.31k | GetDriverByName_unlocked(pszDriverName) != nullptr) |
522 | 0 | { |
523 | 0 | for (int i = 0; i < nDrivers; ++i) |
524 | 0 | { |
525 | 0 | if (papoDrivers[i] == poDriver) |
526 | 0 | { |
527 | 0 | return i; |
528 | 0 | } |
529 | 0 | } |
530 | | |
531 | 0 | CPLAssert(false); |
532 | 0 | } |
533 | | |
534 | 4.31k | if (poDriver->pfnOpen != nullptr || |
535 | 44 | poDriver->pfnOpenWithDriverArg != nullptr) |
536 | 4.26k | poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES"); |
537 | | |
538 | 4.31k | if (poDriver->pfnCreate != nullptr || poDriver->pfnCreateEx != nullptr) |
539 | 1.58k | poDriver->SetMetadataItem(GDAL_DCAP_CREATE, "YES"); |
540 | | |
541 | 4.31k | if (poDriver->pfnCreateCopy != nullptr) |
542 | 1.01k | poDriver->SetMetadataItem(GDAL_DCAP_CREATECOPY, "YES"); |
543 | | |
544 | 4.31k | if (poDriver->pfnCreateMultiDimensional != nullptr) |
545 | 90 | poDriver->SetMetadataItem(GDAL_DCAP_CREATE_MULTIDIMENSIONAL, "YES"); |
546 | | |
547 | | // Backward compatibility for GDAL raster out-of-tree drivers: |
548 | | // If a driver hasn't explicitly set a vector capability, assume it is |
549 | | // a raster-only driver (legacy OGR drivers will have DCAP_VECTOR set before |
550 | | // calling RegisterDriver()). |
551 | 4.31k | if (poDriver->GetMetadataItem(GDAL_DCAP_RASTER) == nullptr && |
552 | 1.31k | poDriver->GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr && |
553 | 66 | poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER) == nullptr && |
554 | 44 | poDriver->GetMetadataItem(GDAL_DCAP_GNM) == nullptr) |
555 | 0 | { |
556 | 0 | CPLDebug("GDAL", "Assuming DCAP_RASTER for driver %s. Please fix it.", |
557 | 0 | pszDriverName); |
558 | 0 | poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); |
559 | 0 | } |
560 | | |
561 | 4.31k | if (poDriver->GetMetadataItem(GDAL_DMD_OPENOPTIONLIST) != nullptr && |
562 | 1.59k | poDriver->pfnIdentify == nullptr && |
563 | 44 | poDriver->pfnIdentifyEx == nullptr && |
564 | 44 | !STARTS_WITH_CI(pszDriverName, "Interlis")) |
565 | 0 | { |
566 | 0 | CPLDebug("GDAL", |
567 | 0 | "Driver %s that defines GDAL_DMD_OPENOPTIONLIST must also " |
568 | 0 | "implement Identify(), so that it can be used", |
569 | 0 | pszDriverName); |
570 | 0 | } |
571 | | |
572 | 4.31k | if (poDriver->pfnVectorTranslateFrom != nullptr) |
573 | 46 | poDriver->SetMetadataItem(GDAL_DCAP_VECTOR_TRANSLATE_FROM, "YES"); |
574 | | |
575 | 4.31k | if (m_bInDeferredDriverLoading && |
576 | 0 | cpl::contains(oMapNameToDrivers, CPLString(pszDriverName).toupper())) |
577 | 0 | { |
578 | 0 | if (cpl::contains(m_oMapRealDrivers, pszDriverName)) |
579 | 0 | { |
580 | 0 | CPLError( |
581 | 0 | CE_Failure, CPLE_AppDefined, |
582 | 0 | "RegisterDriver() in m_bInDeferredDriverLoading: %s already " |
583 | 0 | "registered!", |
584 | 0 | pszDriverName); |
585 | 0 | delete poDriver; |
586 | 0 | return -1; |
587 | 0 | } |
588 | 0 | m_oMapRealDrivers[pszDriverName] = |
589 | 0 | std::unique_ptr<GDALDriver>(poDriver); |
590 | 0 | return -1; |
591 | 0 | } |
592 | | |
593 | | /* -------------------------------------------------------------------- */ |
594 | | /* Otherwise grow the list to hold the new entry. */ |
595 | | /* -------------------------------------------------------------------- */ |
596 | 4.31k | if (bHidden) |
597 | 0 | { |
598 | 0 | m_aoHiddenDrivers.push_back(std::unique_ptr<GDALDriver>(poDriver)); |
599 | 0 | return -1; |
600 | 0 | } |
601 | | |
602 | 4.31k | GDALDriver **papoNewDrivers = |
603 | 4.31k | static_cast<GDALDriver **>(VSI_REALLOC_VERBOSE( |
604 | 4.31k | papoDrivers, sizeof(GDALDriver *) * (nDrivers + 1))); |
605 | 4.31k | if (papoNewDrivers == nullptr) |
606 | 0 | return -1; |
607 | 4.31k | papoDrivers = papoNewDrivers; |
608 | | |
609 | 4.31k | papoDrivers[nDrivers] = poDriver; |
610 | 4.31k | ++nDrivers; |
611 | | |
612 | 4.31k | oMapNameToDrivers[CPLString(pszDriverName).toupper()] = poDriver; |
613 | | |
614 | 4.31k | if (EQUAL(pszDriverName, "MEM") && |
615 | 22 | oMapNameToDrivers.find("MEMORY") == oMapNameToDrivers.end()) |
616 | 22 | { |
617 | | // Instantiate a Memory driver, that is the same as the MEM one, |
618 | | // for legacy purposes. It can be queried through GetDriverByName() |
619 | | // but doesn't appear in the driver list. |
620 | 22 | auto poMemoryDriver = new GDALDriver(); |
621 | 22 | poMemoryDriver->SetDescription("Memory"); |
622 | 22 | poMemoryDriver->SetMetadata(poDriver->GetMetadata()); |
623 | 22 | poMemoryDriver->pfnOpen = poDriver->pfnOpen; |
624 | 22 | poMemoryDriver->pfnIdentify = poDriver->pfnIdentify; |
625 | 22 | poMemoryDriver->pfnCreate = poDriver->pfnCreate; |
626 | 22 | poMemoryDriver->pfnCreateMultiDimensional = |
627 | 22 | poDriver->pfnCreateMultiDimensional; |
628 | 22 | poMemoryDriver->pfnDelete = poDriver->pfnDelete; |
629 | 22 | oMapNameToDrivers[CPLString(poMemoryDriver->GetDescription()) |
630 | 22 | .toupper()] = poMemoryDriver; |
631 | 22 | } |
632 | | |
633 | 4.31k | int iResult = nDrivers - 1; |
634 | | |
635 | 4.31k | return iResult; |
636 | 4.31k | } |
637 | | |
638 | | /************************************************************************/ |
639 | | /* GetDriverByName_unlocked() */ |
640 | | /************************************************************************/ |
641 | | |
642 | | GDALDriver * |
643 | | GDALDriverManager::GetDriverByName_unlocked(const char *pszName) const |
644 | 401k | { |
645 | 401k | const CPLString osName = CPLString(pszName).toupper(); |
646 | 401k | if (osName == "MEMORY") |
647 | 430 | { |
648 | 430 | CPLErrorOnce(CE_Warning, CPLE_AppDefined, |
649 | 430 | "DeprecationWarning: 'Memory' driver is deprecated since " |
650 | 430 | "GDAL 3.11. Use 'MEM' onwards"); |
651 | 430 | } |
652 | 401k | auto oIter = oMapNameToDrivers.find(osName); |
653 | 401k | return oIter == oMapNameToDrivers.end() ? nullptr : oIter->second; |
654 | 401k | } |
655 | | |
656 | | /************************************************************************/ |
657 | | /* GDALRegisterDriver() */ |
658 | | /************************************************************************/ |
659 | | |
660 | | /** |
661 | | * \brief Register a driver for use. |
662 | | * |
663 | | * @see GDALDriverManager::GetRegisterDriver() |
664 | | */ |
665 | | |
666 | | int CPL_STDCALL GDALRegisterDriver(GDALDriverH hDriver) |
667 | | |
668 | 0 | { |
669 | 0 | VALIDATE_POINTER1(hDriver, "GDALRegisterDriver", 0); |
670 | | |
671 | 0 | return GetGDALDriverManager()->RegisterDriver( |
672 | 0 | static_cast<GDALDriver *>(hDriver)); |
673 | 0 | } |
674 | | |
675 | | /************************************************************************/ |
676 | | /* DeregisterDriver() */ |
677 | | /************************************************************************/ |
678 | | |
679 | | /** |
680 | | * \brief Deregister the passed driver. |
681 | | * |
682 | | * If the driver isn't found no change is made. |
683 | | * |
684 | | * The C analog is GDALDeregisterDriver(). |
685 | | * |
686 | | * @param poDriver the driver to deregister. |
687 | | */ |
688 | | |
689 | | void GDALDriverManager::DeregisterDriver(GDALDriver *poDriver) |
690 | | |
691 | 20 | { |
692 | 20 | CPLMutexHolderD(&hDMMutex); |
693 | | |
694 | 20 | int i = 0; // Used after for. |
695 | 2.97k | for (; i < nDrivers; ++i) |
696 | 2.97k | { |
697 | 2.97k | if (papoDrivers[i] == poDriver) |
698 | 20 | break; |
699 | 2.97k | } |
700 | | |
701 | 20 | if (i == nDrivers) |
702 | 0 | return; |
703 | | |
704 | 20 | oMapNameToDrivers.erase(CPLString(poDriver->GetDescription()).toupper()); |
705 | 20 | --nDrivers; |
706 | | // Move all following drivers down by one to pack the list. |
707 | 900 | while (i < nDrivers) |
708 | 880 | { |
709 | 880 | papoDrivers[i] = papoDrivers[i + 1]; |
710 | 880 | ++i; |
711 | 880 | } |
712 | 20 | } |
713 | | |
714 | | /************************************************************************/ |
715 | | /* GDALDeregisterDriver() */ |
716 | | /************************************************************************/ |
717 | | |
718 | | /** |
719 | | * \brief Deregister the passed driver. |
720 | | * |
721 | | * @see GDALDriverManager::GetDeregisterDriver() |
722 | | */ |
723 | | |
724 | | void CPL_STDCALL GDALDeregisterDriver(GDALDriverH hDriver) |
725 | | |
726 | 0 | { |
727 | 0 | VALIDATE_POINTER0(hDriver, "GDALDeregisterDriver"); |
728 | | |
729 | 0 | GetGDALDriverManager()->DeregisterDriver( |
730 | 0 | static_cast<GDALDriver *>(hDriver)); |
731 | 0 | } |
732 | | |
733 | | /************************************************************************/ |
734 | | /* GetDriverByName() */ |
735 | | /************************************************************************/ |
736 | | |
737 | | /** |
738 | | * \brief Fetch a driver based on the short name. |
739 | | * |
740 | | * The C analog is the GDALGetDriverByName() function. |
741 | | * |
742 | | * @param pszName the short name, such as GTiff, being searched for. |
743 | | * |
744 | | * @return the identified driver, or NULL if no match is found. |
745 | | */ |
746 | | |
747 | | GDALDriver *GDALDriverManager::GetDriverByName(const char *pszName) |
748 | | |
749 | 396k | { |
750 | 396k | CPLMutexHolderD(&hDMMutex); |
751 | | |
752 | 396k | if (m_bInDeferredDriverLoading) |
753 | 0 | { |
754 | 0 | return nullptr; |
755 | 0 | } |
756 | | |
757 | | // Alias old name to new name |
758 | 396k | if (EQUAL(pszName, "CartoDB")) |
759 | 0 | pszName = "Carto"; |
760 | | |
761 | 396k | return GetDriverByName_unlocked(pszName); |
762 | 396k | } |
763 | | |
764 | | /************************************************************************/ |
765 | | /* GDALGetDriverByName() */ |
766 | | /************************************************************************/ |
767 | | |
768 | | /** |
769 | | * \brief Fetch a driver based on the short name. |
770 | | * |
771 | | * @see GDALDriverManager::GetDriverByName() |
772 | | */ |
773 | | |
774 | | GDALDriverH CPL_STDCALL GDALGetDriverByName(const char *pszName) |
775 | | |
776 | 275k | { |
777 | 275k | VALIDATE_POINTER1(pszName, "GDALGetDriverByName", nullptr); |
778 | | |
779 | 275k | return GetGDALDriverManager()->GetDriverByName(pszName); |
780 | 275k | } |
781 | | |
782 | | /************************************************************************/ |
783 | | /* AutoSkipDrivers() */ |
784 | | /************************************************************************/ |
785 | | |
786 | | /** |
787 | | * \brief This method unload undesirable drivers. |
788 | | * |
789 | | * All drivers specified in the comma delimited list in the GDAL_SKIP |
790 | | * environment variable) will be deregistered and destroyed. This method |
791 | | * should normally be called after registration of standard drivers to allow |
792 | | * the user a way of unloading undesired drivers. The GDALAllRegister() |
793 | | * function already invokes AutoSkipDrivers() at the end, so if that functions |
794 | | * is called, it should not be necessary to call this method from application |
795 | | * code. |
796 | | * |
797 | | * Note: space separator is also accepted for backward compatibility, but some |
798 | | * vector formats have spaces in their names, so it is encouraged to use comma |
799 | | * to avoid issues. |
800 | | */ |
801 | | |
802 | | void GDALDriverManager::AutoSkipDrivers() |
803 | | |
804 | 22 | { |
805 | 22 | char **apapszList[2] = {nullptr, nullptr}; |
806 | 22 | const char *pszGDAL_SKIP = CPLGetConfigOption("GDAL_SKIP", nullptr); |
807 | 22 | if (pszGDAL_SKIP != nullptr) |
808 | 6 | { |
809 | | // Favor comma as a separator. If not found, then use space. |
810 | 6 | const char *pszSep = (strchr(pszGDAL_SKIP, ',') != nullptr) ? "," : " "; |
811 | 6 | apapszList[0] = |
812 | 6 | CSLTokenizeStringComplex(pszGDAL_SKIP, pszSep, FALSE, FALSE); |
813 | 6 | } |
814 | 22 | const char *pszOGR_SKIP = CPLGetConfigOption("OGR_SKIP", nullptr); |
815 | 22 | if (pszOGR_SKIP != nullptr) |
816 | 10 | { |
817 | | // OGR has always used comma as a separator. |
818 | 10 | apapszList[1] = |
819 | 10 | CSLTokenizeStringComplex(pszOGR_SKIP, ",", FALSE, FALSE); |
820 | 10 | } |
821 | | |
822 | 22 | for (auto j : {0, 1}) |
823 | 44 | { |
824 | 64 | for (int i = 0; apapszList[j] != nullptr && apapszList[j][i] != nullptr; |
825 | 44 | ++i) |
826 | 20 | { |
827 | 20 | GDALDriver *const poDriver = GetDriverByName(apapszList[j][i]); |
828 | | |
829 | 20 | if (poDriver == nullptr) |
830 | 0 | { |
831 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
832 | 0 | "Unable to find driver %s to unload from GDAL_SKIP " |
833 | 0 | "environment variable.", |
834 | 0 | apapszList[j][i]); |
835 | 0 | } |
836 | 20 | else |
837 | 20 | { |
838 | 20 | CPLDebug("GDAL", "AutoSkipDriver(%s)", apapszList[j][i]); |
839 | 20 | DeregisterDriver(poDriver); |
840 | 20 | delete poDriver; |
841 | 20 | } |
842 | 20 | } |
843 | 44 | } |
844 | | |
845 | 22 | CSLDestroy(apapszList[0]); |
846 | 22 | CSLDestroy(apapszList[1]); |
847 | 22 | } |
848 | | |
849 | | /************************************************************************/ |
850 | | /* GetSearchPaths() */ |
851 | | /************************************************************************/ |
852 | | |
853 | | //! @cond Doxygen_Suppress |
854 | | char **GDALDriverManager::GetSearchPaths(const char *pszGDAL_DRIVER_PATH) |
855 | 44 | { |
856 | 44 | char **papszSearchPaths = nullptr; |
857 | 44 | CPL_IGNORE_RET_VAL(pszGDAL_DRIVER_PATH); |
858 | 44 | #ifndef GDAL_NO_AUTOLOAD |
859 | 44 | if (pszGDAL_DRIVER_PATH != nullptr) |
860 | 0 | { |
861 | | #ifdef _WIN32 |
862 | | papszSearchPaths = |
863 | | CSLTokenizeStringComplex(pszGDAL_DRIVER_PATH, ";", TRUE, FALSE); |
864 | | #else |
865 | 0 | papszSearchPaths = |
866 | 0 | CSLTokenizeStringComplex(pszGDAL_DRIVER_PATH, ":", TRUE, FALSE); |
867 | 0 | #endif |
868 | 0 | } |
869 | 44 | else |
870 | 44 | { |
871 | 44 | #ifdef INSTALL_PLUGIN_FULL_DIR |
872 | | // CMake way |
873 | 44 | papszSearchPaths = |
874 | 44 | CSLAddString(papszSearchPaths, INSTALL_PLUGIN_FULL_DIR); |
875 | | #elif defined(GDAL_PREFIX) |
876 | | papszSearchPaths = CSLAddString(papszSearchPaths, |
877 | | #ifdef MACOSX_FRAMEWORK |
878 | | GDAL_PREFIX "/PlugIns"); |
879 | | #else |
880 | | GDAL_PREFIX "/lib/gdalplugins"); |
881 | | #endif |
882 | | #else |
883 | | char szExecPath[1024]; |
884 | | |
885 | | if (CPLGetExecPath(szExecPath, sizeof(szExecPath))) |
886 | | { |
887 | | papszSearchPaths = CSLAddString( |
888 | | papszSearchPaths, |
889 | | (CPLGetDirnameSafe(szExecPath) + "\\gdalplugins").c_str()); |
890 | | } |
891 | | else |
892 | | { |
893 | | papszSearchPaths = |
894 | | CSLAddString(papszSearchPaths, "/usr/local/lib/gdalplugins"); |
895 | | } |
896 | | #endif |
897 | | |
898 | | #ifdef MACOSX_FRAMEWORK |
899 | | #define num2str(x) str(x) |
900 | | #define str(x) #x |
901 | | papszSearchPaths = CSLAddString( |
902 | | papszSearchPaths, |
903 | | "/Library/Application Support/GDAL/" num2str( |
904 | | GDAL_VERSION_MAJOR) "." num2str(GDAL_VERSION_MINOR) "/PlugIns"); |
905 | | #endif |
906 | 44 | } |
907 | 44 | #endif // GDAL_NO_AUTOLOAD |
908 | 44 | return papszSearchPaths; |
909 | 44 | } |
910 | | |
911 | | //! @endcond |
912 | | |
913 | | /************************************************************************/ |
914 | | /* LoadPlugin() */ |
915 | | /************************************************************************/ |
916 | | |
917 | | /** |
918 | | * \brief Load a single GDAL driver/plugin from shared libraries. |
919 | | * |
920 | | * This function will load a single named driver/plugin from shared libraries. |
921 | | * It searches the "driver path" for .so (or .dll) files named |
922 | | * "gdal_{name}.[so|dll|dylib]" or "ogr_{name}.[so|dll|dylib]", then tries to |
923 | | * call a function within them called GDALRegister_{name}(), or failing that |
924 | | * called GDALRegisterMe(). |
925 | | * |
926 | | * \see GDALDriverManager::AutoLoadDrivers() for the rules used to determine |
927 | | * which paths are searched for plugin library files. |
928 | | */ |
929 | | |
930 | | CPLErr GDALDriverManager::LoadPlugin(const char *name) |
931 | 0 | { |
932 | | #ifdef GDAL_NO_AUTOLOAD |
933 | | CPLDebug("GDAL", "GDALDriverManager::LoadPlugin() not compiled in."); |
934 | | return CE_Failure; |
935 | | #else |
936 | 0 | const char *pszGDAL_DRIVER_PATH = |
937 | 0 | CPLGetConfigOption("GDAL_DRIVER_PATH", nullptr); |
938 | 0 | if (pszGDAL_DRIVER_PATH == nullptr) |
939 | 0 | pszGDAL_DRIVER_PATH = CPLGetConfigOption("OGR_DRIVER_PATH", nullptr); |
940 | | |
941 | | /* -------------------------------------------------------------------- */ |
942 | | /* Where should we look for stuff? */ |
943 | | /* -------------------------------------------------------------------- */ |
944 | 0 | const CPLStringList aosSearchPaths(GetSearchPaths(pszGDAL_DRIVER_PATH)); |
945 | | |
946 | | /* -------------------------------------------------------------------- */ |
947 | | /* Format the ABI version specific subdirectory to look in. */ |
948 | | /* -------------------------------------------------------------------- */ |
949 | 0 | CPLString osABIVersion; |
950 | |
|
951 | 0 | osABIVersion.Printf("%d.%d", GDAL_VERSION_MAJOR, GDAL_VERSION_MINOR); |
952 | | |
953 | | /* -------------------------------------------------------------------- */ |
954 | | /* Scan each directory looking for files matching */ |
955 | | /* gdal_{name}.[so|dll|dylib] or ogr_{name}.[so|dll|dylib] */ |
956 | | /* -------------------------------------------------------------------- */ |
957 | 0 | const int nSearchPaths = aosSearchPaths.size(); |
958 | 0 | for (int iDir = 0; iDir < nSearchPaths; ++iDir) |
959 | 0 | { |
960 | 0 | std::string osABISpecificDir = |
961 | 0 | CPLFormFilenameSafe(aosSearchPaths[iDir], osABIVersion, nullptr); |
962 | |
|
963 | 0 | VSIStatBufL sStatBuf; |
964 | 0 | if (VSIStatL(osABISpecificDir.c_str(), &sStatBuf) != 0) |
965 | 0 | osABISpecificDir = aosSearchPaths[iDir]; |
966 | |
|
967 | 0 | CPLString gdal_or_ogr[2] = {"gdal_", "ogr_"}; |
968 | 0 | CPLString platformExtensions[3] = {"so", "dll", "dylib"}; |
969 | |
|
970 | 0 | for (const CPLString &prefix : gdal_or_ogr) |
971 | 0 | { |
972 | 0 | for (const CPLString &extension : platformExtensions) |
973 | 0 | { |
974 | 0 | const std::string osFilename = CPLFormFilenameSafe( |
975 | 0 | osABISpecificDir.c_str(), |
976 | 0 | CPLSPrintf("%s%s", prefix.c_str(), name), extension); |
977 | 0 | if (VSIStatL(osFilename.c_str(), &sStatBuf) != 0) |
978 | 0 | continue; |
979 | | |
980 | 0 | CPLString osFuncName; |
981 | 0 | if (EQUAL(prefix, "gdal_")) |
982 | 0 | { |
983 | 0 | osFuncName.Printf("GDALRegister_%s", name); |
984 | 0 | } |
985 | 0 | else |
986 | 0 | { |
987 | 0 | osFuncName.Printf("RegisterOGR%s", name); |
988 | 0 | } |
989 | 0 | CPLErrorReset(); |
990 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
991 | 0 | void *pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
992 | 0 | CPLPopErrorHandler(); |
993 | 0 | if (pRegister == nullptr) |
994 | 0 | { |
995 | 0 | CPLString osLastErrorMsg(CPLGetLastErrorMsg()); |
996 | 0 | osFuncName = "GDALRegisterMe"; |
997 | 0 | pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
998 | 0 | if (pRegister == nullptr) |
999 | 0 | { |
1000 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s", |
1001 | 0 | osLastErrorMsg.c_str()); |
1002 | 0 | return CE_Failure; |
1003 | 0 | } |
1004 | 0 | } |
1005 | 0 | CPLDebug("GDAL", "Registering %s using %s in %s", name, |
1006 | 0 | osFuncName.c_str(), osFilename.c_str()); |
1007 | 0 | CPLErrorReset(); |
1008 | 0 | reinterpret_cast<void (*)()>(pRegister)(); |
1009 | 0 | if (CPLGetErrorCounter() > 0) |
1010 | 0 | { |
1011 | 0 | return CE_Failure; |
1012 | 0 | } |
1013 | 0 | return CE_None; |
1014 | 0 | } |
1015 | 0 | } |
1016 | 0 | } |
1017 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1018 | 0 | "Failed to find driver %s in configured driver paths.", name); |
1019 | 0 | return CE_Failure; |
1020 | 0 | #endif // GDAL_NO_AUTOLOAD |
1021 | 0 | } |
1022 | | |
1023 | | /************************************************************************/ |
1024 | | /* AutoLoadDrivers() */ |
1025 | | /************************************************************************/ |
1026 | | |
1027 | | /** |
1028 | | * \brief Auto-load GDAL drivers from shared libraries. |
1029 | | * |
1030 | | * This function will automatically load drivers from shared libraries. It |
1031 | | * searches the "driver path" for .so (or .dll) files that start with the |
1032 | | * prefix "gdal_X.so". It then tries to load them and then tries to call a |
1033 | | * function within them called GDALRegister_X() where the 'X' is the same as |
1034 | | * the remainder of the shared library basename ('X' is case sensitive), or |
1035 | | * failing that to call GDALRegisterMe(). |
1036 | | * |
1037 | | * There are a few rules for the driver path. If the GDAL_DRIVER_PATH |
1038 | | * environment variable it set, it is taken to be a list of directories to |
1039 | | * search separated by colons on UNIX, or semi-colons on Windows. Otherwise |
1040 | | * the /usr/local/lib/gdalplugins directory, and (if known) the |
1041 | | * lib/gdalplugins subdirectory of the gdal home directory are searched on |
1042 | | * UNIX and \$(BINDIR)\\gdalplugins on Windows. |
1043 | | * |
1044 | | * Auto loading can be completely disabled by setting the GDAL_DRIVER_PATH |
1045 | | * config option to "disable". |
1046 | | * |
1047 | | * Starting with gdal 3.5, the default search path \$(prefix)/lib/gdalplugins |
1048 | | * can be overridden at compile time by passing |
1049 | | * -DINSTALL_PLUGIN_DIR=/another/path to cmake. |
1050 | | */ |
1051 | | |
1052 | | void GDALDriverManager::AutoLoadDrivers() |
1053 | | |
1054 | 22 | { |
1055 | | #ifdef GDAL_NO_AUTOLOAD |
1056 | | CPLDebug("GDAL", "GDALDriverManager::AutoLoadDrivers() not compiled in."); |
1057 | | #else |
1058 | 22 | const char *pszGDAL_DRIVER_PATH = |
1059 | 22 | CPLGetConfigOption("GDAL_DRIVER_PATH", nullptr); |
1060 | 22 | if (pszGDAL_DRIVER_PATH == nullptr) |
1061 | 22 | pszGDAL_DRIVER_PATH = CPLGetConfigOption("OGR_DRIVER_PATH", nullptr); |
1062 | | |
1063 | | /* -------------------------------------------------------------------- */ |
1064 | | /* Allow applications to completely disable this search by */ |
1065 | | /* setting the driver path to the special string "disable". */ |
1066 | | /* -------------------------------------------------------------------- */ |
1067 | 22 | if (pszGDAL_DRIVER_PATH != nullptr && EQUAL(pszGDAL_DRIVER_PATH, "disable")) |
1068 | 0 | { |
1069 | 0 | CPLDebug("GDAL", "GDALDriverManager::AutoLoadDrivers() disabled."); |
1070 | 0 | return; |
1071 | 0 | } |
1072 | | |
1073 | | /* -------------------------------------------------------------------- */ |
1074 | | /* Where should we look for stuff? */ |
1075 | | /* -------------------------------------------------------------------- */ |
1076 | 22 | char **papszSearchPaths = GetSearchPaths(pszGDAL_DRIVER_PATH); |
1077 | | |
1078 | | /* -------------------------------------------------------------------- */ |
1079 | | /* Format the ABI version specific subdirectory to look in. */ |
1080 | | /* -------------------------------------------------------------------- */ |
1081 | 22 | CPLString osABIVersion; |
1082 | | |
1083 | 22 | osABIVersion.Printf("%d.%d", GDAL_VERSION_MAJOR, GDAL_VERSION_MINOR); |
1084 | | |
1085 | | /* -------------------------------------------------------------------- */ |
1086 | | /* Scan each directory looking for files starting with gdal_ */ |
1087 | | /* -------------------------------------------------------------------- */ |
1088 | 22 | const int nSearchPaths = CSLCount(papszSearchPaths); |
1089 | 22 | bool bFoundOnePlugin = false; |
1090 | 44 | for (int iDir = 0; iDir < nSearchPaths; ++iDir) |
1091 | 22 | { |
1092 | 22 | std::string osABISpecificDir = |
1093 | 22 | CPLFormFilenameSafe(papszSearchPaths[iDir], osABIVersion, nullptr); |
1094 | | |
1095 | 22 | VSIStatBufL sStatBuf; |
1096 | 22 | if (VSIStatL(osABISpecificDir.c_str(), &sStatBuf) != 0) |
1097 | 22 | osABISpecificDir = papszSearchPaths[iDir]; |
1098 | | |
1099 | 22 | char **papszFiles = VSIReadDir(osABISpecificDir.c_str()); |
1100 | 22 | const int nFileCount = CSLCount(papszFiles); |
1101 | | |
1102 | 22 | for (int iFile = 0; iFile < nFileCount; ++iFile) |
1103 | 0 | { |
1104 | 0 | const CPLString osExtension = |
1105 | 0 | CPLGetExtensionSafe(papszFiles[iFile]); |
1106 | |
|
1107 | 0 | if (!EQUAL(osExtension, "dll") && !EQUAL(osExtension, "so") && |
1108 | 0 | !EQUAL(osExtension, "dylib")) |
1109 | 0 | { |
1110 | 0 | if (strcmp(papszFiles[iFile], "drivers.ini") == 0) |
1111 | 0 | { |
1112 | 0 | m_osDriversIniPath = CPLFormFilenameSafe( |
1113 | 0 | osABISpecificDir.c_str(), papszFiles[iFile], nullptr); |
1114 | 0 | } |
1115 | 0 | continue; |
1116 | 0 | } |
1117 | | |
1118 | 0 | if (cpl::contains(m_oSetPluginFileNames, papszFiles[iFile])) |
1119 | 0 | { |
1120 | 0 | continue; |
1121 | 0 | } |
1122 | | |
1123 | 0 | CPLString osFuncName; |
1124 | 0 | if (STARTS_WITH_CI(papszFiles[iFile], "gdal_")) |
1125 | 0 | { |
1126 | 0 | osFuncName.Printf( |
1127 | 0 | "GDALRegister_%s", |
1128 | 0 | CPLGetBasenameSafe(papszFiles[iFile]).c_str() + |
1129 | 0 | strlen("gdal_")); |
1130 | 0 | } |
1131 | 0 | else if (STARTS_WITH_CI(papszFiles[iFile], "ogr_")) |
1132 | 0 | { |
1133 | 0 | osFuncName.Printf( |
1134 | 0 | "RegisterOGR%s", |
1135 | 0 | CPLGetBasenameSafe(papszFiles[iFile]).c_str() + |
1136 | 0 | strlen("ogr_")); |
1137 | 0 | } |
1138 | 0 | else |
1139 | 0 | continue; |
1140 | | |
1141 | 0 | const std::string osFilename = CPLFormFilenameSafe( |
1142 | 0 | osABISpecificDir.c_str(), papszFiles[iFile], nullptr); |
1143 | |
|
1144 | 0 | CPLErrorReset(); |
1145 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1146 | 0 | void *pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
1147 | 0 | CPLPopErrorHandler(); |
1148 | 0 | if (pRegister == nullptr) |
1149 | 0 | { |
1150 | 0 | CPLString osLastErrorMsg(CPLGetLastErrorMsg()); |
1151 | 0 | osFuncName = "GDALRegisterMe"; |
1152 | 0 | pRegister = CPLGetSymbol(osFilename.c_str(), osFuncName); |
1153 | 0 | if (pRegister == nullptr) |
1154 | 0 | { |
1155 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s", |
1156 | 0 | osLastErrorMsg.c_str()); |
1157 | 0 | } |
1158 | 0 | } |
1159 | |
|
1160 | 0 | if (pRegister != nullptr) |
1161 | 0 | { |
1162 | 0 | bFoundOnePlugin = true; |
1163 | 0 | CPLDebug("GDAL", "Auto register %s using %s.", |
1164 | 0 | osFilename.c_str(), osFuncName.c_str()); |
1165 | |
|
1166 | 0 | reinterpret_cast<void (*)()>(pRegister)(); |
1167 | 0 | } |
1168 | 0 | } |
1169 | | |
1170 | 22 | CSLDestroy(papszFiles); |
1171 | 22 | } |
1172 | | |
1173 | 22 | CSLDestroy(papszSearchPaths); |
1174 | | |
1175 | | // No need to reorder drivers if there are no plugins |
1176 | 22 | if (!bFoundOnePlugin) |
1177 | 22 | m_osDriversIniPath.clear(); |
1178 | | |
1179 | 22 | #endif // GDAL_NO_AUTOLOAD |
1180 | 22 | } |
1181 | | |
1182 | | /************************************************************************/ |
1183 | | /* ReorderDrivers() */ |
1184 | | /************************************************************************/ |
1185 | | |
1186 | | /** |
1187 | | * \brief Reorder drivers according to the order of the drivers.ini file. |
1188 | | * |
1189 | | * This function is called by GDALAllRegister(), at the end of driver loading, |
1190 | | * in particular after plugin loading. |
1191 | | * It will load the drivers.ini configuration file located next to plugins and |
1192 | | * will use it to reorder the registration order of drivers. This can be |
1193 | | * important in some situations where multiple drivers could open the same |
1194 | | * dataset. |
1195 | | */ |
1196 | | |
1197 | | void GDALDriverManager::ReorderDrivers() |
1198 | 22 | { |
1199 | 22 | #ifndef GDAL_NO_AUTOLOAD |
1200 | 22 | if (m_osDriversIniPath.empty()) |
1201 | 22 | { |
1202 | 22 | if (m_oSetPluginFileNames.empty()) |
1203 | 22 | return; |
1204 | | |
1205 | 0 | m_osDriversIniPath = GetPluginFullPath("drivers.ini"); |
1206 | 0 | if (m_osDriversIniPath.empty()) |
1207 | 0 | return; |
1208 | 0 | } |
1209 | | |
1210 | 0 | CPLMutexHolderD(&hDMMutex); |
1211 | |
|
1212 | 0 | VSILFILE *fp = VSIFOpenL(m_osDriversIniPath.c_str(), "rb"); |
1213 | 0 | if (fp == nullptr) |
1214 | 0 | return; |
1215 | | |
1216 | | // Parse drivers.ini |
1217 | 0 | bool bInOrderSection = false; |
1218 | 0 | std::vector<std::string> aosOrderedDrivers; |
1219 | 0 | std::set<std::string> oSetOrderedDrivers; |
1220 | 0 | while (const char *pszLine = CPLReadLine2L(fp, 1024, nullptr)) |
1221 | 0 | { |
1222 | 0 | if (pszLine[0] == '#') |
1223 | 0 | continue; |
1224 | 0 | int i = 0; |
1225 | 0 | while (pszLine[i] != 0 && |
1226 | 0 | isspace(static_cast<unsigned char>(pszLine[i]))) |
1227 | 0 | i++; |
1228 | 0 | if (pszLine[i] == 0) |
1229 | 0 | continue; |
1230 | 0 | if (strcmp(pszLine, "[order]") == 0) |
1231 | 0 | { |
1232 | 0 | bInOrderSection = true; |
1233 | 0 | } |
1234 | 0 | else if (pszLine[0] == '[') |
1235 | 0 | { |
1236 | 0 | bInOrderSection = false; |
1237 | 0 | } |
1238 | 0 | else if (bInOrderSection) |
1239 | 0 | { |
1240 | 0 | CPLString osUCDriverName(pszLine); |
1241 | 0 | osUCDriverName.toupper(); |
1242 | 0 | if (osUCDriverName != "MEMORY") |
1243 | 0 | { |
1244 | 0 | if (cpl::contains(oSetOrderedDrivers, osUCDriverName)) |
1245 | 0 | { |
1246 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1247 | 0 | "Duplicated name %s in [order] section", pszLine); |
1248 | 0 | } |
1249 | 0 | else if (cpl::contains(oMapNameToDrivers, osUCDriverName)) |
1250 | 0 | { |
1251 | 0 | aosOrderedDrivers.emplace_back(pszLine); |
1252 | 0 | oSetOrderedDrivers.insert(std::move(osUCDriverName)); |
1253 | 0 | } |
1254 | | #ifdef DEBUG_VERBOSE |
1255 | | else |
1256 | | { |
1257 | | // Completely expected situation for "non-maximal" builds, |
1258 | | // but can help diagnose bad entries in drivers.ini |
1259 | | CPLDebug("GDAL", |
1260 | | "Driver %s is listed in %s but not registered.", |
1261 | | pszLine, m_osDriversIniPath.c_str()); |
1262 | | } |
1263 | | #endif |
1264 | 0 | } |
1265 | 0 | } |
1266 | 0 | } |
1267 | 0 | VSIFCloseL(fp); |
1268 | | |
1269 | | // Find potential registered drivers not in drivers.ini, and put them in |
1270 | | // their registration order in aosUnorderedDrivers |
1271 | 0 | std::vector<std::string> aosUnorderedDrivers; |
1272 | 0 | for (int i = 0; i < nDrivers; ++i) |
1273 | 0 | { |
1274 | 0 | const char *pszName = papoDrivers[i]->GetDescription(); |
1275 | 0 | if (!cpl::contains(oSetOrderedDrivers, CPLString(pszName).toupper())) |
1276 | 0 | { |
1277 | | // Could happen for a private plugin |
1278 | 0 | CPLDebug("GDAL", |
1279 | 0 | "Driver %s is registered but not listed in %s. " |
1280 | 0 | "It will be registered before other drivers.", |
1281 | 0 | pszName, m_osDriversIniPath.c_str()); |
1282 | 0 | aosUnorderedDrivers.emplace_back(pszName); |
1283 | 0 | } |
1284 | 0 | } |
1285 | | |
1286 | | // Put aosUnorderedDrivers in front of existing aosOrderedDrivers |
1287 | 0 | if (!aosUnorderedDrivers.empty()) |
1288 | 0 | { |
1289 | 0 | aosUnorderedDrivers.insert(aosUnorderedDrivers.end(), |
1290 | 0 | aosOrderedDrivers.begin(), |
1291 | 0 | aosOrderedDrivers.end()); |
1292 | 0 | std::swap(aosOrderedDrivers, aosUnorderedDrivers); |
1293 | 0 | } |
1294 | | |
1295 | | // Update papoDrivers[] to reflect aosOrderedDrivers order. |
1296 | 0 | CPLAssert(static_cast<int>(aosOrderedDrivers.size()) == nDrivers); |
1297 | 0 | for (int i = 0; i < nDrivers; ++i) |
1298 | 0 | { |
1299 | 0 | const auto oIter = |
1300 | 0 | oMapNameToDrivers.find(CPLString(aosOrderedDrivers[i]).toupper()); |
1301 | 0 | CPLAssert(oIter != oMapNameToDrivers.end()); |
1302 | 0 | papoDrivers[i] = oIter->second; |
1303 | 0 | } |
1304 | 0 | #endif |
1305 | 0 | } |
1306 | | |
1307 | | /************************************************************************/ |
1308 | | /* GDALPluginDriverProxy */ |
1309 | | /************************************************************************/ |
1310 | | |
1311 | | /** Constructor for a plugin driver proxy. |
1312 | | * |
1313 | | * @param osPluginFileName Plugin filename. e.g "ogr_Parquet.so" |
1314 | | */ |
1315 | | GDALPluginDriverProxy::GDALPluginDriverProxy( |
1316 | | const std::string &osPluginFileName) |
1317 | 0 | : m_osPluginFileName(osPluginFileName) |
1318 | 0 | { |
1319 | 0 | } |
1320 | | |
1321 | | //! @cond Doxygen_Suppress |
1322 | | #define DEFINE_DRIVER_METHOD_GET_CALLBACK(method_name, output_type) \ |
1323 | | GDALDriver::output_type GDALPluginDriverProxy::method_name() \ |
1324 | 0 | { \ |
1325 | 0 | auto poRealDriver = GetRealDriver(); \ |
1326 | 0 | if (!poRealDriver) \ |
1327 | 0 | return nullptr; \ |
1328 | 0 | return poRealDriver->method_name(); \ |
1329 | 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() |
1330 | | |
1331 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetOpenCallback, OpenCallback) |
1332 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCreateCallback, CreateCallback) |
1333 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCreateMultiDimensionalCallback, |
1334 | | CreateMultiDimensionalCallback) |
1335 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCreateCopyCallback, CreateCopyCallback) |
1336 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetDeleteCallback, DeleteCallback) |
1337 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetRenameCallback, RenameCallback) |
1338 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetCopyFilesCallback, CopyFilesCallback) |
1339 | | DEFINE_DRIVER_METHOD_GET_CALLBACK(GetInstantiateAlgorithmCallback, |
1340 | | InstantiateAlgorithmCallback) |
1341 | | |
1342 | | //! @endcond |
1343 | | |
1344 | | CSLConstList GDALPluginDriverProxy::GetMetadata(const char *pszDomain) |
1345 | 0 | { |
1346 | 0 | auto poRealDriver = GetRealDriver(); |
1347 | 0 | if (!poRealDriver) |
1348 | 0 | return nullptr; |
1349 | 0 | return poRealDriver->GetMetadata(pszDomain); |
1350 | 0 | } |
1351 | | |
1352 | | CPLErr GDALPluginDriverProxy::SetMetadataItem(const char *pszName, |
1353 | | const char *pszValue, |
1354 | | const char *pszDomain) |
1355 | 0 | { |
1356 | 0 | if (!pszDomain || pszDomain[0] == 0) |
1357 | 0 | { |
1358 | 0 | if (!EQUAL(pszName, GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE)) |
1359 | 0 | { |
1360 | 0 | m_oSetMetadataItems.insert(pszName); |
1361 | 0 | } |
1362 | 0 | } |
1363 | 0 | return GDALDriver::SetMetadataItem(pszName, pszValue, pszDomain); |
1364 | 0 | } |
1365 | | |
1366 | | static const char *const apszProxyMetadataItems[] = { |
1367 | | GDAL_DMD_LONGNAME, |
1368 | | GDAL_DMD_EXTENSIONS, |
1369 | | GDAL_DMD_EXTENSION, |
1370 | | GDAL_DCAP_RASTER, |
1371 | | GDAL_DCAP_MULTIDIM_RASTER, |
1372 | | GDAL_DCAP_VECTOR, |
1373 | | GDAL_DCAP_GNM, |
1374 | | GDAL_DMD_OPENOPTIONLIST, |
1375 | | GDAL_DCAP_OPEN, |
1376 | | GDAL_DCAP_CREATE, |
1377 | | GDAL_DCAP_CREATE_MULTIDIMENSIONAL, |
1378 | | GDAL_DCAP_CREATECOPY, |
1379 | | GDAL_DCAP_UPDATE, |
1380 | | GDAL_DMD_SUBDATASETS, |
1381 | | GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, |
1382 | | GDAL_DCAP_NONSPATIAL, |
1383 | | GDAL_DMD_CONNECTION_PREFIX, |
1384 | | GDAL_DCAP_VECTOR_TRANSLATE_FROM, |
1385 | | GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE, |
1386 | | }; |
1387 | | |
1388 | | const char *GDALPluginDriverProxy::GetMetadataItem(const char *pszName, |
1389 | | const char *pszDomain) |
1390 | 0 | { |
1391 | 0 | const auto IsListedProxyMetadataItem = [](const char *pszItem) |
1392 | 0 | { |
1393 | 0 | for (const char *pszListedItem : apszProxyMetadataItems) |
1394 | 0 | { |
1395 | 0 | if (EQUAL(pszItem, pszListedItem)) |
1396 | 0 | return true; |
1397 | 0 | } |
1398 | 0 | return false; |
1399 | 0 | }; |
1400 | |
|
1401 | 0 | if (!pszDomain || pszDomain[0] == 0) |
1402 | 0 | { |
1403 | 0 | if (EQUAL(pszName, "IS_NON_LOADED_PLUGIN")) |
1404 | 0 | { |
1405 | 0 | return !m_poRealDriver ? "YES" : nullptr; |
1406 | 0 | } |
1407 | 0 | else if (EQUAL(pszName, "MISSING_PLUGIN_FILENAME")) |
1408 | 0 | { |
1409 | 0 | return m_osPluginFullPath.empty() ? m_osPluginFileName.c_str() |
1410 | 0 | : nullptr; |
1411 | 0 | } |
1412 | 0 | else if (IsListedProxyMetadataItem(pszName)) |
1413 | 0 | { |
1414 | 0 | const char *pszValue = |
1415 | 0 | GDALDriver::GetMetadataItem(pszName, pszDomain); |
1416 | 0 | if (!pszValue && EQUAL(pszName, GDAL_DMD_EXTENSION)) |
1417 | 0 | { |
1418 | 0 | const char *pszOtherValue = |
1419 | 0 | GDALDriver::GetMetadataItem(GDAL_DMD_EXTENSIONS, pszDomain); |
1420 | 0 | if (pszOtherValue && strchr(pszOtherValue, ' ')) |
1421 | 0 | return pszOtherValue; |
1422 | 0 | } |
1423 | 0 | else if (!pszValue && EQUAL(pszName, GDAL_DMD_EXTENSIONS)) |
1424 | 0 | { |
1425 | 0 | return GDALDriver::GetMetadataItem(GDAL_DMD_EXTENSION, |
1426 | 0 | pszDomain); |
1427 | 0 | } |
1428 | 0 | return pszValue; |
1429 | 0 | } |
1430 | 0 | else if (cpl::contains(m_oSetMetadataItems, pszName)) |
1431 | 0 | { |
1432 | 0 | return GDALDriver::GetMetadataItem(pszName, pszDomain); |
1433 | 0 | } |
1434 | 0 | } |
1435 | | |
1436 | 0 | auto poRealDriver = GetRealDriver(); |
1437 | 0 | if (!poRealDriver) |
1438 | 0 | return nullptr; |
1439 | 0 | return poRealDriver->GetMetadataItem(pszName, pszDomain); |
1440 | 0 | } |
1441 | | |
1442 | | /************************************************************************/ |
1443 | | /* GetRealDriver() */ |
1444 | | /************************************************************************/ |
1445 | | |
1446 | | GDALDriver *GDALPluginDriverProxy::GetRealDriver() |
1447 | 0 | { |
1448 | | // No need to take the mutex has this member variable is not modified |
1449 | | // under the mutex. |
1450 | 0 | if (m_osPluginFullPath.empty()) |
1451 | 0 | return nullptr; |
1452 | | |
1453 | 0 | CPLMutexHolderD(&hDMMutex); |
1454 | |
|
1455 | 0 | if (m_poRealDriver) |
1456 | 0 | return m_poRealDriver.get(); |
1457 | | |
1458 | 0 | auto poDriverManager = GetGDALDriverManager(); |
1459 | 0 | auto oIter = poDriverManager->m_oMapRealDrivers.find(GetDescription()); |
1460 | 0 | if (oIter != poDriverManager->m_oMapRealDrivers.end()) |
1461 | 0 | { |
1462 | 0 | m_poRealDriver = std::move(oIter->second); |
1463 | 0 | poDriverManager->m_oMapRealDrivers.erase(oIter); |
1464 | 0 | } |
1465 | 0 | else |
1466 | 0 | { |
1467 | | #ifdef GDAL_NO_AUTOLOAD |
1468 | | return nullptr; |
1469 | | #else |
1470 | 0 | CPLString osFuncName; |
1471 | 0 | if (STARTS_WITH(m_osPluginFileName.c_str(), "gdal_")) |
1472 | 0 | { |
1473 | 0 | osFuncName = "GDALRegister_"; |
1474 | 0 | osFuncName += m_osPluginFileName.substr( |
1475 | 0 | strlen("gdal_"), |
1476 | 0 | m_osPluginFileName.find('.') - strlen("gdal_")); |
1477 | 0 | } |
1478 | 0 | else |
1479 | 0 | { |
1480 | 0 | CPLAssert(STARTS_WITH(m_osPluginFileName.c_str(), "ogr_")); |
1481 | 0 | osFuncName = "RegisterOGR"; |
1482 | 0 | osFuncName += m_osPluginFileName.substr( |
1483 | 0 | strlen("ogr_"), m_osPluginFileName.find('.') - strlen("ogr_")); |
1484 | 0 | } |
1485 | |
|
1486 | 0 | CPLErrorReset(); |
1487 | 0 | CPLPushErrorHandler(CPLQuietErrorHandler); |
1488 | 0 | void *pRegister = CPLGetSymbol(m_osPluginFullPath.c_str(), osFuncName); |
1489 | 0 | CPLPopErrorHandler(); |
1490 | 0 | if (pRegister == nullptr) |
1491 | 0 | { |
1492 | 0 | CPLString osLastErrorMsg(CPLGetLastErrorMsg()); |
1493 | 0 | osFuncName = "GDALRegisterMe"; |
1494 | 0 | pRegister = CPLGetSymbol(m_osPluginFullPath.c_str(), osFuncName); |
1495 | 0 | if (pRegister == nullptr) |
1496 | 0 | { |
1497 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s", |
1498 | 0 | osLastErrorMsg.c_str()); |
1499 | 0 | } |
1500 | 0 | } |
1501 | |
|
1502 | 0 | if (pRegister != nullptr) |
1503 | 0 | { |
1504 | 0 | CPLDebug("GDAL", "On-demand registering %s using %s.", |
1505 | 0 | m_osPluginFullPath.c_str(), osFuncName.c_str()); |
1506 | |
|
1507 | 0 | poDriverManager->m_bInDeferredDriverLoading = true; |
1508 | 0 | try |
1509 | 0 | { |
1510 | 0 | reinterpret_cast<void (*)()>(pRegister)(); |
1511 | 0 | } |
1512 | 0 | catch (...) |
1513 | 0 | { |
1514 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "%s threw an exception", |
1515 | 0 | osFuncName.c_str()); |
1516 | 0 | } |
1517 | 0 | poDriverManager->m_bInDeferredDriverLoading = false; |
1518 | |
|
1519 | 0 | oIter = poDriverManager->m_oMapRealDrivers.find(GetDescription()); |
1520 | 0 | if (oIter == poDriverManager->m_oMapRealDrivers.end()) |
1521 | 0 | { |
1522 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1523 | 0 | "Function %s of %s did not register a driver %s", |
1524 | 0 | osFuncName.c_str(), m_osPluginFullPath.c_str(), |
1525 | 0 | GetDescription()); |
1526 | 0 | } |
1527 | 0 | else |
1528 | 0 | { |
1529 | 0 | m_poRealDriver = std::move(oIter->second); |
1530 | 0 | poDriverManager->m_oMapRealDrivers.erase(oIter); |
1531 | 0 | } |
1532 | 0 | } |
1533 | 0 | #endif // GDAL_NO_AUTOLOAD |
1534 | 0 | } |
1535 | |
|
1536 | 0 | if (m_poRealDriver) |
1537 | 0 | { |
1538 | 0 | pfnDelete = m_poRealDriver->pfnDelete; |
1539 | 0 | pfnRename = m_poRealDriver->pfnRename; |
1540 | 0 | pfnCopyFiles = m_poRealDriver->pfnCopyFiles; |
1541 | |
|
1542 | 0 | if (strcmp(GetDescription(), m_poRealDriver->GetDescription()) != 0) |
1543 | 0 | { |
1544 | 0 | CPLError( |
1545 | 0 | CE_Warning, CPLE_AppDefined, |
1546 | 0 | "Driver %s has not the same name as its underlying driver (%s)", |
1547 | 0 | GetDescription(), m_poRealDriver->GetDescription()); |
1548 | 0 | } |
1549 | |
|
1550 | 0 | for (const auto &osItem : m_oSetMetadataItems) |
1551 | 0 | { |
1552 | 0 | const char *pszProxyValue = GetMetadataItem(osItem.c_str()); |
1553 | 0 | const char *pszRealValue = |
1554 | 0 | m_poRealDriver->GetMetadataItem(osItem.c_str()); |
1555 | 0 | if (pszProxyValue && |
1556 | 0 | (!pszRealValue || strcmp(pszProxyValue, pszRealValue) != 0)) |
1557 | 0 | { |
1558 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1559 | 0 | "Proxy driver %s declares %s whereas its real driver " |
1560 | 0 | "doesn't declare it or with a different value", |
1561 | 0 | GetDescription(), osItem.c_str()); |
1562 | 0 | } |
1563 | 0 | } |
1564 | 0 | for (const char *pszListedItem : apszProxyMetadataItems) |
1565 | 0 | { |
1566 | 0 | const char *pszRealValue = |
1567 | 0 | m_poRealDriver->GetMetadataItem(pszListedItem); |
1568 | 0 | if (pszRealValue) |
1569 | 0 | { |
1570 | 0 | const char *pszProxyValue = GetMetadataItem(pszListedItem); |
1571 | 0 | if (!pszProxyValue || strcmp(pszProxyValue, pszRealValue) != 0) |
1572 | 0 | { |
1573 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1574 | 0 | "Driver %s declares %s whereas its proxy " |
1575 | 0 | "doesn't declare it or with a different value", |
1576 | 0 | GetDescription(), pszListedItem); |
1577 | 0 | } |
1578 | 0 | } |
1579 | 0 | } |
1580 | |
|
1581 | 0 | const auto CheckFunctionPointer = |
1582 | 0 | [this](void *pfnFuncProxy, void *pfnFuncReal, const char *pszFunc) |
1583 | 0 | { |
1584 | 0 | if (pfnFuncReal && !pfnFuncProxy) |
1585 | 0 | { |
1586 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1587 | 0 | "Driver %s declares a %s callback whereas its proxy " |
1588 | 0 | "does not declare it", |
1589 | 0 | GetDescription(), pszFunc); |
1590 | 0 | } |
1591 | 0 | else if (!pfnFuncReal && pfnFuncProxy) |
1592 | 0 | { |
1593 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1594 | 0 | "Proxy driver %s declares a %s callback whereas the " |
1595 | 0 | "real driver does not.", |
1596 | 0 | GetDescription(), pszFunc); |
1597 | 0 | } |
1598 | 0 | }; |
1599 | |
|
1600 | 0 | CheckFunctionPointer( |
1601 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnIdentify), |
1602 | 0 | reinterpret_cast<void *>(pfnIdentify), "pfnIdentify"); |
1603 | | |
1604 | | // The real driver might provide a more accurate identification method |
1605 | 0 | if (m_poRealDriver->pfnIdentify) |
1606 | 0 | pfnIdentify = m_poRealDriver->pfnIdentify; |
1607 | |
|
1608 | 0 | CheckFunctionPointer( |
1609 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnGetSubdatasetInfoFunc), |
1610 | 0 | reinterpret_cast<void *>(pfnGetSubdatasetInfoFunc), |
1611 | 0 | "pfnGetSubdatasetInfoFunc"); |
1612 | |
|
1613 | 0 | const auto CheckFunctionPointerVersusCap = |
1614 | 0 | [this](void *pfnFunc, const char *pszFunc, const char *pszItemName) |
1615 | 0 | { |
1616 | 0 | if (pfnFunc && !GetMetadataItem(pszItemName)) |
1617 | 0 | { |
1618 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1619 | 0 | "Driver %s declares a %s callback whereas its proxy " |
1620 | 0 | "does not declare %s", |
1621 | 0 | GetDescription(), pszFunc, pszItemName); |
1622 | 0 | } |
1623 | 0 | else if (!pfnFunc && GetMetadataItem(pszItemName)) |
1624 | 0 | { |
1625 | 0 | CPLError(CE_Warning, CPLE_AppDefined, |
1626 | 0 | "Proxy driver %s declares %s whereas the real " |
1627 | 0 | "driver does not declare a %s callback", |
1628 | 0 | GetDescription(), pszItemName, pszFunc); |
1629 | 0 | } |
1630 | 0 | }; |
1631 | |
|
1632 | 0 | CheckFunctionPointerVersusCap( |
1633 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnOpen), "pfnOpen", |
1634 | 0 | GDAL_DCAP_OPEN); |
1635 | 0 | CheckFunctionPointerVersusCap( |
1636 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnCreate), "pfnCreate", |
1637 | 0 | GDAL_DCAP_CREATE); |
1638 | 0 | CheckFunctionPointerVersusCap( |
1639 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnCreateCopy), |
1640 | 0 | "pfnCreateCopy", GDAL_DCAP_CREATECOPY); |
1641 | 0 | CheckFunctionPointerVersusCap( |
1642 | 0 | reinterpret_cast<void *>(m_poRealDriver->pfnCreateMultiDimensional), |
1643 | 0 | "pfnCreateMultiDimensional", GDAL_DCAP_CREATE_MULTIDIMENSIONAL); |
1644 | 0 | } |
1645 | |
|
1646 | 0 | return m_poRealDriver.get(); |
1647 | 0 | } |
1648 | | |
1649 | | /************************************************************************/ |
1650 | | /* GetPluginFullPath() */ |
1651 | | /************************************************************************/ |
1652 | | |
1653 | | std::string GDALDriverManager::GetPluginFullPath(const char *pszFilename) const |
1654 | 0 | { |
1655 | 0 | if (!m_osLastTriedDirectory.empty()) |
1656 | 0 | { |
1657 | 0 | std::string osFullFilename = CPLFormFilenameSafe( |
1658 | 0 | m_osLastTriedDirectory.c_str(), pszFilename, nullptr); |
1659 | 0 | VSIStatBufL sStatBuf; |
1660 | 0 | if (VSIStatL(osFullFilename.c_str(), &sStatBuf) == 0) |
1661 | 0 | { |
1662 | 0 | return osFullFilename; |
1663 | 0 | } |
1664 | 0 | } |
1665 | | |
1666 | 0 | const char *pszGDAL_DRIVER_PATH = |
1667 | 0 | CPLGetConfigOption("GDAL_DRIVER_PATH", nullptr); |
1668 | 0 | if (pszGDAL_DRIVER_PATH == nullptr) |
1669 | 0 | pszGDAL_DRIVER_PATH = CPLGetConfigOption("OGR_DRIVER_PATH", nullptr); |
1670 | | |
1671 | | /* ---------------------------------------------------------------- */ |
1672 | | /* Allow applications to completely disable this search by */ |
1673 | | /* setting the driver path to the special string "disable". */ |
1674 | | /* ---------------------------------------------------------------- */ |
1675 | 0 | if (pszGDAL_DRIVER_PATH != nullptr && EQUAL(pszGDAL_DRIVER_PATH, "disable")) |
1676 | 0 | { |
1677 | 0 | CPLDebug("GDAL", "GDALDriverManager::GetPluginFullPath() disabled."); |
1678 | 0 | return std::string(); |
1679 | 0 | } |
1680 | | |
1681 | | /* ---------------------------------------------------------------- */ |
1682 | | /* Where should we look for stuff? */ |
1683 | | /* ---------------------------------------------------------------- */ |
1684 | 0 | const CPLStringList aosSearchPaths( |
1685 | 0 | GDALDriverManager::GetSearchPaths(pszGDAL_DRIVER_PATH)); |
1686 | | |
1687 | | /* ---------------------------------------------------------------- */ |
1688 | | /* Format the ABI version specific subdirectory to look in. */ |
1689 | | /* ---------------------------------------------------------------- */ |
1690 | 0 | CPLString osABIVersion; |
1691 | |
|
1692 | 0 | osABIVersion.Printf("%d.%d", GDAL_VERSION_MAJOR, GDAL_VERSION_MINOR); |
1693 | | |
1694 | | /* ---------------------------------------------------------------- */ |
1695 | | /* Scan each directory looking for the file of interest. */ |
1696 | | /* ---------------------------------------------------------------- */ |
1697 | 0 | const int nSearchPaths = aosSearchPaths.size(); |
1698 | 0 | for (int iDir = 0; iDir < nSearchPaths; ++iDir) |
1699 | 0 | { |
1700 | 0 | std::string osABISpecificDir = |
1701 | 0 | CPLFormFilenameSafe(aosSearchPaths[iDir], osABIVersion, nullptr); |
1702 | |
|
1703 | 0 | VSIStatBufL sStatBuf; |
1704 | 0 | if (VSIStatL(osABISpecificDir.c_str(), &sStatBuf) != 0) |
1705 | 0 | osABISpecificDir = aosSearchPaths[iDir]; |
1706 | |
|
1707 | 0 | std::string osFullFilename = |
1708 | 0 | CPLFormFilenameSafe(osABISpecificDir.c_str(), pszFilename, nullptr); |
1709 | 0 | if (VSIStatL(osFullFilename.c_str(), &sStatBuf) == 0) |
1710 | 0 | { |
1711 | 0 | m_osLastTriedDirectory = std::move(osABISpecificDir); |
1712 | 0 | return osFullFilename; |
1713 | 0 | } |
1714 | 0 | } |
1715 | | |
1716 | 0 | return std::string(); |
1717 | 0 | } |
1718 | | |
1719 | | /************************************************************************/ |
1720 | | /* DeclareDeferredPluginDriver() */ |
1721 | | /************************************************************************/ |
1722 | | |
1723 | | /** Declare a driver that will be loaded as a plugin, when actually needed. |
1724 | | * |
1725 | | * @param poProxyDriver Plugin driver proxy |
1726 | | * |
1727 | | * @since 3.9 |
1728 | | */ |
1729 | | void GDALDriverManager::DeclareDeferredPluginDriver( |
1730 | | GDALPluginDriverProxy *poProxyDriver) |
1731 | 0 | { |
1732 | 0 | CPLMutexHolderD(&hDMMutex); |
1733 | |
|
1734 | 0 | const auto &osPluginFileName = poProxyDriver->GetPluginFileName(); |
1735 | 0 | const char *pszPluginFileName = osPluginFileName.c_str(); |
1736 | 0 | if ((!STARTS_WITH(pszPluginFileName, "gdal_") && |
1737 | 0 | !STARTS_WITH(pszPluginFileName, "ogr_")) || |
1738 | 0 | !strchr(pszPluginFileName, '.')) |
1739 | 0 | { |
1740 | 0 | CPLError(CE_Failure, CPLE_AppDefined, "Invalid plugin filename: %s", |
1741 | 0 | pszPluginFileName); |
1742 | 0 | return; |
1743 | 0 | } |
1744 | | |
1745 | 0 | if (GDALGetDriverByName(poProxyDriver->GetDescription())) |
1746 | 0 | { |
1747 | 0 | CPLError(CE_Failure, CPLE_AppDefined, |
1748 | 0 | "DeclarePluginDriver(): trying to register %s several times", |
1749 | 0 | poProxyDriver->GetDescription()); |
1750 | 0 | delete poProxyDriver; |
1751 | 0 | return; |
1752 | 0 | } |
1753 | | |
1754 | 0 | const std::string osFullPath = GetPluginFullPath(pszPluginFileName); |
1755 | 0 | poProxyDriver->SetPluginFullPath(osFullPath); |
1756 | |
|
1757 | 0 | if (osFullPath.empty()) |
1758 | 0 | { |
1759 | | // Do not try to re-register a non-existent deferred plugin |
1760 | | // This would cause memory leaks in case of repeated calls to GDALAllRegister() |
1761 | | // Cf https://github.com/rasterio/rasterio/issues/3250 |
1762 | 0 | for (const auto &poDriver : m_aoHiddenDrivers) |
1763 | 0 | { |
1764 | 0 | if (EQUAL(poDriver->GetDescription(), |
1765 | 0 | poProxyDriver->GetDescription())) |
1766 | 0 | { |
1767 | 0 | delete poProxyDriver; |
1768 | 0 | return; |
1769 | 0 | } |
1770 | 0 | } |
1771 | | |
1772 | 0 | CPLDebug("GDAL", |
1773 | 0 | "Proxy driver %s *not* registered due to %s not being found", |
1774 | 0 | poProxyDriver->GetDescription(), pszPluginFileName); |
1775 | 0 | RegisterDriver(poProxyDriver, /*bHidden=*/true); |
1776 | 0 | } |
1777 | 0 | else |
1778 | 0 | { |
1779 | | //CPLDebugOnly("GDAL", "Registering proxy driver %s", |
1780 | | // poProxyDriver->GetDescription()); |
1781 | 0 | RegisterDriver(poProxyDriver); |
1782 | 0 | m_oSetPluginFileNames.insert(pszPluginFileName); |
1783 | 0 | } |
1784 | 0 | } |
1785 | | |
1786 | | /************************************************************************/ |
1787 | | /* GDALDestroyDriverManager() */ |
1788 | | /************************************************************************/ |
1789 | | |
1790 | | /** |
1791 | | * \brief Destroy the driver manager. |
1792 | | * |
1793 | | * Incidentally unloads all managed drivers. |
1794 | | * |
1795 | | * NOTE: This function is not thread safe. It should not be called while |
1796 | | * other threads are actively using GDAL. |
1797 | | * |
1798 | | * \see GDALDestroy() |
1799 | | * \deprecated Use GDALDestroy() instead |
1800 | | */ |
1801 | | |
1802 | | void CPL_STDCALL GDALDestroyDriverManager(void) |
1803 | | |
1804 | 0 | { |
1805 | | // THREADSAFETY: We would like to lock the mutex here, but it |
1806 | | // needs to be reacquired within the destructor during driver |
1807 | | // deregistration. |
1808 | | |
1809 | | // FIXME: Disable following code as it crashed on OSX CI test. |
1810 | | // std::lock_guard<std::mutex> oLock(oDeleteMutex); |
1811 | |
|
1812 | 0 | if (poDM != nullptr) |
1813 | 0 | { |
1814 | 0 | delete poDM; |
1815 | 0 | poDM = nullptr; |
1816 | 0 | } |
1817 | 0 | } |