Line data Source code
1 : /******************************************************************************
2 : *
3 : * Project: GDAL Core
4 : * Purpose: Implementation of GDALDriver class (and C wrappers)
5 : * Author: Frank Warmerdam, warmerdam@pobox.com
6 : *
7 : ******************************************************************************
8 : * Copyright (c) 1998, 2000, Frank Warmerdam
9 : * Copyright (c) 2007-2014, Even Rouault <even dot rouault at spatialys.com>
10 : *
11 : * SPDX-License-Identifier: MIT
12 : ****************************************************************************/
13 :
14 : #include "cpl_port.h"
15 : #include "gdal.h"
16 : #include "gdal_priv.h"
17 : #include "gdal_rat.h"
18 : #include "gdalalgorithm.h"
19 : #include "gdal_known_connection_prefixes.h"
20 :
21 : #include <algorithm>
22 : #include <cerrno>
23 : #include <cstdlib>
24 : #include <cstring>
25 : #include <set>
26 : #include <sys/stat.h>
27 :
28 : #include "cpl_conv.h"
29 : #include "cpl_error.h"
30 : #include "cpl_minixml.h"
31 : #include "cpl_multiproc.h"
32 : #include "cpl_progress.h"
33 : #include "cpl_string.h"
34 : #include "cpl_vsi.h"
35 : #include "ograpispy.h"
36 : #include "ogr_core.h"
37 : #include "ogrsf_frmts.h"
38 :
39 : /************************************************************************/
40 : /* GDALDriver() */
41 : /************************************************************************/
42 :
43 : GDALDriver::GDALDriver() = default;
44 :
45 : /************************************************************************/
46 : /* ~GDALDriver() */
47 : /************************************************************************/
48 :
49 450830 : GDALDriver::~GDALDriver()
50 :
51 : {
52 254821 : if (pfnUnloadDriver != nullptr)
53 6921 : pfnUnloadDriver(this);
54 450830 : }
55 :
56 : /************************************************************************/
57 : /* GDALCreateDriver() */
58 : /************************************************************************/
59 :
60 : /**
61 : * \brief Create a GDALDriver.
62 : *
63 : * Creates a driver in the GDAL heap.
64 : */
65 :
66 225 : GDALDriverH CPL_STDCALL GDALCreateDriver()
67 : {
68 225 : return new GDALDriver();
69 : }
70 :
71 : /************************************************************************/
72 : /* GDALDestroyDriver() */
73 : /************************************************************************/
74 :
75 : /**
76 : * \brief Destroy a GDALDriver.
77 : *
78 : * This is roughly equivalent to deleting the driver, but is guaranteed
79 : * to take place in the GDAL heap. It is important this that function
80 : * not be called on a driver that is registered with the GDALDriverManager.
81 : *
82 : * @param hDriver the driver to destroy.
83 : */
84 :
85 0 : void CPL_STDCALL GDALDestroyDriver(GDALDriverH hDriver)
86 :
87 : {
88 0 : if (hDriver != nullptr)
89 0 : delete GDALDriver::FromHandle(hDriver);
90 0 : }
91 :
92 : /************************************************************************/
93 : /* Open() */
94 : /************************************************************************/
95 :
96 : //! @cond Doxygen_Suppress
97 :
98 498790 : GDALDataset *GDALDriver::Open(GDALOpenInfo *poOpenInfo, bool bSetOpenOptions)
99 : {
100 :
101 498790 : GDALDataset *poDS = nullptr;
102 498790 : pfnOpen = GetOpenCallback();
103 498790 : if (pfnOpen != nullptr)
104 : {
105 498787 : poDS = pfnOpen(poOpenInfo);
106 : }
107 3 : else if (pfnOpenWithDriverArg != nullptr)
108 : {
109 3 : poDS = pfnOpenWithDriverArg(this, poOpenInfo);
110 : }
111 :
112 498790 : if (poDS)
113 : {
114 : // Only set GDAL_OF_THREAD_SAFE if the driver itself has set it in
115 : // poDS->nOpenFlags
116 62176 : int nOpenFlags = poOpenInfo->nOpenFlags &
117 : ~(GDAL_OF_FROM_GDALOPEN | GDAL_OF_THREAD_SAFE);
118 62176 : if (poDS->nOpenFlags & GDAL_OF_THREAD_SAFE)
119 914 : nOpenFlags |= GDAL_OF_THREAD_SAFE;
120 62176 : poDS->nOpenFlags = nOpenFlags;
121 :
122 62176 : if (strlen(poDS->GetDescription()) == 0)
123 14120 : poDS->SetDescription(poOpenInfo->pszFilename);
124 :
125 62176 : if (poDS->poDriver == nullptr)
126 58139 : poDS->poDriver = this;
127 :
128 62176 : if (poDS->papszOpenOptions == nullptr && bSetOpenOptions)
129 : {
130 29 : poDS->papszOpenOptions = CSLDuplicate(poOpenInfo->papszOpenOptions);
131 : }
132 :
133 62176 : if (!(poOpenInfo->nOpenFlags & GDAL_OF_INTERNAL))
134 : {
135 49752 : if (CPLGetPID() != GDALGetResponsiblePIDForCurrentThread())
136 6 : CPLDebug(
137 : "GDAL",
138 : "GDALOpen(%s, this=%p) succeeds as "
139 : "%s (pid=%d, responsiblePID=%d).",
140 6 : poOpenInfo->pszFilename, poDS, GetDescription(),
141 6 : static_cast<int>(CPLGetPID()),
142 6 : static_cast<int>(GDALGetResponsiblePIDForCurrentThread()));
143 : else
144 49746 : CPLDebug("GDAL", "GDALOpen(%s, this=%p) succeeds as %s.",
145 49746 : poOpenInfo->pszFilename, poDS, GetDescription());
146 :
147 49752 : poDS->AddToDatasetOpenList();
148 : }
149 : }
150 :
151 498790 : return poDS;
152 : }
153 :
154 : //! @endcond
155 :
156 : /************************************************************************/
157 : /* Create() */
158 : /************************************************************************/
159 :
160 : /**
161 : * \brief Create a new dataset with this driver.
162 : *
163 : * What argument values are legal for particular drivers is driver specific,
164 : * and there is no way to query in advance to establish legal values.
165 : *
166 : * That function will try to validate the creation option list passed to the
167 : * driver with the GDALValidateCreationOptions() method. This check can be
168 : * disabled by defining the configuration option
169 : * GDAL_VALIDATE_CREATION_OPTIONS=NO.
170 : *
171 : * After you have finished working with the returned dataset, it is
172 : * <b>required</b> to close it with GDALClose(). This does not only close the
173 : * file handle, but also ensures that all the data and metadata has been written
174 : * to the dataset (GDALFlushCache() is not sufficient for that purpose).
175 : *
176 : * The arguments nXSize, nYSize and nBands can be passed to 0 when
177 : * creating a vector-only dataset for a compatible driver.
178 : *
179 : * Equivalent of the C function GDALCreate().
180 : *
181 : * @param pszFilename the name of the dataset to create. UTF-8 encoded.
182 : * @param nXSize width of created raster in pixels.
183 : * @param nYSize height of created raster in pixels.
184 : * @param nBands number of bands.
185 : * @param eType type of raster.
186 : * @param papszOptions list of driver specific control parameters.
187 : * The APPEND_SUBDATASET=YES option can be
188 : * specified to avoid prior destruction of existing dataset.
189 : *
190 : * @return NULL on failure, or a new GDALDataset.
191 : */
192 :
193 25318 : GDALDataset *GDALDriver::Create(const char *pszFilename, int nXSize, int nYSize,
194 : int nBands, GDALDataType eType,
195 : CSLConstList papszOptions)
196 :
197 : {
198 : /* -------------------------------------------------------------------- */
199 : /* Does this format support creation. */
200 : /* -------------------------------------------------------------------- */
201 25318 : pfnCreate = GetCreateCallback();
202 25318 : if (CPL_UNLIKELY(pfnCreate == nullptr && pfnCreateEx == nullptr &&
203 : pfnCreateVectorOnly == nullptr))
204 : {
205 1 : CPLError(CE_Failure, CPLE_NotSupported,
206 : "GDALDriver::Create() ... no create method implemented"
207 : " for this format.");
208 :
209 1 : return nullptr;
210 : }
211 : /* -------------------------------------------------------------------- */
212 : /* Do some rudimentary argument checking. */
213 : /* -------------------------------------------------------------------- */
214 25317 : if (CPL_UNLIKELY(nBands < 0))
215 : {
216 1 : CPLError(CE_Failure, CPLE_AppDefined,
217 : "Attempt to create dataset with %d bands is illegal,"
218 : "Must be >= 0.",
219 : nBands);
220 1 : return nullptr;
221 : }
222 :
223 25316 : if (CPL_UNLIKELY(GetMetadataItem(GDAL_DCAP_RASTER) != nullptr &&
224 : GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr &&
225 25316 : (nXSize < 1 || nYSize < 1)))
226 : {
227 2 : CPLError(CE_Failure, CPLE_AppDefined,
228 : "Attempt to create %dx%d dataset is illegal,"
229 : "sizes must be larger than zero.",
230 : nXSize, nYSize);
231 2 : return nullptr;
232 : }
233 :
234 25314 : if (CPL_UNLIKELY(nBands != 0 &&
235 : (eType == GDT_Unknown || eType == GDT_TypeCount)))
236 : {
237 1 : CPLError(CE_Failure, CPLE_IllegalArg,
238 : "Illegal GDT_Unknown/GDT_TypeCount argument");
239 1 : return nullptr;
240 : }
241 :
242 : /* -------------------------------------------------------------------- */
243 : /* Make sure we cleanup if there is an existing dataset of this */
244 : /* name. But even if that seems to fail we will continue since */
245 : /* it might just be a corrupt file or something. */
246 : /* -------------------------------------------------------------------- */
247 25313 : if (!CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false))
248 : {
249 : // Someone issuing Create("foo.tif") on a
250 : // memory driver doesn't expect files with those names to be deleted
251 : // on a file system...
252 : // This is somewhat messy. Ideally there should be a way for the
253 : // driver to overload the default behavior
254 25294 : if (!EQUAL(GetDescription(), "MEM") &&
255 38825 : !EQUAL(GetDescription(), "Memory") &&
256 : // ogr2ogr -f PostgreSQL might reach the Delete method of the
257 : // PostgisRaster driver which is undesirable
258 13531 : !EQUAL(GetDescription(), "PostgreSQL"))
259 : {
260 13529 : QuietDelete(pszFilename);
261 : }
262 : }
263 :
264 : /* -------------------------------------------------------------------- */
265 : /* Validate creation options. */
266 : /* -------------------------------------------------------------------- */
267 25313 : if (CPLTestBool(
268 : CPLGetConfigOption("GDAL_VALIDATE_CREATION_OPTIONS", "YES")))
269 25313 : GDALValidateCreationOptions(this, papszOptions);
270 :
271 : /* -------------------------------------------------------------------- */
272 : /* Proceed with creation. */
273 : /* -------------------------------------------------------------------- */
274 50626 : CPLDebug("GDAL", "GDALDriver::Create(%s,%s,%d,%d,%d,%s,%p)",
275 25313 : GetDescription(), pszFilename, nXSize, nYSize, nBands,
276 : GDALGetDataTypeName(eType), papszOptions);
277 :
278 25313 : GDALDataset *poDS = nullptr;
279 25313 : if (pfnCreateEx != nullptr)
280 : {
281 0 : poDS = pfnCreateEx(this, pszFilename, nXSize, nYSize, nBands, eType,
282 : const_cast<char **>(papszOptions));
283 : }
284 25313 : else if (pfnCreate != nullptr)
285 : {
286 25313 : poDS = pfnCreate(pszFilename, nXSize, nYSize, nBands, eType,
287 : const_cast<char **>(papszOptions));
288 : }
289 0 : else if (nBands < 1)
290 : {
291 0 : poDS = pfnCreateVectorOnly(this, pszFilename,
292 : const_cast<char **>(papszOptions));
293 : }
294 :
295 25313 : if (poDS != nullptr)
296 : {
297 48026 : if (poDS->GetDescription() == nullptr ||
298 24013 : strlen(poDS->GetDescription()) == 0)
299 21335 : poDS->SetDescription(pszFilename);
300 :
301 24013 : if (poDS->poDriver == nullptr)
302 23278 : poDS->poDriver = this;
303 :
304 24013 : poDS->AddToDatasetOpenList();
305 : }
306 :
307 25313 : return poDS;
308 : }
309 :
310 : /************************************************************************/
311 : /* GDALCreate() */
312 : /************************************************************************/
313 :
314 : /**
315 : * \brief Create a new dataset with this driver.
316 : *
317 : * @see GDALDriver::Create()
318 : */
319 :
320 20932 : GDALDatasetH CPL_DLL CPL_STDCALL GDALCreate(GDALDriverH hDriver,
321 : const char *pszFilename, int nXSize,
322 : int nYSize, int nBands,
323 : GDALDataType eBandType,
324 : CSLConstList papszOptions)
325 :
326 : {
327 20932 : VALIDATE_POINTER1(hDriver, "GDALCreate", nullptr);
328 :
329 20932 : GDALDatasetH hDS = GDALDriver::FromHandle(hDriver)->Create(
330 : pszFilename, nXSize, nYSize, nBands, eBandType, papszOptions);
331 :
332 : #ifdef OGRAPISPY_ENABLED
333 20932 : if (nBands < 1)
334 : {
335 3832 : OGRAPISpyCreateDataSource(hDriver, pszFilename,
336 : const_cast<char **>(papszOptions), hDS);
337 : }
338 : #endif
339 :
340 20932 : return hDS;
341 : }
342 :
343 : /************************************************************************/
344 : /* CreateMultiDimensional() */
345 : /************************************************************************/
346 :
347 : /**
348 : * \brief Create a new multidimensional dataset with this driver.
349 : *
350 : * Only drivers that advertise the GDAL_DCAP_MULTIDIM_RASTER capability and
351 : * implement the pfnCreateMultiDimensional method might return a non nullptr
352 : * GDALDataset.
353 : *
354 : * This is the same as the C function GDALCreateMultiDimensional().
355 : *
356 : * @param pszFilename the name of the dataset to create. UTF-8 encoded.
357 : * @param papszRootGroupOptions driver specific options regarding the creation
358 : * of the root group. Might be nullptr.
359 : * @param papszOptions driver specific options regarding the creation
360 : * of the dataset. Might be nullptr.
361 : * @return a new dataset, or nullptr in case of failure.
362 : *
363 : * @since GDAL 3.1
364 : */
365 :
366 : GDALDataset *
367 562 : GDALDriver::CreateMultiDimensional(const char *pszFilename,
368 : CSLConstList papszRootGroupOptions,
369 : CSLConstList papszOptions)
370 :
371 : {
372 : /* -------------------------------------------------------------------- */
373 : /* Does this format support creation. */
374 : /* -------------------------------------------------------------------- */
375 562 : pfnCreateMultiDimensional = GetCreateMultiDimensionalCallback();
376 562 : if (pfnCreateMultiDimensional == nullptr)
377 : {
378 0 : CPLError(CE_Failure, CPLE_NotSupported,
379 : "GDALDriver::CreateMultiDimensional() ... "
380 : "no CreateMultiDimensional method implemented "
381 : "for this format.");
382 :
383 0 : return nullptr;
384 : }
385 :
386 : /* -------------------------------------------------------------------- */
387 : /* Validate creation options. */
388 : /* -------------------------------------------------------------------- */
389 562 : if (CPLTestBool(
390 : CPLGetConfigOption("GDAL_VALIDATE_CREATION_OPTIONS", "YES")))
391 : {
392 : const char *pszOptionList =
393 562 : GetMetadataItem(GDAL_DMD_MULTIDIM_DATASET_CREATIONOPTIONLIST);
394 1124 : CPLString osDriver;
395 562 : osDriver.Printf("driver %s", GetDescription());
396 562 : GDALValidateOptions(pszOptionList, papszOptions, "creation option",
397 : osDriver);
398 : }
399 :
400 562 : auto poDstDS = pfnCreateMultiDimensional(pszFilename, papszRootGroupOptions,
401 : papszOptions);
402 :
403 562 : if (poDstDS != nullptr)
404 : {
405 1120 : if (poDstDS->GetDescription() == nullptr ||
406 560 : strlen(poDstDS->GetDescription()) == 0)
407 147 : poDstDS->SetDescription(pszFilename);
408 :
409 560 : if (poDstDS->poDriver == nullptr)
410 558 : poDstDS->poDriver = this;
411 : }
412 :
413 562 : return poDstDS;
414 : }
415 :
416 : /************************************************************************/
417 : /* GDALCreateMultiDimensional() */
418 : /************************************************************************/
419 :
420 : /** \brief Create a new multidimensional dataset with this driver.
421 : *
422 : * This is the same as the C++ method GDALDriver::CreateMultiDimensional().
423 : */
424 510 : GDALDatasetH GDALCreateMultiDimensional(GDALDriverH hDriver,
425 : const char *pszName,
426 : CSLConstList papszRootGroupOptions,
427 : CSLConstList papszOptions)
428 : {
429 510 : VALIDATE_POINTER1(hDriver, __func__, nullptr);
430 510 : VALIDATE_POINTER1(pszName, __func__, nullptr);
431 510 : return GDALDataset::ToHandle(
432 : GDALDriver::FromHandle(hDriver)->CreateMultiDimensional(
433 510 : pszName, papszRootGroupOptions, papszOptions));
434 : }
435 :
436 : /************************************************************************/
437 : /* DefaultCreateCopyMultiDimensional() */
438 : /************************************************************************/
439 :
440 : //! @cond Doxygen_Suppress
441 :
442 27 : CPLErr GDALDriver::DefaultCreateCopyMultiDimensional(
443 : GDALDataset *poSrcDS, GDALDataset *poDstDS, bool bStrict,
444 : CSLConstList papszOptions, GDALProgressFunc pfnProgress,
445 : void *pProgressData)
446 : {
447 27 : if (pfnProgress == nullptr)
448 3 : pfnProgress = GDALDummyProgress;
449 :
450 54 : auto poSrcRG = poSrcDS->GetRootGroup();
451 27 : if (!poSrcRG)
452 0 : return CE_Failure;
453 54 : auto poDstRG = poDstDS->GetRootGroup();
454 27 : if (!poDstRG)
455 0 : return CE_Failure;
456 27 : GUInt64 nCurCost = 0;
457 54 : return poDstRG->CopyFrom(poDstRG, poSrcDS, poSrcRG, bStrict, nCurCost,
458 : poSrcRG->GetTotalCopyCost(), pfnProgress,
459 27 : pProgressData, papszOptions)
460 27 : ? CE_None
461 27 : : CE_Failure;
462 : }
463 :
464 : //! @endcond
465 :
466 : /************************************************************************/
467 : /* DefaultCopyMasks() */
468 : /************************************************************************/
469 :
470 : //! @cond Doxygen_Suppress
471 6260 : CPLErr GDALDriver::DefaultCopyMasks(GDALDataset *poSrcDS, GDALDataset *poDstDS,
472 : int bStrict)
473 :
474 : {
475 6260 : return DefaultCopyMasks(poSrcDS, poDstDS, bStrict, nullptr, nullptr,
476 6260 : nullptr);
477 : }
478 :
479 8314 : CPLErr GDALDriver::DefaultCopyMasks(GDALDataset *poSrcDS, GDALDataset *poDstDS,
480 : int bStrict, CSLConstList /*papszOptions*/,
481 : GDALProgressFunc pfnProgress,
482 : void *pProgressData)
483 :
484 : {
485 8314 : if (pfnProgress == nullptr)
486 6260 : pfnProgress = GDALDummyProgress;
487 :
488 8314 : int nBands = poSrcDS->GetRasterCount();
489 8314 : if (nBands == 0)
490 0 : return CE_None;
491 :
492 : /* -------------------------------------------------------------------- */
493 : /* Try to copy mask if it seems appropriate. */
494 : /* -------------------------------------------------------------------- */
495 8314 : const char *papszOptions[2] = {"COMPRESSED=YES", nullptr};
496 8314 : CPLErr eErr = CE_None;
497 :
498 8314 : int nTotalBandsWithMask = 0;
499 28776 : for (int iBand = 0; iBand < nBands; ++iBand)
500 : {
501 20462 : GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(iBand + 1);
502 :
503 20462 : int nMaskFlags = poSrcBand->GetMaskFlags();
504 20462 : if (!(nMaskFlags &
505 : (GMF_ALL_VALID | GMF_PER_DATASET | GMF_ALPHA | GMF_NODATA)))
506 : {
507 8 : nTotalBandsWithMask++;
508 : }
509 : }
510 :
511 8314 : int iBandWithMask = 0;
512 28776 : for (int iBand = 0; eErr == CE_None && iBand < nBands; ++iBand)
513 : {
514 20462 : GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(iBand + 1);
515 :
516 20462 : int nMaskFlags = poSrcBand->GetMaskFlags();
517 20462 : if (eErr == CE_None && !(nMaskFlags & (GMF_ALL_VALID | GMF_PER_DATASET |
518 : GMF_ALPHA | GMF_NODATA)))
519 : {
520 8 : GDALRasterBand *poDstBand = poDstDS->GetRasterBand(iBand + 1);
521 8 : if (poDstBand != nullptr)
522 : {
523 8 : eErr = poDstBand->CreateMaskBand(nMaskFlags);
524 8 : if (eErr == CE_None)
525 : {
526 24 : void *pScaledData = GDALCreateScaledProgress(
527 8 : double(iBandWithMask) /
528 8 : std::max(1, nTotalBandsWithMask),
529 8 : double(iBandWithMask + 1) /
530 8 : std::max(1, nTotalBandsWithMask),
531 : pfnProgress, pProgressData);
532 16 : eErr = GDALRasterBandCopyWholeRaster(
533 8 : poSrcBand->GetMaskBand(), poDstBand->GetMaskBand(),
534 : papszOptions, GDALScaledProgress, pScaledData);
535 8 : GDALDestroyScaledProgress(pScaledData);
536 : }
537 0 : else if (!bStrict)
538 : {
539 0 : eErr = CE_None;
540 : }
541 : }
542 : }
543 : }
544 :
545 : /* -------------------------------------------------------------------- */
546 : /* Try to copy a per-dataset mask if we have one. */
547 : /* -------------------------------------------------------------------- */
548 8314 : const int nMaskFlags = poSrcDS->GetRasterBand(1)->GetMaskFlags();
549 8314 : if (eErr == CE_None &&
550 8314 : !(nMaskFlags & (GMF_ALL_VALID | GMF_ALPHA | GMF_NODATA)) &&
551 13 : (nMaskFlags & GMF_PER_DATASET))
552 : {
553 8 : eErr = poDstDS->CreateMaskBand(nMaskFlags);
554 8 : if (eErr == CE_None)
555 : {
556 8 : eErr = GDALRasterBandCopyWholeRaster(
557 8 : poSrcDS->GetRasterBand(1)->GetMaskBand(),
558 8 : poDstDS->GetRasterBand(1)->GetMaskBand(), papszOptions,
559 : pfnProgress, pProgressData);
560 : }
561 0 : else if (!bStrict)
562 : {
563 0 : eErr = CE_None;
564 : }
565 : }
566 :
567 8314 : return eErr;
568 : }
569 :
570 : /************************************************************************/
571 : /* DefaultCreateCopy() */
572 : /************************************************************************/
573 :
574 1484 : GDALDataset *GDALDriver::DefaultCreateCopy(const char *pszFilename,
575 : GDALDataset *poSrcDS, int bStrict,
576 : CSLConstList papszOptions,
577 : GDALProgressFunc pfnProgress,
578 : void *pProgressData)
579 :
580 : {
581 1484 : if (pfnProgress == nullptr)
582 0 : pfnProgress = GDALDummyProgress;
583 :
584 1484 : CPLErrorReset();
585 :
586 : /* -------------------------------------------------------------------- */
587 : /* Use multidimensional raster API if available. */
588 : /* -------------------------------------------------------------------- */
589 2968 : auto poSrcGroup = poSrcDS->GetRootGroup();
590 1484 : if (poSrcGroup != nullptr && GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER))
591 : {
592 48 : CPLStringList aosDatasetCO;
593 31 : for (const char *pszOption : cpl::Iterate(papszOptions))
594 : {
595 7 : if (!STARTS_WITH_CI(pszOption, "ARRAY:"))
596 0 : aosDatasetCO.AddString(pszOption);
597 : }
598 : auto poDstDS = std::unique_ptr<GDALDataset>(
599 48 : CreateMultiDimensional(pszFilename, nullptr, aosDatasetCO.List()));
600 24 : if (!poDstDS)
601 0 : return nullptr;
602 48 : auto poDstGroup = poDstDS->GetRootGroup();
603 24 : if (!poDstGroup)
604 0 : return nullptr;
605 24 : if (DefaultCreateCopyMultiDimensional(
606 24 : poSrcDS, poDstDS.get(), CPL_TO_BOOL(bStrict), papszOptions,
607 24 : pfnProgress, pProgressData) != CE_None)
608 0 : return nullptr;
609 24 : return poDstDS.release();
610 : }
611 :
612 : /* -------------------------------------------------------------------- */
613 : /* Validate that we can create the output as requested. */
614 : /* -------------------------------------------------------------------- */
615 1460 : const int nXSize = poSrcDS->GetRasterXSize();
616 1460 : const int nYSize = poSrcDS->GetRasterYSize();
617 1460 : const int nBands = poSrcDS->GetRasterCount();
618 :
619 1460 : CPLDebug("GDAL", "Using default GDALDriver::CreateCopy implementation.");
620 :
621 1460 : const int nLayerCount = poSrcDS->GetLayerCount();
622 1484 : if (nBands == 0 && nLayerCount == 0 &&
623 24 : GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr)
624 : {
625 17 : CPLError(CE_Failure, CPLE_NotSupported,
626 : "GDALDriver::DefaultCreateCopy does not support zero band");
627 17 : return nullptr;
628 : }
629 1443 : if (poSrcDS->GetDriver() != nullptr &&
630 1413 : poSrcDS->GetDriver()->GetMetadataItem(GDAL_DCAP_RASTER) != nullptr &&
631 1406 : poSrcDS->GetDriver()->GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr &&
632 2859 : GetMetadataItem(GDAL_DCAP_RASTER) == nullptr &&
633 3 : GetMetadataItem(GDAL_DCAP_VECTOR) != nullptr)
634 : {
635 3 : CPLError(CE_Failure, CPLE_NotSupported,
636 : "Source driver is raster-only whereas output driver is "
637 : "vector-only");
638 3 : return nullptr;
639 : }
640 1440 : else if (poSrcDS->GetDriver() != nullptr &&
641 1410 : poSrcDS->GetDriver()->GetMetadataItem(GDAL_DCAP_RASTER) ==
642 7 : nullptr &&
643 7 : poSrcDS->GetDriver()->GetMetadataItem(GDAL_DCAP_VECTOR) !=
644 7 : nullptr &&
645 2850 : GetMetadataItem(GDAL_DCAP_RASTER) != nullptr &&
646 0 : GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr)
647 : {
648 0 : CPLError(CE_Failure, CPLE_NotSupported,
649 : "Source driver is vector-only whereas output driver is "
650 : "raster-only");
651 0 : return nullptr;
652 : }
653 :
654 1440 : if (!pfnProgress(0.0, nullptr, pProgressData))
655 : {
656 2 : CPLError(CE_Failure, CPLE_UserInterrupt, "User terminated");
657 2 : return nullptr;
658 : }
659 :
660 : /* -------------------------------------------------------------------- */
661 : /* Propagate some specific structural metadata as options if it */
662 : /* appears to be supported by the target driver and the caller */
663 : /* didn't provide values. */
664 : /* -------------------------------------------------------------------- */
665 1438 : char **papszCreateOptions = CSLDuplicate(papszOptions);
666 1438 : const char *const apszOptItems[] = {"NBITS", "IMAGE_STRUCTURE", "PIXELTYPE",
667 : "IMAGE_STRUCTURE", nullptr};
668 :
669 4282 : for (int iOptItem = 0; nBands > 0 && apszOptItems[iOptItem] != nullptr;
670 2844 : iOptItem += 2)
671 : {
672 : // does the source have this metadata item on the first band?
673 2844 : auto poBand = poSrcDS->GetRasterBand(1);
674 2844 : poBand->EnablePixelTypeSignedByteWarning(false);
675 5688 : const char *pszValue = poBand->GetMetadataItem(
676 2844 : apszOptItems[iOptItem], apszOptItems[iOptItem + 1]);
677 2844 : poBand->EnablePixelTypeSignedByteWarning(true);
678 :
679 2844 : if (pszValue == nullptr)
680 2842 : continue;
681 :
682 : // Do not override provided value.
683 2 : if (CSLFetchNameValue(papszCreateOptions, pszValue) != nullptr)
684 0 : continue;
685 :
686 : // Does this appear to be a supported creation option on this driver?
687 : const char *pszOptionList =
688 2 : GetMetadataItem(GDAL_DMD_CREATIONOPTIONLIST);
689 :
690 2 : if (pszOptionList == nullptr ||
691 2 : strstr(pszOptionList, apszOptItems[iOptItem]) == nullptr)
692 0 : continue;
693 :
694 2 : papszCreateOptions = CSLSetNameValue(papszCreateOptions,
695 2 : apszOptItems[iOptItem], pszValue);
696 : }
697 :
698 : /* -------------------------------------------------------------------- */
699 : /* Create destination dataset. */
700 : /* -------------------------------------------------------------------- */
701 1438 : GDALDataType eType = GDT_Unknown;
702 :
703 1438 : if (nBands > 0)
704 1422 : eType = poSrcDS->GetRasterBand(1)->GetRasterDataType();
705 : GDALDataset *poDstDS =
706 1438 : Create(pszFilename, nXSize, nYSize, nBands, eType, papszCreateOptions);
707 :
708 1438 : CSLDestroy(papszCreateOptions);
709 :
710 1438 : if (poDstDS == nullptr)
711 326 : return nullptr;
712 :
713 1112 : int nDstBands = poDstDS->GetRasterCount();
714 1112 : CPLErr eErr = CE_None;
715 1112 : if (nDstBands != nBands)
716 : {
717 0 : if (GetMetadataItem(GDAL_DCAP_RASTER) != nullptr)
718 : {
719 : // Should not happen for a well-behaved driver.
720 0 : CPLError(
721 : CE_Failure, CPLE_AppDefined,
722 : "Output driver created only %d bands whereas %d were expected",
723 : nDstBands, nBands);
724 0 : eErr = CE_Failure;
725 : }
726 0 : nDstBands = 0;
727 : }
728 :
729 : /* -------------------------------------------------------------------- */
730 : /* Try setting the projection and geotransform if it seems */
731 : /* suitable. */
732 : /* -------------------------------------------------------------------- */
733 1112 : if (nDstBands == 0 && !bStrict)
734 4 : CPLTurnFailureIntoWarning(true);
735 :
736 1112 : GDALGeoTransform gt;
737 2093 : if (eErr == CE_None && poSrcDS->GetGeoTransform(gt) == CE_None &&
738 2093 : gt != GDALGeoTransform())
739 : {
740 979 : eErr = poDstDS->SetGeoTransform(gt);
741 979 : if (!bStrict)
742 622 : eErr = CE_None;
743 : }
744 :
745 1112 : if (eErr == CE_None)
746 : {
747 1103 : const auto poSrcSRS = poSrcDS->GetSpatialRefRasterOnly();
748 1103 : if (poSrcSRS && !poSrcSRS->IsEmpty())
749 : {
750 917 : eErr = poDstDS->SetSpatialRef(poSrcSRS);
751 917 : if (!bStrict)
752 592 : eErr = CE_None;
753 : }
754 : }
755 :
756 : /* -------------------------------------------------------------------- */
757 : /* Copy GCPs. */
758 : /* -------------------------------------------------------------------- */
759 1112 : if (poSrcDS->GetGCPCount() > 0 && eErr == CE_None)
760 : {
761 2 : eErr = poDstDS->SetGCPs(poSrcDS->GetGCPCount(), poSrcDS->GetGCPs(),
762 : poSrcDS->GetGCPProjection());
763 2 : if (!bStrict)
764 1 : eErr = CE_None;
765 : }
766 :
767 1112 : if (nDstBands == 0 && !bStrict)
768 4 : CPLTurnFailureIntoWarning(false);
769 :
770 : /* -------------------------------------------------------------------- */
771 : /* Copy metadata. */
772 : /* -------------------------------------------------------------------- */
773 1112 : DefaultCopyMetadata(poSrcDS, poDstDS, papszOptions, nullptr);
774 :
775 : /* -------------------------------------------------------------------- */
776 : /* Loop copying bands. */
777 : /* -------------------------------------------------------------------- */
778 2888 : for (int iBand = 0; eErr == CE_None && iBand < nDstBands; ++iBand)
779 : {
780 1776 : GDALRasterBand *const poSrcBand = poSrcDS->GetRasterBand(iBand + 1);
781 1776 : GDALRasterBand *const poDstBand = poDstDS->GetRasterBand(iBand + 1);
782 :
783 : /* --------------------------------------------------------------------
784 : */
785 : /* Do we need to copy a colortable. */
786 : /* --------------------------------------------------------------------
787 : */
788 1776 : GDALColorTable *const poCT = poSrcBand->GetColorTable();
789 1776 : if (poCT != nullptr)
790 39 : poDstBand->SetColorTable(poCT);
791 :
792 : /* --------------------------------------------------------------------
793 : */
794 : /* Do we need to copy other metadata? Most of this is */
795 : /* non-critical, so lets not bother folks if it fails are we */
796 : /* are not in strict mode. */
797 : /* --------------------------------------------------------------------
798 : */
799 1776 : if (!bStrict)
800 1142 : CPLTurnFailureIntoWarning(true);
801 :
802 1776 : if (strlen(poSrcBand->GetDescription()) > 0)
803 52 : poDstBand->SetDescription(poSrcBand->GetDescription());
804 :
805 1776 : if (CSLCount(poSrcBand->GetMetadata()) > 0)
806 114 : poDstBand->SetMetadata(poSrcBand->GetMetadata());
807 :
808 1776 : int bSuccess = FALSE;
809 1776 : double dfValue = poSrcBand->GetOffset(&bSuccess);
810 1776 : if (bSuccess && dfValue != 0.0)
811 5 : poDstBand->SetOffset(dfValue);
812 :
813 1776 : dfValue = poSrcBand->GetScale(&bSuccess);
814 1776 : if (bSuccess && dfValue != 1.0)
815 4 : poDstBand->SetScale(dfValue);
816 :
817 1776 : GDALCopyNoDataValue(poDstBand, poSrcBand);
818 :
819 2958 : if (poSrcBand->GetColorInterpretation() != GCI_Undefined &&
820 1182 : poSrcBand->GetColorInterpretation() !=
821 1182 : poDstBand->GetColorInterpretation())
822 955 : poDstBand->SetColorInterpretation(
823 955 : poSrcBand->GetColorInterpretation());
824 :
825 1776 : char **papszCatNames = poSrcBand->GetCategoryNames();
826 1776 : if (nullptr != papszCatNames)
827 1 : poDstBand->SetCategoryNames(papszCatNames);
828 :
829 : // Only copy RAT if it is of reasonable size to fit in memory
830 1776 : GDALRasterAttributeTable *poRAT = poSrcBand->GetDefaultRAT();
831 1778 : if (poRAT != nullptr && static_cast<GIntBig>(poRAT->GetColumnCount()) *
832 2 : poRAT->GetRowCount() <
833 : 1024 * 1024)
834 : {
835 2 : poDstBand->SetDefaultRAT(poRAT);
836 : }
837 :
838 1776 : if (!bStrict)
839 : {
840 1142 : CPLTurnFailureIntoWarning(false);
841 : }
842 : else
843 : {
844 634 : eErr = CPLGetLastErrorType();
845 : }
846 : }
847 :
848 : /* -------------------------------------------------------------------- */
849 : /* Copy image data. */
850 : /* -------------------------------------------------------------------- */
851 1112 : if (eErr == CE_None && nDstBands > 0)
852 : {
853 1068 : const char *const apszCopyRasterOptionsSkipHoles[] = {"SKIP_HOLES=YES",
854 : nullptr};
855 1068 : const bool bSkipHoles = CPLTestBool(
856 : CSLFetchNameValueDef(papszOptions, "SKIP_HOLES", "FALSE"));
857 1068 : eErr = GDALDatasetCopyWholeRaster(
858 : poSrcDS, poDstDS,
859 : bSkipHoles ? apszCopyRasterOptionsSkipHoles : nullptr, pfnProgress,
860 : pProgressData);
861 : }
862 :
863 : /* -------------------------------------------------------------------- */
864 : /* Should we copy some masks over? */
865 : /* -------------------------------------------------------------------- */
866 1112 : if (eErr == CE_None && nDstBands > 0)
867 1050 : eErr = DefaultCopyMasks(poSrcDS, poDstDS, eErr);
868 :
869 : /* -------------------------------------------------------------------- */
870 : /* Copy vector layers */
871 : /* -------------------------------------------------------------------- */
872 1112 : if (eErr == CE_None)
873 : {
874 1063 : if (nLayerCount > 0 && poDstDS->TestCapability(ODsCCreateLayer))
875 : {
876 20 : for (int iLayer = 0; iLayer < nLayerCount; ++iLayer)
877 : {
878 10 : OGRLayer *poLayer = poSrcDS->GetLayer(iLayer);
879 :
880 10 : if (poLayer == nullptr)
881 0 : continue;
882 :
883 10 : poDstDS->CopyLayer(poLayer, poLayer->GetName(), nullptr);
884 : }
885 : }
886 : }
887 :
888 : /* -------------------------------------------------------------------- */
889 : /* Try to cleanup the output dataset if the translation failed. */
890 : /* -------------------------------------------------------------------- */
891 1112 : if (eErr != CE_None)
892 : {
893 49 : delete poDstDS;
894 49 : if (!CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false))
895 : {
896 : // Only delete if creating a new file
897 49 : Delete(pszFilename);
898 : }
899 49 : return nullptr;
900 : }
901 : else
902 : {
903 1063 : CPLErrorReset();
904 : }
905 :
906 1063 : return poDstDS;
907 : }
908 :
909 : /************************************************************************/
910 : /* DefaultCopyMetadata() */
911 : /************************************************************************/
912 :
913 5474 : void GDALDriver::DefaultCopyMetadata(GDALDataset *poSrcDS, GDALDataset *poDstDS,
914 : CSLConstList papszOptions,
915 : CSLConstList papszExcludedDomains)
916 : {
917 : const char *pszCopySrcMDD =
918 5474 : CSLFetchNameValueDef(papszOptions, "COPY_SRC_MDD", "AUTO");
919 5474 : char **papszSrcMDD = CSLFetchNameValueMultiple(papszOptions, "SRC_MDD");
920 5474 : if (EQUAL(pszCopySrcMDD, "AUTO") || CPLTestBool(pszCopySrcMDD) ||
921 : papszSrcMDD)
922 : {
923 4 : if ((!papszSrcMDD || CSLFindString(papszSrcMDD, "") >= 0 ||
924 2 : CSLFindString(papszSrcMDD, "_DEFAULT_") >= 0) &&
925 10790 : CSLFindString(papszExcludedDomains, "") < 0 &&
926 5314 : CSLFindString(papszExcludedDomains, "_DEFAULT_") < 0)
927 : {
928 5314 : if (poSrcDS->GetMetadata() != nullptr)
929 870 : poDstDS->SetMetadata(poSrcDS->GetMetadata());
930 : }
931 :
932 : /* -------------------------------------------------------------------- */
933 : /* Copy transportable special domain metadata. */
934 : /* It would be nice to copy geolocation, but it is pretty fragile. */
935 : /* -------------------------------------------------------------------- */
936 5472 : constexpr const char *apszDefaultDomains[] = {
937 : "RPC", "xml:XMP", "json:ISIS3", "json:VICAR"};
938 27360 : for (const char *pszDomain : apszDefaultDomains)
939 : {
940 43760 : if ((!papszSrcMDD || CSLFindString(papszSrcMDD, pszDomain) >= 0) &&
941 21872 : CSLFindString(papszExcludedDomains, pszDomain) < 0)
942 : {
943 21872 : CSLConstList papszMD = poSrcDS->GetMetadata(pszDomain);
944 21872 : if (papszMD)
945 5 : poDstDS->SetMetadata(papszMD, pszDomain);
946 : }
947 : }
948 :
949 5472 : if ((!EQUAL(pszCopySrcMDD, "AUTO") && CPLTestBool(pszCopySrcMDD)) ||
950 : papszSrcMDD)
951 : {
952 24 : for (const char *pszDomain :
953 30 : CPLStringList(poSrcDS->GetMetadataDomainList()))
954 : {
955 36 : if (pszDomain[0] != 0 &&
956 12 : (!papszSrcMDD ||
957 12 : CSLFindString(papszSrcMDD, pszDomain) >= 0))
958 : {
959 10 : bool bCanCopy = true;
960 10 : if (CSLFindString(papszExcludedDomains, pszDomain) >= 0)
961 : {
962 0 : bCanCopy = false;
963 : }
964 : else
965 : {
966 50 : for (const char *pszOtherDomain : apszDefaultDomains)
967 : {
968 40 : if (EQUAL(pszDomain, pszOtherDomain))
969 : {
970 0 : bCanCopy = false;
971 0 : break;
972 : }
973 : }
974 10 : if (!papszSrcMDD)
975 : {
976 6 : constexpr const char *const apszReservedDomains[] =
977 : {"IMAGE_STRUCTURE", "DERIVED_SUBDATASETS"};
978 6 : for (const char *pszOtherDomain :
979 12 : apszReservedDomains)
980 : {
981 10 : if (EQUAL(pszDomain, pszOtherDomain))
982 : {
983 4 : bCanCopy = false;
984 4 : break;
985 : }
986 : }
987 : }
988 : }
989 10 : if (bCanCopy)
990 : {
991 6 : poDstDS->SetMetadata(poSrcDS->GetMetadata(pszDomain),
992 6 : pszDomain);
993 : }
994 : }
995 : }
996 : }
997 : }
998 5474 : CSLDestroy(papszSrcMDD);
999 5474 : }
1000 :
1001 : /************************************************************************/
1002 : /* QuietDeleteForCreateCopy() */
1003 : /************************************************************************/
1004 :
1005 12228 : CPLErr GDALDriver::QuietDeleteForCreateCopy(const char *pszFilename,
1006 : GDALDataset *poSrcDS)
1007 : {
1008 : // Someone issuing CreateCopy("foo.tif") on a
1009 : // memory driver doesn't expect files with those names to be deleted
1010 : // on a file system...
1011 : // This is somewhat messy. Ideally there should be a way for the
1012 : // driver to overload the default behavior
1013 23817 : if (!EQUAL(GetDescription(), "MEM") && !EQUAL(GetDescription(), "Memory") &&
1014 : // Also exclude database formats for which there's no file list
1015 : // and whose opening might be slow (GeoRaster in particular)
1016 35406 : !EQUAL(GetDescription(), "GeoRaster") &&
1017 11589 : !EQUAL(GetDescription(), "PostGISRaster"))
1018 : {
1019 : /* --------------------------------------------------------------------
1020 : */
1021 : /* Establish list of files of output dataset if it already
1022 : * exists. */
1023 : /* --------------------------------------------------------------------
1024 : */
1025 23142 : std::set<std::string> oSetExistingDestFiles;
1026 : {
1027 23142 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
1028 11571 : const char *const apszAllowedDrivers[] = {GetDescription(),
1029 11571 : nullptr};
1030 : auto poExistingOutputDS =
1031 : std::unique_ptr<GDALDataset>(GDALDataset::Open(
1032 23142 : pszFilename, GDAL_OF_RASTER, apszAllowedDrivers));
1033 11571 : if (poExistingOutputDS)
1034 : {
1035 694 : for (const char *pszFileInList :
1036 660 : CPLStringList(poExistingOutputDS->GetFileList()))
1037 : {
1038 : oSetExistingDestFiles.insert(
1039 347 : CPLString(pszFileInList).replaceAll('\\', '/'));
1040 : }
1041 : }
1042 : }
1043 :
1044 : /* --------------------------------------------------------------------
1045 : */
1046 : /* Check if the source dataset shares some files with the dest
1047 : * one.*/
1048 : /* --------------------------------------------------------------------
1049 : */
1050 23142 : std::set<std::string> oSetExistingDestFilesFoundInSource;
1051 11571 : if (!oSetExistingDestFiles.empty())
1052 : {
1053 626 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
1054 : // We need to reopen in a temporary dataset for the particular
1055 : // case of overwritten a .tif.ovr file from a .tif
1056 : // If we probe the file list of the .tif, it will then open the
1057 : // .tif.ovr !
1058 313 : const char *const apszAllowedDrivers[] = {
1059 313 : poSrcDS->GetDriver() ? poSrcDS->GetDriver()->GetDescription()
1060 : : nullptr,
1061 313 : nullptr};
1062 : auto poSrcDSTmp = std::unique_ptr<GDALDataset>(GDALDataset::Open(
1063 313 : poSrcDS->GetDescription(), GDAL_OF_RASTER, apszAllowedDrivers,
1064 626 : poSrcDS->papszOpenOptions));
1065 313 : if (poSrcDSTmp)
1066 : {
1067 212 : for (const char *pszFileInList :
1068 412 : CPLStringList(poSrcDSTmp->GetFileList()))
1069 : {
1070 424 : CPLString osFilename(pszFileInList);
1071 212 : osFilename.replaceAll('\\', '/');
1072 212 : if (cpl::contains(oSetExistingDestFiles, osFilename))
1073 : {
1074 : oSetExistingDestFilesFoundInSource.insert(
1075 16 : std::move(osFilename));
1076 : }
1077 : }
1078 : }
1079 : }
1080 :
1081 : // If the source file(s) and the dest one share some files in
1082 : // common, only remove the files that are *not* in common
1083 11571 : if (!oSetExistingDestFilesFoundInSource.empty())
1084 : {
1085 36 : for (const std::string &osFilename : oSetExistingDestFiles)
1086 : {
1087 21 : if (!cpl::contains(oSetExistingDestFilesFoundInSource,
1088 : osFilename))
1089 : {
1090 5 : VSIUnlink(osFilename.c_str());
1091 : }
1092 : }
1093 : }
1094 :
1095 11571 : QuietDelete(pszFilename);
1096 : }
1097 :
1098 12228 : return CE_None;
1099 : }
1100 :
1101 : //! @endcond
1102 :
1103 : /************************************************************************/
1104 : /* CreateCopy() */
1105 : /************************************************************************/
1106 :
1107 : /**
1108 : * \brief Create a copy of a dataset.
1109 : *
1110 : * This method will attempt to create a copy of a raster dataset with the
1111 : * indicated filename, and in this drivers format. Band number, size,
1112 : * type, projection, geotransform and so forth are all to be copied from
1113 : * the provided template dataset.
1114 : *
1115 : * Note that many sequential write once formats (such as JPEG and PNG) don't
1116 : * implement the Create() method but do implement this CreateCopy() method.
1117 : * If the driver doesn't implement CreateCopy(), but does implement Create()
1118 : * then the default CreateCopy() mechanism built on calling Create() will
1119 : * be used.
1120 : * So to test if CreateCopy() is available, you can test if GDAL_DCAP_CREATECOPY
1121 : * or GDAL_DCAP_CREATE is set in the GDAL metadata.
1122 : *
1123 : * It is intended that CreateCopy() will often be used with a source dataset
1124 : * which is a virtual dataset allowing configuration of band types, and other
1125 : * information without actually duplicating raster data (see the VRT driver).
1126 : * This is what is done by the gdal_translate utility for example.
1127 : *
1128 : * That function will try to validate the creation option list passed to the
1129 : * driver with the GDALValidateCreationOptions() method. This check can be
1130 : * disabled by defining the configuration option
1131 : * GDAL_VALIDATE_CREATION_OPTIONS=NO.
1132 : *
1133 : * This function copy all metadata from the default domain ("")
1134 : *
1135 : * Even is bStrict is TRUE, only the <b>value</b> of the data is equivalent,
1136 : * but the data layout (INTERLEAVE as PIXEL/LINE/BAND) of the dst dataset is
1137 : * controlled by the papszOptions creation options, and may differ from the
1138 : * poSrcDS src dataset.
1139 : * Starting from GDAL 3.5, if no INTERLEAVE and COMPRESS creation option has
1140 : * been specified in papszOptions, and if the driver supports equivalent
1141 : * interleaving as the src dataset, the CreateCopy() will internally add the
1142 : * proper creation option to get the same data interleaving.
1143 : *
1144 : * After you have finished working with the returned dataset, it is
1145 : * <b>required</b> to close it with GDALClose(). This does not only close the
1146 : * file handle, but also ensures that all the data and metadata has been written
1147 : * to the dataset (GDALFlushCache() is not sufficient for that purpose).
1148 : *
1149 : * For multidimensional datasets, papszOptions can contain array creation
1150 : * options, if they are prefixed with "ARRAY:". \see GDALGroup::CopyFrom()
1151 : * documentation for further details regarding such options.
1152 : *
1153 : * @param pszFilename the name for the new dataset. UTF-8 encoded.
1154 : * @param poSrcDS the dataset being duplicated.
1155 : * @param bStrict TRUE if the copy must be strictly equivalent, or more
1156 : * normally FALSE indicating that the copy may adapt as needed for the
1157 : * output format.
1158 : * @param papszOptions additional format dependent options controlling
1159 : * creation of the output file.
1160 : * The APPEND_SUBDATASET=YES option can be specified to avoid prior destruction
1161 : * of existing dataset.
1162 : * Starting with GDAL 3.8.0, the following options are recognized by the
1163 : * GTiff, COG, VRT, PNG au JPEG drivers:
1164 : * <ul>
1165 : * <li>COPY_SRC_MDD=AUTO/YES/NO: whether metadata domains of the source dataset
1166 : * should be copied to the destination dataset. In the default AUTO mode, only
1167 : * "safe" domains will be copied, which include the default metadata domain
1168 : * (some drivers may include other domains such as IMD, RPC, GEOLOCATION). When
1169 : * setting YES, all domains will be copied (but a few reserved ones like
1170 : * IMAGE_STRUCTURE or DERIVED_SUBDATASETS). When setting NO, no source metadata
1171 : * will be copied.
1172 : * </li>
1173 : *<li>SRC_MDD=domain_name: which source metadata domain should be copied.
1174 : * This option restricts the list of source metadata domains to be copied
1175 : * (it implies COPY_SRC_MDD=YES if it is not set). This option may be specified
1176 : * as many times as they are source domains. The default metadata domain is the
1177 : * empty string "" ("_DEFAULT_") may also be used when empty string is not practical)
1178 : * </li>
1179 : * </ul>
1180 : * @param pfnProgress a function to be used to report progress of the copy.
1181 : * @param pProgressData application data passed into progress function.
1182 : *
1183 : * @return a pointer to the newly created dataset (may be read-only access).
1184 : */
1185 :
1186 11674 : GDALDataset *GDALDriver::CreateCopy(const char *pszFilename,
1187 : GDALDataset *poSrcDS, int bStrict,
1188 : CSLConstList papszOptions,
1189 : GDALProgressFunc pfnProgress,
1190 : void *pProgressData)
1191 :
1192 : {
1193 11674 : if (pfnProgress == nullptr)
1194 8840 : pfnProgress = GDALDummyProgress;
1195 :
1196 11674 : const int nBandCount = poSrcDS->GetRasterCount();
1197 :
1198 : /* -------------------------------------------------------------------- */
1199 : /* If no INTERLEAVE creation option is given, we will try to add */
1200 : /* one that matches the current srcDS interleaving */
1201 : /* -------------------------------------------------------------------- */
1202 11674 : char **papszOptionsToDelete = nullptr;
1203 : const char *srcInterleave =
1204 11674 : poSrcDS->GetMetadataItem("INTERLEAVE", "IMAGE_STRUCTURE");
1205 6946 : if (nBandCount > 1 && srcInterleave != nullptr &&
1206 25165 : CSLFetchNameValue(papszOptions, "INTERLEAVE") == nullptr &&
1207 6545 : EQUAL(CSLFetchNameValueDef(papszOptions, "COMPRESS", "NONE"), "NONE"))
1208 : {
1209 :
1210 : // look for INTERLEAVE values of the driver
1211 6362 : char **interleavesCSL = nullptr;
1212 : const char *pszOptionList =
1213 6362 : this->GetMetadataItem(GDAL_DMD_CREATIONOPTIONLIST);
1214 : CPLXMLNode *xmlNode =
1215 6362 : !pszOptionList ? nullptr : CPLParseXMLString(pszOptionList);
1216 6362 : for (CPLXMLNode *child = !xmlNode ? nullptr : xmlNode->psChild;
1217 132426 : child != nullptr; child = child->psNext)
1218 : {
1219 126064 : if ((child->eType == CXT_Element) &&
1220 126064 : EQUAL(child->pszValue, "Option"))
1221 : {
1222 : const char *nameAttribute =
1223 126064 : CPLGetXMLValue(child, "name", nullptr);
1224 126064 : const bool isInterleaveAttribute =
1225 126064 : nameAttribute && EQUAL(nameAttribute, "INTERLEAVE");
1226 126064 : if (isInterleaveAttribute)
1227 : {
1228 1189 : for (CPLXMLNode *optionChild = child->psChild;
1229 7202 : optionChild != nullptr;
1230 6013 : optionChild = optionChild->psNext)
1231 : {
1232 6013 : if ((optionChild->eType == CXT_Element) &&
1233 2431 : EQUAL(optionChild->pszValue, "Value"))
1234 : {
1235 2431 : CPLXMLNode *optionChildValue = optionChild->psChild;
1236 2431 : if (optionChildValue &&
1237 2431 : (optionChildValue->eType == CXT_Text))
1238 : {
1239 2431 : interleavesCSL = CSLAddString(
1240 2431 : interleavesCSL, optionChildValue->pszValue);
1241 : }
1242 : }
1243 : }
1244 : }
1245 : }
1246 : }
1247 6362 : CPLDestroyXMLNode(xmlNode);
1248 :
1249 : const char *dstInterleaveBand =
1250 11559 : (CSLFindString(interleavesCSL, "BAND") >= 0) ? "BAND"
1251 5197 : : (CSLFindString(interleavesCSL, "BSQ") >= 0) ? "BSQ"
1252 6362 : : nullptr;
1253 : const char *dstInterleaveLine =
1254 12724 : (CSLFindString(interleavesCSL, "LINE") >= 0) ? "LINE"
1255 6362 : : (CSLFindString(interleavesCSL, "BIL") >= 0) ? "BIL"
1256 6362 : : nullptr;
1257 : const char *dstInterleavePixel =
1258 11559 : (CSLFindString(interleavesCSL, "PIXEL") >= 0) ? "PIXEL"
1259 5197 : : (CSLFindString(interleavesCSL, "BIP") >= 0) ? "BIP"
1260 6362 : : nullptr;
1261 6362 : const char *dstInterleave =
1262 6674 : EQUAL(srcInterleave, "BAND") ? dstInterleaveBand
1263 622 : : EQUAL(srcInterleave, "LINE") ? dstInterleaveLine
1264 310 : : EQUAL(srcInterleave, "PIXEL") ? dstInterleavePixel
1265 : : nullptr;
1266 6362 : CSLDestroy(interleavesCSL);
1267 :
1268 6362 : if (dstInterleave != nullptr)
1269 : {
1270 1189 : papszOptionsToDelete = CSLDuplicate(papszOptions);
1271 1189 : papszOptionsToDelete = CSLSetNameValue(papszOptionsToDelete,
1272 : "INTERLEAVE", dstInterleave);
1273 1189 : papszOptionsToDelete = CSLSetNameValue(
1274 : papszOptionsToDelete, "@INTERLEAVE_ADDED_AUTOMATICALLY", "YES");
1275 1189 : papszOptions = papszOptionsToDelete;
1276 : }
1277 : }
1278 :
1279 : /* -------------------------------------------------------------------- */
1280 : /* Make sure we cleanup if there is an existing dataset of this */
1281 : /* name. But even if that seems to fail we will continue since */
1282 : /* it might just be a corrupt file or something. */
1283 : /* -------------------------------------------------------------------- */
1284 : const bool bAppendSubdataset =
1285 11674 : CPLFetchBool(papszOptions, "APPEND_SUBDATASET", false);
1286 : // Note: @QUIET_DELETE_ON_CREATE_COPY is set to NO by the KMLSuperOverlay
1287 : // driver when writing a .kmz file. Also by GDALTranslate() if it has
1288 : // already done a similar job.
1289 23323 : if (!bAppendSubdataset &&
1290 11649 : CPLFetchBool(papszOptions, "@QUIET_DELETE_ON_CREATE_COPY", true))
1291 : {
1292 8999 : QuietDeleteForCreateCopy(pszFilename, poSrcDS);
1293 : }
1294 :
1295 : int iIdxQuietDeleteOnCreateCopy =
1296 11674 : CSLPartialFindString(papszOptions, "@QUIET_DELETE_ON_CREATE_COPY=");
1297 11674 : if (iIdxQuietDeleteOnCreateCopy >= 0)
1298 : {
1299 2650 : if (papszOptionsToDelete == nullptr)
1300 1599 : papszOptionsToDelete = CSLDuplicate(papszOptions);
1301 2650 : papszOptionsToDelete = CSLRemoveStrings(
1302 : papszOptionsToDelete, iIdxQuietDeleteOnCreateCopy, 1, nullptr);
1303 2650 : papszOptions = papszOptionsToDelete;
1304 : }
1305 :
1306 : /* -------------------------------------------------------------------- */
1307 : /* If _INTERNAL_DATASET=YES, the returned dataset will not be */
1308 : /* registered in the global list of open datasets. */
1309 : /* -------------------------------------------------------------------- */
1310 : const int iIdxInternalDataset =
1311 11674 : CSLPartialFindString(papszOptions, "_INTERNAL_DATASET=");
1312 11674 : bool bInternalDataset = false;
1313 11674 : if (iIdxInternalDataset >= 0)
1314 : {
1315 : bInternalDataset =
1316 4165 : CPLFetchBool(papszOptions, "_INTERNAL_DATASET", false);
1317 4165 : if (papszOptionsToDelete == nullptr)
1318 4165 : papszOptionsToDelete = CSLDuplicate(papszOptions);
1319 4165 : papszOptionsToDelete = CSLRemoveStrings(
1320 : papszOptionsToDelete, iIdxInternalDataset, 1, nullptr);
1321 4165 : papszOptions = papszOptionsToDelete;
1322 : }
1323 :
1324 : /* -------------------------------------------------------------------- */
1325 : /* Validate creation options. */
1326 : /* -------------------------------------------------------------------- */
1327 11674 : if (CPLTestBool(
1328 : CPLGetConfigOption("GDAL_VALIDATE_CREATION_OPTIONS", "YES")))
1329 : {
1330 23348 : auto poSrcGroup = poSrcDS->GetRootGroup();
1331 11674 : if (poSrcGroup != nullptr && GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER))
1332 : {
1333 224 : CPLStringList aosDatasetCO;
1334 119 : for (const char *pszOption : cpl::Iterate(papszOptions))
1335 : {
1336 7 : if (!STARTS_WITH_CI(pszOption, "ARRAY:"))
1337 0 : aosDatasetCO.AddString(pszOption);
1338 : }
1339 112 : GDALValidateCreationOptions(this, aosDatasetCO.List());
1340 : }
1341 : else
1342 : {
1343 11562 : GDALValidateCreationOptions(this, papszOptions);
1344 : }
1345 : }
1346 :
1347 : /* -------------------------------------------------------------------- */
1348 : /* Advise the source raster that we are going to read it completely */
1349 : /* -------------------------------------------------------------------- */
1350 :
1351 11674 : const int nXSize = poSrcDS->GetRasterXSize();
1352 11674 : const int nYSize = poSrcDS->GetRasterYSize();
1353 11674 : GDALDataType eDT = GDT_Unknown;
1354 11674 : if (nBandCount > 0)
1355 : {
1356 11439 : GDALRasterBand *poSrcBand = poSrcDS->GetRasterBand(1);
1357 11439 : if (poSrcBand)
1358 11439 : eDT = poSrcBand->GetRasterDataType();
1359 : }
1360 11674 : poSrcDS->AdviseRead(0, 0, nXSize, nYSize, nXSize, nYSize, eDT, nBandCount,
1361 11674 : nullptr, nullptr);
1362 :
1363 : /* -------------------------------------------------------------------- */
1364 : /* If the format provides a CreateCopy() method use that, */
1365 : /* otherwise fallback to the internal implementation using the */
1366 : /* Create() method. */
1367 : /* -------------------------------------------------------------------- */
1368 11674 : GDALDataset *poDstDS = nullptr;
1369 11674 : auto l_pfnCreateCopy = GetCreateCopyCallback();
1370 22173 : if (l_pfnCreateCopy != nullptr &&
1371 10499 : !CPLTestBool(CPLGetConfigOption("GDAL_DEFAULT_CREATE_COPY", "NO")))
1372 : {
1373 10499 : poDstDS = l_pfnCreateCopy(pszFilename, poSrcDS, bStrict,
1374 : const_cast<char **>(papszOptions),
1375 : pfnProgress, pProgressData);
1376 10499 : if (poDstDS != nullptr)
1377 : {
1378 18480 : if (poDstDS->GetDescription() == nullptr ||
1379 9240 : strlen(poDstDS->GetDescription()) == 0)
1380 431 : poDstDS->SetDescription(pszFilename);
1381 :
1382 9240 : if (poDstDS->poDriver == nullptr)
1383 8169 : poDstDS->poDriver = this;
1384 :
1385 9240 : if (!bInternalDataset)
1386 5075 : poDstDS->AddToDatasetOpenList();
1387 : }
1388 : }
1389 : else
1390 : {
1391 1175 : poDstDS = DefaultCreateCopy(pszFilename, poSrcDS, bStrict, papszOptions,
1392 : pfnProgress, pProgressData);
1393 : }
1394 :
1395 11674 : CSLDestroy(papszOptionsToDelete);
1396 11674 : return poDstDS;
1397 : }
1398 :
1399 : /************************************************************************/
1400 : /* GDALCreateCopy() */
1401 : /************************************************************************/
1402 :
1403 : /**
1404 : * \brief Create a copy of a dataset.
1405 : *
1406 : * @see GDALDriver::CreateCopy()
1407 : */
1408 :
1409 6850 : GDALDatasetH CPL_STDCALL GDALCreateCopy(GDALDriverH hDriver,
1410 : const char *pszFilename,
1411 : GDALDatasetH hSrcDS, int bStrict,
1412 : CSLConstList papszOptions,
1413 : GDALProgressFunc pfnProgress,
1414 : void *pProgressData)
1415 :
1416 : {
1417 6850 : VALIDATE_POINTER1(hDriver, "GDALCreateCopy", nullptr);
1418 6850 : VALIDATE_POINTER1(hSrcDS, "GDALCreateCopy", nullptr);
1419 :
1420 6850 : return GDALDriver::FromHandle(hDriver)->CreateCopy(
1421 : pszFilename, GDALDataset::FromHandle(hSrcDS), bStrict, papszOptions,
1422 6850 : pfnProgress, pProgressData);
1423 : }
1424 :
1425 : /************************************************************************/
1426 : /* CanVectorTranslateFrom() */
1427 : /************************************************************************/
1428 :
1429 : /** Returns whether the driver can translate from a vector dataset,
1430 : * using the arguments passed to GDALVectorTranslate() stored in
1431 : * papszVectorTranslateArguments.
1432 : *
1433 : * This is used to determine if the driver supports the VectorTranslateFrom()
1434 : * operation.
1435 : *
1436 : * @param pszDestName Target dataset name
1437 : * @param poSourceDS Source dataset
1438 : * @param papszVectorTranslateArguments Non-positional arguments passed to
1439 : * GDALVectorTranslate() (may be nullptr)
1440 : * @param[out] ppapszFailureReasons nullptr, or a pointer to an null-terminated
1441 : * array of strings to record the reason(s) for the impossibility.
1442 : * @return true if VectorTranslateFrom() can be called with the same arguments.
1443 : * @since GDAL 3.8
1444 : */
1445 919 : bool GDALDriver::CanVectorTranslateFrom(
1446 : const char *pszDestName, GDALDataset *poSourceDS,
1447 : CSLConstList papszVectorTranslateArguments, char ***ppapszFailureReasons)
1448 :
1449 : {
1450 919 : if (ppapszFailureReasons)
1451 : {
1452 0 : *ppapszFailureReasons = nullptr;
1453 : }
1454 :
1455 919 : if (!pfnCanVectorTranslateFrom)
1456 : {
1457 913 : if (ppapszFailureReasons)
1458 : {
1459 0 : *ppapszFailureReasons = CSLAddString(
1460 : nullptr,
1461 : "CanVectorTranslateFrom() not implemented for this driver");
1462 : }
1463 913 : return false;
1464 : }
1465 :
1466 6 : char **papszFailureReasons = nullptr;
1467 6 : bool bRet = pfnCanVectorTranslateFrom(
1468 : pszDestName, poSourceDS, papszVectorTranslateArguments,
1469 : ppapszFailureReasons ? ppapszFailureReasons : &papszFailureReasons);
1470 6 : if (!ppapszFailureReasons)
1471 : {
1472 2 : for (const char *pszReason :
1473 10 : cpl::Iterate(static_cast<CSLConstList>(papszFailureReasons)))
1474 : {
1475 2 : CPLDebug("GDAL", "%s", pszReason);
1476 : }
1477 6 : CSLDestroy(papszFailureReasons);
1478 : }
1479 6 : return bRet;
1480 : }
1481 :
1482 193 : bool GDALDriver::HasOpenOption(const char *pszOpenOptionName) const
1483 : {
1484 193 : if (pszOpenOptionName == nullptr)
1485 0 : return false;
1486 :
1487 : // Const cast is safe here since we are only reading the metadata
1488 386 : auto pszOOMd{const_cast<GDALDriver *>(this)->GetMetadataItem(
1489 193 : GDAL_DMD_OPENOPTIONLIST)};
1490 193 : if (pszOOMd == nullptr)
1491 74 : return false;
1492 :
1493 238 : const CPLXMLTreeCloser oXml{CPLParseXMLString(pszOOMd)};
1494 1185 : for (CPLXMLNode *option = oXml->psChild; option != nullptr;
1495 1066 : option = option->psNext)
1496 : {
1497 1067 : if (EQUAL(CPLGetXMLValue(CPLGetXMLNode(option, "name"), nullptr, ""),
1498 : pszOpenOptionName))
1499 1 : return true;
1500 : }
1501 118 : return false;
1502 : }
1503 :
1504 : /************************************************************************/
1505 : /* VectorTranslateFrom() */
1506 : /************************************************************************/
1507 :
1508 : /** Create a copy of a vector dataset, using the arguments passed to
1509 : * GDALVectorTranslate() stored in papszVectorTranslateArguments.
1510 : *
1511 : * This may be implemented by some drivers that can convert from an existing
1512 : * dataset in an optimized way.
1513 : *
1514 : * This is for example used by the PMTiles to convert from MBTiles.
1515 : *
1516 : * @param pszDestName Target dataset name
1517 : * @param poSourceDS Source dataset
1518 : * @param papszVectorTranslateArguments Non-positional arguments passed to
1519 : * GDALVectorTranslate() (may be nullptr)
1520 : * @param pfnProgress a function to be used to report progress of the copy.
1521 : * @param pProgressData application data passed into progress function.
1522 : * @return a new dataset in case of success, or nullptr in case of error.
1523 : * @since GDAL 3.8
1524 : */
1525 4 : GDALDataset *GDALDriver::VectorTranslateFrom(
1526 : const char *pszDestName, GDALDataset *poSourceDS,
1527 : CSLConstList papszVectorTranslateArguments, GDALProgressFunc pfnProgress,
1528 : void *pProgressData)
1529 :
1530 : {
1531 4 : if (!pfnVectorTranslateFrom)
1532 : {
1533 0 : CPLError(CE_Failure, CPLE_AppDefined,
1534 : "VectorTranslateFrom() not implemented for this driver");
1535 0 : return nullptr;
1536 : }
1537 :
1538 4 : return pfnVectorTranslateFrom(pszDestName, poSourceDS,
1539 : papszVectorTranslateArguments, pfnProgress,
1540 4 : pProgressData);
1541 : }
1542 :
1543 : /************************************************************************/
1544 : /* QuietDelete() */
1545 : /************************************************************************/
1546 :
1547 : /**
1548 : * \brief Delete dataset if found.
1549 : *
1550 : * This is a helper method primarily used by Create() and
1551 : * CreateCopy() to predelete any dataset of the name soon to be
1552 : * created. It will attempt to delete the named dataset if
1553 : * one is found, otherwise it does nothing. An error is only
1554 : * returned if the dataset is found but the delete fails.
1555 : *
1556 : * This is a static method and it doesn't matter what driver instance
1557 : * it is invoked on. It will attempt to discover the correct driver
1558 : * using Identify().
1559 : *
1560 : * @param pszName the dataset name to try and delete.
1561 : * @param papszAllowedDrivers NULL to consider all candidate drivers, or a NULL
1562 : * terminated list of strings with the driver short names that must be
1563 : * considered. (Note: implemented only starting with GDAL 3.4.1)
1564 : * @return CE_None if the dataset does not exist, or is deleted without issues.
1565 : */
1566 :
1567 25138 : CPLErr GDALDriver::QuietDelete(const char *pszName,
1568 : CSLConstList papszAllowedDrivers)
1569 :
1570 : {
1571 : VSIStatBufL sStat;
1572 : const bool bExists =
1573 25138 : VSIStatExL(pszName, &sStat,
1574 25138 : VSI_STAT_EXISTS_FLAG | VSI_STAT_NATURE_FLAG) == 0;
1575 :
1576 : #ifdef S_ISFIFO
1577 25138 : if (bExists && S_ISFIFO(sStat.st_mode))
1578 0 : return CE_None;
1579 : #endif
1580 :
1581 25138 : GDALDriver *poDriver = nullptr;
1582 25138 : if (papszAllowedDrivers)
1583 : {
1584 76 : GDALOpenInfo oOpenInfo(pszName, GDAL_OF_ALL);
1585 38 : for (const char *pszDriverName : cpl::Iterate(papszAllowedDrivers))
1586 : {
1587 : GDALDriver *poTmpDriver =
1588 38 : GDALDriver::FromHandle(GDALGetDriverByName(pszDriverName));
1589 38 : if (poTmpDriver)
1590 : {
1591 : const bool bIdentifyRes =
1592 38 : poTmpDriver->pfnIdentifyEx
1593 76 : ? poTmpDriver->pfnIdentifyEx(poTmpDriver, &oOpenInfo) >
1594 : 0
1595 76 : : poTmpDriver->pfnIdentify &&
1596 38 : poTmpDriver->pfnIdentify(&oOpenInfo) > 0;
1597 38 : if (bIdentifyRes)
1598 : {
1599 38 : poDriver = poTmpDriver;
1600 38 : break;
1601 : }
1602 : }
1603 : }
1604 : }
1605 : else
1606 : {
1607 50200 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
1608 25100 : poDriver = GDALDriver::FromHandle(GDALIdentifyDriver(pszName, nullptr));
1609 : }
1610 :
1611 25138 : if (poDriver == nullptr)
1612 24210 : return CE_None;
1613 :
1614 971 : if (bExists && VSI_ISDIR(sStat.st_mode) &&
1615 43 : (EQUAL(poDriver->GetDescription(), "MapInfo File") ||
1616 43 : EQUAL(poDriver->GetDescription(), "ESRI Shapefile")))
1617 : {
1618 : // Those drivers are a bit special and handle directories as container
1619 : // of layers, but it is quite common to found other files too, and
1620 : // removing the directory might be non-desirable.
1621 42 : return CE_None;
1622 : }
1623 :
1624 886 : CPLDebug("GDAL", "QuietDelete(%s) invoking Delete()", pszName);
1625 :
1626 886 : poDriver->pfnDelete = poDriver->GetDeleteCallback();
1627 924 : const bool bQuiet = !bExists && poDriver->pfnDelete == nullptr &&
1628 38 : poDriver->pfnDeleteDataSource == nullptr;
1629 886 : if (bQuiet)
1630 : {
1631 76 : CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
1632 38 : return poDriver->Delete(pszName);
1633 : }
1634 : else
1635 : {
1636 848 : return poDriver->Delete(pszName);
1637 : }
1638 : }
1639 :
1640 : /************************************************************************/
1641 : /* Delete() */
1642 : /************************************************************************/
1643 :
1644 : /**
1645 : * \brief Delete named dataset.
1646 : *
1647 : * The driver will attempt to delete the named dataset in a driver specific
1648 : * fashion. Full featured drivers will delete all associated files,
1649 : * database objects, or whatever is appropriate. The default behavior when
1650 : * no driver specific behavior is provided is to attempt to delete all the
1651 : * files that are returned by GDALGetFileList() on the dataset handle.
1652 : *
1653 : * It is unwise to have open dataset handles on this dataset when it is
1654 : * deleted.
1655 : *
1656 : * Equivalent of the C function GDALDeleteDataset().
1657 : *
1658 : * @param pszFilename name of dataset to delete.
1659 : *
1660 : * @return CE_None on success, or CE_Failure if the operation fails.
1661 : */
1662 :
1663 4401 : CPLErr GDALDriver::Delete(const char *pszFilename)
1664 :
1665 : {
1666 4401 : pfnDelete = GetDeleteCallback();
1667 4401 : if (pfnDelete != nullptr)
1668 1080 : return pfnDelete(pszFilename);
1669 3321 : else if (pfnDeleteDataSource != nullptr)
1670 0 : return pfnDeleteDataSource(this, pszFilename);
1671 :
1672 : /* -------------------------------------------------------------------- */
1673 : /* Collect file list. */
1674 : /* -------------------------------------------------------------------- */
1675 3321 : GDALDatasetH hDS = GDALOpenEx(pszFilename, GDAL_OF_VERBOSE_ERROR, nullptr,
1676 : nullptr, nullptr);
1677 :
1678 3321 : if (hDS == nullptr)
1679 : {
1680 237 : if (CPLGetLastErrorNo() == 0)
1681 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1682 : "Unable to open %s to obtain file list.", pszFilename);
1683 :
1684 237 : return CE_Failure;
1685 : }
1686 :
1687 6168 : const CPLStringList aosFileList(GDALGetFileList(hDS));
1688 :
1689 3084 : GDALClose(hDS);
1690 3084 : hDS = nullptr;
1691 :
1692 3084 : if (aosFileList.empty())
1693 : {
1694 0 : CPLError(CE_Failure, CPLE_NotSupported,
1695 : "Unable to determine files associated with %s, "
1696 : "delete fails.",
1697 : pszFilename);
1698 0 : return CE_Failure;
1699 : }
1700 :
1701 3084 : return Delete(nullptr, aosFileList.List());
1702 : }
1703 :
1704 : /************************************************************************/
1705 : /* Delete() */
1706 : /************************************************************************/
1707 :
1708 : /**
1709 : * \brief Delete a currently opened dataset
1710 : *
1711 : * The driver will attempt to delete the passed dataset in a driver specific
1712 : * fashion. Full featured drivers will delete all associated files,
1713 : * database objects, or whatever is appropriate. The default behavior when
1714 : * no driver specific behavior is provided is to attempt to delete all the
1715 : * files that are returned by GDALGetFileList() on the dataset handle.
1716 : *
1717 : * Note that this will generally not work on Windows systems that don't accept
1718 : * deleting opened files.
1719 : *
1720 : * At least one of poDS or papszFileList must not be NULL
1721 : *
1722 : * @param poDS dataset to delete, or NULL
1723 : * @param papszFileList File list to delete, typically obtained with
1724 : * poDS->GetFileList(), or NULL
1725 : *
1726 : * @return CE_None on success, or CE_Failure if the operation fails.
1727 : *
1728 : * @since 3.12
1729 : */
1730 :
1731 3095 : CPLErr GDALDriver::Delete(GDALDataset *poDS, CSLConstList papszFileList)
1732 :
1733 : {
1734 3095 : if (poDS)
1735 : {
1736 10 : pfnDelete = GetDeleteCallback();
1737 10 : if (pfnDelete != nullptr)
1738 6 : return pfnDelete(poDS->GetDescription());
1739 4 : else if (pfnDeleteDataSource != nullptr)
1740 0 : return pfnDeleteDataSource(this, poDS->GetDescription());
1741 : }
1742 :
1743 : /* -------------------------------------------------------------------- */
1744 : /* Delete all files. */
1745 : /* -------------------------------------------------------------------- */
1746 3089 : CPLErr eErr = CE_None;
1747 6833 : for (int i = 0; papszFileList && papszFileList[i]; ++i)
1748 : {
1749 3744 : if (VSIUnlink(papszFileList[i]) != 0)
1750 : {
1751 4 : CPLError(CE_Failure, CPLE_AppDefined, "Deleting %s failed:\n%s",
1752 2 : papszFileList[i], VSIStrerror(errno));
1753 2 : eErr = CE_Failure;
1754 : }
1755 : }
1756 :
1757 3089 : return eErr;
1758 : }
1759 :
1760 : /************************************************************************/
1761 : /* GDALDeleteDataset() */
1762 : /************************************************************************/
1763 :
1764 : /**
1765 : * \brief Delete named dataset.
1766 : *
1767 : * @see GDALDriver::Delete()
1768 : */
1769 :
1770 2391 : CPLErr CPL_STDCALL GDALDeleteDataset(GDALDriverH hDriver,
1771 : const char *pszFilename)
1772 :
1773 : {
1774 2391 : if (hDriver == nullptr)
1775 10 : hDriver = GDALIdentifyDriver(pszFilename, nullptr);
1776 :
1777 2391 : if (hDriver == nullptr)
1778 : {
1779 1 : CPLError(CE_Failure, CPLE_AppDefined, "No identifiable driver for %s.",
1780 : pszFilename);
1781 1 : return CE_Failure;
1782 : }
1783 :
1784 : #ifdef OGRAPISPY_ENABLED
1785 2390 : if (GDALGetMetadataItem(hDriver, GDAL_DCAP_VECTOR, nullptr))
1786 : {
1787 453 : OGRAPISpyDeleteDataSource(hDriver, pszFilename);
1788 : }
1789 : #endif
1790 :
1791 2390 : return GDALDriver::FromHandle(hDriver)->Delete(pszFilename);
1792 : }
1793 :
1794 : /************************************************************************/
1795 : /* DefaultRename() */
1796 : /* */
1797 : /* The generic implementation based on the file list used when */
1798 : /* there is no format specific implementation. */
1799 : /************************************************************************/
1800 :
1801 : //! @cond Doxygen_Suppress
1802 175 : CPLErr GDALDriver::DefaultRename(const char *pszNewName, const char *pszOldName)
1803 :
1804 : {
1805 : /* -------------------------------------------------------------------- */
1806 : /* Collect file list. */
1807 : /* -------------------------------------------------------------------- */
1808 : auto poDS = std::unique_ptr<GDALDataset>(
1809 : GDALDataset::Open(pszOldName, GDAL_OF_ALL | GDAL_OF_VERBOSE_ERROR,
1810 350 : nullptr, nullptr, nullptr));
1811 :
1812 175 : if (!poDS)
1813 : {
1814 0 : if (CPLGetLastErrorNo() == 0)
1815 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1816 : "Unable to open %s to obtain file list.", pszOldName);
1817 :
1818 0 : return CE_Failure;
1819 : }
1820 :
1821 350 : const CPLStringList aosFileList(poDS->GetFileList());
1822 :
1823 175 : poDS.reset();
1824 :
1825 175 : if (aosFileList.empty())
1826 : {
1827 0 : CPLError(CE_Failure, CPLE_NotSupported,
1828 : "Unable to determine files associated with %s,\n"
1829 : "rename fails.",
1830 : pszOldName);
1831 :
1832 0 : return CE_Failure;
1833 : }
1834 :
1835 : /* -------------------------------------------------------------------- */
1836 : /* Produce a list of new filenames that correspond to the old */
1837 : /* names. */
1838 : /* -------------------------------------------------------------------- */
1839 175 : CPLErr eErr = CE_None;
1840 : const CPLStringList aosNewFileList(
1841 350 : CPLCorrespondingPaths(pszOldName, pszNewName, aosFileList.List()));
1842 :
1843 175 : if (aosNewFileList.empty())
1844 0 : return CE_Failure;
1845 :
1846 : // Guaranteed by CPLCorrespondingPaths()
1847 175 : CPLAssert(aosNewFileList.size() == aosFileList.size());
1848 :
1849 : VSIStatBufL sStatBuf;
1850 177 : if (VSIStatL(pszOldName, &sStatBuf) == 0 && VSI_ISDIR(sStatBuf.st_mode) &&
1851 2 : VSIStatL(pszNewName, &sStatBuf) != 0)
1852 : {
1853 2 : if (VSIMkdirRecursive(pszNewName, 0755) != 0)
1854 : {
1855 1 : CPLError(CE_Failure, CPLE_AppDefined,
1856 : "Cannot create directory '%s'", pszNewName);
1857 1 : return CE_Failure;
1858 : }
1859 : }
1860 :
1861 358 : for (int i = 0; i < aosFileList.size(); ++i)
1862 : {
1863 184 : if (CPLMoveFile(aosNewFileList[i], aosFileList[i]) != 0)
1864 : {
1865 : // Above method will have emitted an error in case of failure.
1866 0 : eErr = CE_Failure;
1867 : // Try to put the ones we moved back.
1868 0 : for (--i; i >= 0; i--)
1869 : {
1870 : // Nothing we can do if the moving back doesn't work...
1871 0 : CPL_IGNORE_RET_VAL(
1872 0 : CPLMoveFile(aosFileList[i], aosNewFileList[i]));
1873 : }
1874 0 : break;
1875 : }
1876 : }
1877 :
1878 174 : return eErr;
1879 : }
1880 :
1881 : //! @endcond
1882 :
1883 : /************************************************************************/
1884 : /* Rename() */
1885 : /************************************************************************/
1886 :
1887 : /**
1888 : * \brief Rename a dataset.
1889 : *
1890 : * Rename a dataset. This may including moving the dataset to a new directory
1891 : * or even a new filesystem.
1892 : *
1893 : * It is unwise to have open dataset handles on this dataset when it is
1894 : * being renamed.
1895 : *
1896 : * Equivalent of the C function GDALRenameDataset().
1897 : *
1898 : * @param pszNewName new name for the dataset.
1899 : * @param pszOldName old name for the dataset.
1900 : *
1901 : * @return CE_None on success, or CE_Failure if the operation fails.
1902 : */
1903 :
1904 177 : CPLErr GDALDriver::Rename(const char *pszNewName, const char *pszOldName)
1905 :
1906 : {
1907 177 : pfnRename = GetRenameCallback();
1908 177 : if (pfnRename != nullptr)
1909 3 : return pfnRename(pszNewName, pszOldName);
1910 :
1911 174 : return DefaultRename(pszNewName, pszOldName);
1912 : }
1913 :
1914 : /************************************************************************/
1915 : /* GDALRenameDataset() */
1916 : /************************************************************************/
1917 :
1918 : /**
1919 : * \brief Rename a dataset.
1920 : *
1921 : * @see GDALDriver::Rename()
1922 : */
1923 :
1924 178 : CPLErr CPL_STDCALL GDALRenameDataset(GDALDriverH hDriver,
1925 : const char *pszNewName,
1926 : const char *pszOldName)
1927 :
1928 : {
1929 178 : if (hDriver == nullptr)
1930 5 : hDriver = GDALIdentifyDriver(pszOldName, nullptr);
1931 :
1932 178 : if (hDriver == nullptr)
1933 : {
1934 1 : CPLError(CE_Failure, CPLE_AppDefined, "No identifiable driver for %s.",
1935 : pszOldName);
1936 1 : return CE_Failure;
1937 : }
1938 :
1939 177 : return GDALDriver::FromHandle(hDriver)->Rename(pszNewName, pszOldName);
1940 : }
1941 :
1942 : /************************************************************************/
1943 : /* DefaultCopyFiles() */
1944 : /* */
1945 : /* The default implementation based on file lists used when */
1946 : /* there is no format specific implementation. */
1947 : /************************************************************************/
1948 :
1949 : //! @cond Doxygen_Suppress
1950 12 : CPLErr GDALDriver::DefaultCopyFiles(const char *pszNewName,
1951 : const char *pszOldName)
1952 :
1953 : {
1954 : /* -------------------------------------------------------------------- */
1955 : /* Collect file list. */
1956 : /* -------------------------------------------------------------------- */
1957 : auto poDS = std::unique_ptr<GDALDataset>(
1958 : GDALDataset::Open(pszOldName, GDAL_OF_ALL | GDAL_OF_VERBOSE_ERROR,
1959 24 : nullptr, nullptr, nullptr));
1960 :
1961 12 : if (!poDS)
1962 : {
1963 0 : if (CPLGetLastErrorNo() == 0)
1964 0 : CPLError(CE_Failure, CPLE_OpenFailed,
1965 : "Unable to open %s to obtain file list.", pszOldName);
1966 :
1967 0 : return CE_Failure;
1968 : }
1969 :
1970 24 : const CPLStringList aosFileList(poDS->GetFileList());
1971 :
1972 12 : poDS.reset();
1973 :
1974 12 : if (aosFileList.empty())
1975 : {
1976 0 : CPLError(CE_Failure, CPLE_NotSupported,
1977 : "Unable to determine files associated with %s,\n"
1978 : "copy fails.",
1979 : pszOldName);
1980 :
1981 0 : return CE_Failure;
1982 : }
1983 :
1984 : /* -------------------------------------------------------------------- */
1985 : /* Produce a list of new filenames that correspond to the old */
1986 : /* names. */
1987 : /* -------------------------------------------------------------------- */
1988 12 : CPLErr eErr = CE_None;
1989 : const CPLStringList aosNewFileList(
1990 24 : CPLCorrespondingPaths(pszOldName, pszNewName, aosFileList.List()));
1991 :
1992 12 : if (aosNewFileList.empty())
1993 0 : return CE_Failure;
1994 :
1995 : // Guaranteed by CPLCorrespondingPaths()
1996 12 : CPLAssert(aosNewFileList.size() == aosFileList.size());
1997 :
1998 : VSIStatBufL sStatBuf;
1999 14 : if (VSIStatL(pszOldName, &sStatBuf) == 0 && VSI_ISDIR(sStatBuf.st_mode) &&
2000 2 : VSIStatL(pszNewName, &sStatBuf) != 0)
2001 : {
2002 2 : if (VSIMkdirRecursive(pszNewName, 0755) != 0)
2003 : {
2004 1 : CPLError(CE_Failure, CPLE_AppDefined,
2005 : "Cannot create directory '%s'", pszNewName);
2006 1 : return CE_Failure;
2007 : }
2008 : }
2009 :
2010 33 : for (int i = 0; i < aosFileList.size(); ++i)
2011 : {
2012 22 : if (CPLCopyFile(aosNewFileList[i], aosFileList[i]) != 0)
2013 : {
2014 : // Above method will have emitted an error in case of failure.
2015 0 : eErr = CE_Failure;
2016 : // Try to put the ones we moved back.
2017 0 : for (--i; i >= 0; --i)
2018 : {
2019 0 : if (VSIUnlink(aosNewFileList[i]) != 0)
2020 : {
2021 0 : CPLError(CE_Warning, CPLE_AppDefined, "Cannot delete '%s'",
2022 : aosNewFileList[i]);
2023 : }
2024 : }
2025 0 : break;
2026 : }
2027 : }
2028 :
2029 11 : return eErr;
2030 : }
2031 :
2032 : //! @endcond
2033 :
2034 : /************************************************************************/
2035 : /* CopyFiles() */
2036 : /************************************************************************/
2037 :
2038 : /**
2039 : * \brief Copy the files of a dataset.
2040 : *
2041 : * Copy all the files associated with a dataset.
2042 : *
2043 : * Equivalent of the C function GDALCopyDatasetFiles().
2044 : *
2045 : * @param pszNewName new name for the dataset.
2046 : * @param pszOldName old name for the dataset.
2047 : *
2048 : * @return CE_None on success, or CE_Failure if the operation fails.
2049 : */
2050 :
2051 14 : CPLErr GDALDriver::CopyFiles(const char *pszNewName, const char *pszOldName)
2052 :
2053 : {
2054 14 : pfnCopyFiles = GetCopyFilesCallback();
2055 14 : if (pfnCopyFiles != nullptr)
2056 3 : return pfnCopyFiles(pszNewName, pszOldName);
2057 :
2058 11 : return DefaultCopyFiles(pszNewName, pszOldName);
2059 : }
2060 :
2061 : /************************************************************************/
2062 : /* GDALCopyDatasetFiles() */
2063 : /************************************************************************/
2064 :
2065 : /**
2066 : * \brief Copy the files of a dataset.
2067 : *
2068 : * @see GDALDriver::CopyFiles()
2069 : */
2070 :
2071 15 : CPLErr CPL_STDCALL GDALCopyDatasetFiles(GDALDriverH hDriver,
2072 : const char *pszNewName,
2073 : const char *pszOldName)
2074 :
2075 : {
2076 15 : if (hDriver == nullptr)
2077 10 : hDriver = GDALIdentifyDriver(pszOldName, nullptr);
2078 :
2079 15 : if (hDriver == nullptr)
2080 : {
2081 1 : CPLError(CE_Failure, CPLE_AppDefined, "No identifiable driver for %s.",
2082 : pszOldName);
2083 1 : return CE_Failure;
2084 : }
2085 :
2086 14 : return GDALDriver::FromHandle(hDriver)->CopyFiles(pszNewName, pszOldName);
2087 : }
2088 :
2089 : /************************************************************************/
2090 : /* GDALDriverHasOpenOption() */
2091 : /************************************************************************/
2092 :
2093 : /**
2094 : * \brief Returns TRUE if the given open option is supported by the driver.
2095 : * @param hDriver the handle of the driver
2096 : * @param pszOpenOptionName name of the open option to be checked
2097 : * @return TRUE if the driver supports the open option
2098 : * @since GDAL 3.11
2099 : */
2100 2 : bool GDALDriverHasOpenOption(GDALDriverH hDriver, const char *pszOpenOptionName)
2101 : {
2102 2 : VALIDATE_POINTER1(hDriver, "GDALDriverHasOpenOption", false);
2103 2 : return GDALDriver::FromHandle(hDriver)->HasOpenOption(pszOpenOptionName);
2104 : }
2105 :
2106 : /************************************************************************/
2107 : /* GDALGetDriverShortName() */
2108 : /************************************************************************/
2109 :
2110 : /**
2111 : * \brief Return the short name of a driver
2112 : *
2113 : * This is the string that can be
2114 : * passed to the GDALGetDriverByName() function.
2115 : *
2116 : * For the GeoTIFF driver, this is "GTiff"
2117 : *
2118 : * @param hDriver the handle of the driver
2119 : * @return the short name of the driver. The
2120 : * returned string should not be freed and is owned by the driver.
2121 : */
2122 :
2123 8698610 : const char *CPL_STDCALL GDALGetDriverShortName(GDALDriverH hDriver)
2124 :
2125 : {
2126 8698610 : VALIDATE_POINTER1(hDriver, "GDALGetDriverShortName", nullptr);
2127 :
2128 8698610 : return GDALDriver::FromHandle(hDriver)->GetDescription();
2129 : }
2130 :
2131 : /************************************************************************/
2132 : /* GDALGetDriverLongName() */
2133 : /************************************************************************/
2134 :
2135 : /**
2136 : * \brief Return the long name of a driver
2137 : *
2138 : * For the GeoTIFF driver, this is "GeoTIFF"
2139 : *
2140 : * @param hDriver the handle of the driver
2141 : * @return the long name of the driver or empty string. The
2142 : * returned string should not be freed and is owned by the driver.
2143 : */
2144 :
2145 507 : const char *CPL_STDCALL GDALGetDriverLongName(GDALDriverH hDriver)
2146 :
2147 : {
2148 507 : VALIDATE_POINTER1(hDriver, "GDALGetDriverLongName", nullptr);
2149 :
2150 : const char *pszLongName =
2151 507 : GDALDriver::FromHandle(hDriver)->GetMetadataItem(GDAL_DMD_LONGNAME);
2152 :
2153 507 : if (pszLongName == nullptr)
2154 0 : return "";
2155 :
2156 507 : return pszLongName;
2157 : }
2158 :
2159 : /************************************************************************/
2160 : /* GDALGetDriverHelpTopic() */
2161 : /************************************************************************/
2162 :
2163 : /**
2164 : * \brief Return the URL to the help that describes the driver
2165 : *
2166 : * That URL is relative to the GDAL documentation directory.
2167 : *
2168 : * For the GeoTIFF driver, this is "frmt_gtiff.html"
2169 : *
2170 : * @param hDriver the handle of the driver
2171 : * @return the URL to the help that describes the driver or NULL. The
2172 : * returned string should not be freed and is owned by the driver.
2173 : */
2174 :
2175 0 : const char *CPL_STDCALL GDALGetDriverHelpTopic(GDALDriverH hDriver)
2176 :
2177 : {
2178 0 : VALIDATE_POINTER1(hDriver, "GDALGetDriverHelpTopic", nullptr);
2179 :
2180 0 : return GDALDriver::FromHandle(hDriver)->GetMetadataItem(GDAL_DMD_HELPTOPIC);
2181 : }
2182 :
2183 : /************************************************************************/
2184 : /* GDALGetDriverCreationOptionList() */
2185 : /************************************************************************/
2186 :
2187 : /**
2188 : * \brief Return the list of creation options of the driver
2189 : *
2190 : * Return the list of creation options of the driver used by Create() and
2191 : * CreateCopy() as an XML string
2192 : *
2193 : * @param hDriver the handle of the driver
2194 : * @return an XML string that describes the list of creation options or
2195 : * empty string. The returned string should not be freed and is
2196 : * owned by the driver.
2197 : */
2198 :
2199 0 : const char *CPL_STDCALL GDALGetDriverCreationOptionList(GDALDriverH hDriver)
2200 :
2201 : {
2202 0 : VALIDATE_POINTER1(hDriver, "GDALGetDriverCreationOptionList", nullptr);
2203 :
2204 : const char *pszOptionList =
2205 0 : GDALDriver::FromHandle(hDriver)->GetMetadataItem(
2206 0 : GDAL_DMD_CREATIONOPTIONLIST);
2207 :
2208 0 : if (pszOptionList == nullptr)
2209 0 : return "";
2210 :
2211 0 : return pszOptionList;
2212 : }
2213 :
2214 : /************************************************************************/
2215 : /* GDALValidateCreationOptions() */
2216 : /************************************************************************/
2217 :
2218 : /**
2219 : * \brief Validate the list of creation options that are handled by a driver
2220 : *
2221 : * This is a helper method primarily used by Create() and
2222 : * CreateCopy() to validate that the passed in list of creation options
2223 : * is compatible with the GDAL_DMD_CREATIONOPTIONLIST metadata item defined
2224 : * by some drivers. @see GDALGetDriverCreationOptionList()
2225 : *
2226 : * If the GDAL_DMD_CREATIONOPTIONLIST metadata item is not defined, this
2227 : * function will return TRUE. Otherwise it will check that the keys and values
2228 : * in the list of creation options are compatible with the capabilities declared
2229 : * by the GDAL_DMD_CREATIONOPTIONLIST metadata item. In case of incompatibility
2230 : * a (non fatal) warning will be emitted and FALSE will be returned.
2231 : *
2232 : * @param hDriver the handle of the driver with whom the lists of creation
2233 : * option must be validated
2234 : * @param papszCreationOptions the list of creation options. An array of
2235 : * strings, whose last element is a NULL pointer
2236 : * @return TRUE if the list of creation options is compatible with the Create()
2237 : * and CreateCopy() method of the driver, FALSE otherwise.
2238 : */
2239 :
2240 37150 : int CPL_STDCALL GDALValidateCreationOptions(GDALDriverH hDriver,
2241 : CSLConstList papszCreationOptions)
2242 : {
2243 37150 : VALIDATE_POINTER1(hDriver, "GDALValidateCreationOptions", FALSE);
2244 : const char *pszOptionList =
2245 37150 : GDALDriver::FromHandle(hDriver)->GetMetadataItem(
2246 37150 : GDAL_DMD_CREATIONOPTIONLIST);
2247 37150 : CPLString osDriver;
2248 : osDriver.Printf("driver %s",
2249 37150 : GDALDriver::FromHandle(hDriver)->GetDescription());
2250 37150 : bool bFoundOptionToRemove = false;
2251 37150 : constexpr const char *const apszExcludedOptions[] = {
2252 : "APPEND_SUBDATASET", "COPY_SRC_MDD", "SRC_MDD", "SKIP_HOLES"};
2253 54503 : for (const char *pszCO : cpl::Iterate(papszCreationOptions))
2254 : {
2255 87404 : for (const char *pszExcludedOptions : apszExcludedOptions)
2256 : {
2257 70051 : if (STARTS_WITH_CI(pszCO, pszExcludedOptions) &&
2258 204 : pszCO[strlen(pszExcludedOptions)] == '=')
2259 : {
2260 204 : bFoundOptionToRemove = true;
2261 204 : break;
2262 : }
2263 : }
2264 17557 : if (bFoundOptionToRemove)
2265 204 : break;
2266 : }
2267 37150 : CSLConstList papszOptionsToValidate = papszCreationOptions;
2268 37150 : char **papszOptionsToFree = nullptr;
2269 37150 : if (bFoundOptionToRemove)
2270 : {
2271 626 : for (const char *pszCO : cpl::Iterate(papszCreationOptions))
2272 : {
2273 422 : bool bMatch = false;
2274 1709 : for (const char *pszExcludedOptions : apszExcludedOptions)
2275 : {
2276 1504 : if (STARTS_WITH_CI(pszCO, pszExcludedOptions) &&
2277 217 : pszCO[strlen(pszExcludedOptions)] == '=')
2278 : {
2279 217 : bMatch = true;
2280 217 : break;
2281 : }
2282 : }
2283 422 : if (!bMatch)
2284 205 : papszOptionsToFree = CSLAddString(papszOptionsToFree, pszCO);
2285 : }
2286 204 : papszOptionsToValidate = papszOptionsToFree;
2287 : }
2288 :
2289 37150 : const bool bRet = CPL_TO_BOOL(GDALValidateOptions(
2290 : pszOptionList, papszOptionsToValidate, "creation option", osDriver));
2291 37150 : CSLDestroy(papszOptionsToFree);
2292 37150 : return bRet;
2293 : }
2294 :
2295 : /************************************************************************/
2296 : /* GDALValidateOpenOptions() */
2297 : /************************************************************************/
2298 :
2299 63124 : int GDALValidateOpenOptions(GDALDriverH hDriver,
2300 : const char *const *papszOpenOptions)
2301 : {
2302 63124 : VALIDATE_POINTER1(hDriver, "GDALValidateOpenOptions", FALSE);
2303 : const char *pszOptionList =
2304 63124 : GDALDriver::FromHandle(hDriver)->GetMetadataItem(
2305 63124 : GDAL_DMD_OPENOPTIONLIST);
2306 126248 : CPLString osDriver;
2307 : osDriver.Printf("driver %s",
2308 63124 : GDALDriver::FromHandle(hDriver)->GetDescription());
2309 63124 : return GDALValidateOptions(pszOptionList, papszOpenOptions, "open option",
2310 63124 : osDriver);
2311 : }
2312 :
2313 : /************************************************************************/
2314 : /* GDALValidateOptions() */
2315 : /************************************************************************/
2316 :
2317 114587 : int GDALValidateOptions(const char *pszOptionList,
2318 : const char *const *papszOptionsToValidate,
2319 : const char *pszErrorMessageOptionType,
2320 : const char *pszErrorMessageContainerName)
2321 : {
2322 114587 : if (papszOptionsToValidate == nullptr || *papszOptionsToValidate == nullptr)
2323 96949 : return TRUE;
2324 17638 : if (pszOptionList == nullptr)
2325 181 : return TRUE;
2326 :
2327 17457 : CPLXMLNode *psNode = CPLParseXMLString(pszOptionList);
2328 17457 : if (psNode == nullptr)
2329 : {
2330 0 : CPLError(CE_Warning, CPLE_AppDefined,
2331 : "Could not parse %s list of %s. Assuming options are valid.",
2332 : pszErrorMessageOptionType, pszErrorMessageContainerName);
2333 0 : return TRUE;
2334 : }
2335 :
2336 17457 : bool bRet = true;
2337 47771 : while (*papszOptionsToValidate)
2338 : {
2339 30314 : char *pszKey = nullptr;
2340 : const char *pszValue =
2341 30314 : CPLParseNameValue(*papszOptionsToValidate, &pszKey);
2342 30314 : if (pszKey == nullptr)
2343 : {
2344 1 : CPLError(CE_Warning, CPLE_NotSupported,
2345 : "%s '%s' is not formatted with the key=value format",
2346 : pszErrorMessageOptionType, *papszOptionsToValidate);
2347 1 : bRet = false;
2348 :
2349 1 : ++papszOptionsToValidate;
2350 2316 : continue;
2351 : }
2352 :
2353 30313 : if (EQUAL(pszKey, "VALIDATE_OPEN_OPTIONS"))
2354 : {
2355 0 : ++papszOptionsToValidate;
2356 0 : CPLFree(pszKey);
2357 0 : continue;
2358 : }
2359 :
2360 : // Must we be forgiving in case of missing option ?
2361 30313 : bool bWarnIfMissingKey = true;
2362 30313 : if (pszKey[0] == '@')
2363 : {
2364 2298 : bWarnIfMissingKey = false;
2365 2298 : memmove(pszKey, pszKey + 1, strlen(pszKey + 1) + 1);
2366 : }
2367 :
2368 30313 : CPLXMLNode *psChildNode = psNode->psChild;
2369 304904 : while (psChildNode)
2370 : {
2371 302589 : if (EQUAL(psChildNode->pszValue, "OPTION"))
2372 : {
2373 : const char *pszOptionName =
2374 302589 : CPLGetXMLValue(psChildNode, "name", "");
2375 : /* For option names terminated by wildcard (NITF BLOCKA option
2376 : * names for example) */
2377 302589 : if (strlen(pszOptionName) > 0 &&
2378 302589 : pszOptionName[strlen(pszOptionName) - 1] == '*' &&
2379 1288 : EQUALN(pszOptionName, pszKey, strlen(pszOptionName) - 1))
2380 : {
2381 216 : break;
2382 : }
2383 :
2384 : /* For option names beginning by a wildcard */
2385 302373 : if (pszOptionName[0] == '*' &&
2386 57 : strlen(pszKey) > strlen(pszOptionName) &&
2387 9 : EQUAL(pszKey + strlen(pszKey) - strlen(pszOptionName + 1),
2388 : pszOptionName + 1))
2389 : {
2390 2 : break;
2391 : }
2392 :
2393 : // For options names with * in the middle
2394 302371 : const char *pszStarInOptionName = strchr(pszOptionName, '*');
2395 302371 : if (pszStarInOptionName &&
2396 1093 : pszStarInOptionName != pszOptionName &&
2397 : pszStarInOptionName !=
2398 1093 : pszOptionName + strlen(pszOptionName) - 1 &&
2399 21 : strlen(pszKey) > static_cast<size_t>(pszStarInOptionName -
2400 12 : pszOptionName) &&
2401 12 : EQUALN(pszKey, pszOptionName,
2402 : static_cast<size_t>(pszStarInOptionName -
2403 12 : pszOptionName)) &&
2404 12 : EQUAL(pszKey +
2405 : static_cast<size_t>(pszStarInOptionName -
2406 : pszOptionName) +
2407 : 1,
2408 : pszStarInOptionName + 1))
2409 : {
2410 6 : break;
2411 : }
2412 :
2413 302365 : if (EQUAL(pszOptionName, pszKey))
2414 : {
2415 27662 : break;
2416 : }
2417 : const char *pszAlias =
2418 274703 : CPLGetXMLValue(psChildNode, "alias", nullptr);
2419 : const char *pszDeprecatedAlias =
2420 274703 : pszAlias ? nullptr
2421 273788 : : CPLGetXMLValue(psChildNode, "deprecated_alias",
2422 274703 : nullptr);
2423 274703 : if (!pszAlias && pszDeprecatedAlias)
2424 151 : pszAlias = pszDeprecatedAlias;
2425 274703 : if (pszAlias && EQUAL(pszAlias, pszKey))
2426 : {
2427 112 : if (pszDeprecatedAlias)
2428 : {
2429 0 : CPLDebug(
2430 : "GDAL",
2431 : "Using deprecated alias '%s'. New name is '%s'",
2432 : pszAlias, pszOptionName);
2433 : }
2434 112 : break;
2435 : }
2436 : }
2437 274591 : psChildNode = psChildNode->psNext;
2438 : }
2439 30313 : if (psChildNode == nullptr)
2440 : {
2441 2339 : if (bWarnIfMissingKey &&
2442 24 : (!EQUAL(pszErrorMessageOptionType, "open option") ||
2443 2 : CPLFetchBool(papszOptionsToValidate, "VALIDATE_OPEN_OPTIONS",
2444 : true)))
2445 : {
2446 24 : CPLError(CE_Warning, CPLE_NotSupported,
2447 : "%s does not support %s %s",
2448 : pszErrorMessageContainerName,
2449 : pszErrorMessageOptionType, pszKey);
2450 24 : bRet = false;
2451 : }
2452 :
2453 2315 : CPLFree(pszKey);
2454 2315 : ++papszOptionsToValidate;
2455 2315 : continue;
2456 : }
2457 :
2458 : #ifdef DEBUG
2459 27998 : CPLXMLNode *psChildSubNode = psChildNode->psChild;
2460 166260 : while (psChildSubNode)
2461 : {
2462 138262 : if (psChildSubNode->eType == CXT_Attribute)
2463 : {
2464 96809 : if (!(EQUAL(psChildSubNode->pszValue, "name") ||
2465 68811 : EQUAL(psChildSubNode->pszValue, "alias") ||
2466 68618 : EQUAL(psChildSubNode->pszValue, "deprecated_alias") ||
2467 68531 : EQUAL(psChildSubNode->pszValue, "alt_config_option") ||
2468 68511 : EQUAL(psChildSubNode->pszValue, "description") ||
2469 45001 : EQUAL(psChildSubNode->pszValue, "type") ||
2470 17003 : EQUAL(psChildSubNode->pszValue, "min") ||
2471 16686 : EQUAL(psChildSubNode->pszValue, "max") ||
2472 16301 : EQUAL(psChildSubNode->pszValue, "default") ||
2473 1029 : EQUAL(psChildSubNode->pszValue, "maxsize") ||
2474 999 : EQUAL(psChildSubNode->pszValue, "required") ||
2475 944 : EQUAL(psChildSubNode->pszValue, "scope")))
2476 : {
2477 : /* Driver error */
2478 0 : CPLError(CE_Warning, CPLE_NotSupported,
2479 : "%s : unhandled attribute '%s' for %s %s.",
2480 : pszErrorMessageContainerName,
2481 : psChildSubNode->pszValue, pszKey,
2482 : pszErrorMessageOptionType);
2483 : }
2484 : }
2485 138262 : psChildSubNode = psChildSubNode->psNext;
2486 : }
2487 : #endif
2488 :
2489 27998 : const char *pszType = CPLGetXMLValue(psChildNode, "type", nullptr);
2490 27998 : const char *pszMin = CPLGetXMLValue(psChildNode, "min", nullptr);
2491 27998 : const char *pszMax = CPLGetXMLValue(psChildNode, "max", nullptr);
2492 27998 : if (pszType != nullptr)
2493 : {
2494 27998 : if (EQUAL(pszType, "INT") || EQUAL(pszType, "INTEGER"))
2495 : {
2496 8769 : const char *pszValueIter = pszValue;
2497 22469 : while (*pszValueIter)
2498 : {
2499 13750 : if (!((*pszValueIter >= '0' && *pszValueIter <= '9') ||
2500 69 : *pszValueIter == '+' || *pszValueIter == '-'))
2501 : {
2502 50 : CPLError(CE_Warning, CPLE_NotSupported,
2503 : "'%s' is an unexpected value for %s %s of "
2504 : "type int.",
2505 : pszValue, pszKey, pszErrorMessageOptionType);
2506 50 : bRet = false;
2507 50 : break;
2508 : }
2509 13700 : ++pszValueIter;
2510 : }
2511 8769 : if (*pszValueIter == '\0')
2512 : {
2513 8719 : if (pszMin && atoi(pszValue) < atoi(pszMin))
2514 : {
2515 10 : CPLError(CE_Warning, CPLE_NotSupported,
2516 : "'%s' is an unexpected value for %s %s that "
2517 : "should be >= %s.",
2518 : pszValue, pszKey, pszErrorMessageOptionType,
2519 : pszMin);
2520 10 : bRet = false;
2521 : }
2522 8719 : if (pszMax && atoi(pszValue) > atoi(pszMax))
2523 : {
2524 12 : CPLError(CE_Warning, CPLE_NotSupported,
2525 : "'%s' is an unexpected value for %s %s that "
2526 : "should be <= %s.",
2527 : pszValue, pszKey, pszErrorMessageOptionType,
2528 : pszMax);
2529 12 : bRet = false;
2530 : }
2531 8769 : }
2532 : }
2533 19229 : else if (EQUAL(pszType, "UNSIGNED INT"))
2534 : {
2535 3 : const char *pszValueIter = pszValue;
2536 10 : while (*pszValueIter)
2537 : {
2538 7 : if (!((*pszValueIter >= '0' && *pszValueIter <= '9') ||
2539 0 : *pszValueIter == '+'))
2540 : {
2541 0 : CPLError(CE_Warning, CPLE_NotSupported,
2542 : "'%s' is an unexpected value for %s %s of "
2543 : "type unsigned int.",
2544 : pszValue, pszKey, pszErrorMessageOptionType);
2545 0 : bRet = false;
2546 0 : break;
2547 : }
2548 7 : ++pszValueIter;
2549 : }
2550 3 : if (*pszValueIter == '\0')
2551 : {
2552 3 : if (pszMin && atoi(pszValue) < atoi(pszMin))
2553 : {
2554 0 : CPLError(CE_Warning, CPLE_NotSupported,
2555 : "'%s' is an unexpected value for %s %s that "
2556 : "should be >= %s.",
2557 : pszValue, pszKey, pszErrorMessageOptionType,
2558 : pszMin);
2559 0 : bRet = false;
2560 : }
2561 3 : if (pszMax && atoi(pszValue) > atoi(pszMax))
2562 : {
2563 0 : CPLError(CE_Warning, CPLE_NotSupported,
2564 : "'%s' is an unexpected value for %s %s that "
2565 : "should be <= %s.",
2566 : pszValue, pszKey, pszErrorMessageOptionType,
2567 : pszMax);
2568 0 : bRet = false;
2569 : }
2570 : }
2571 : }
2572 19226 : else if (EQUAL(pszType, "FLOAT"))
2573 : {
2574 767 : char *endPtr = nullptr;
2575 767 : double dfVal = CPLStrtod(pszValue, &endPtr);
2576 767 : if (!(endPtr == nullptr || *endPtr == '\0'))
2577 : {
2578 2 : CPLError(
2579 : CE_Warning, CPLE_NotSupported,
2580 : "'%s' is an unexpected value for %s %s of type float.",
2581 : pszValue, pszKey, pszErrorMessageOptionType);
2582 2 : bRet = false;
2583 : }
2584 : else
2585 : {
2586 765 : if (pszMin && dfVal < CPLAtof(pszMin))
2587 : {
2588 3 : CPLError(CE_Warning, CPLE_NotSupported,
2589 : "'%s' is an unexpected value for %s %s that "
2590 : "should be >= %s.",
2591 : pszValue, pszKey, pszErrorMessageOptionType,
2592 : pszMin);
2593 3 : bRet = false;
2594 : }
2595 765 : if (pszMax && dfVal > CPLAtof(pszMax))
2596 : {
2597 0 : CPLError(CE_Warning, CPLE_NotSupported,
2598 : "'%s' is an unexpected value for %s %s that "
2599 : "should be <= %s.",
2600 : pszValue, pszKey, pszErrorMessageOptionType,
2601 : pszMax);
2602 0 : bRet = false;
2603 : }
2604 : }
2605 : }
2606 18459 : else if (EQUAL(pszType, "BOOLEAN"))
2607 : {
2608 4098 : if (!(EQUAL(pszValue, "ON") || EQUAL(pszValue, "TRUE") ||
2609 4034 : EQUAL(pszValue, "YES") || EQUAL(pszValue, "OFF") ||
2610 487 : EQUAL(pszValue, "FALSE") || EQUAL(pszValue, "NO")))
2611 : {
2612 0 : CPLError(CE_Warning, CPLE_NotSupported,
2613 : "'%s' is an unexpected value for %s %s of type "
2614 : "boolean.",
2615 : pszValue, pszKey, pszErrorMessageOptionType);
2616 0 : bRet = false;
2617 : }
2618 : }
2619 14361 : else if (EQUAL(pszType, "STRING-SELECT"))
2620 : {
2621 7400 : bool bMatchFound = false;
2622 7400 : bool bOtherValuesElementFound = false;
2623 7400 : CPLXMLNode *psStringSelect = psChildNode->psChild;
2624 45534 : while (psStringSelect)
2625 : {
2626 45492 : if (psStringSelect->eType == CXT_Element &&
2627 21968 : EQUAL(psStringSelect->pszValue, "Value"))
2628 : {
2629 21968 : CPLXMLNode *psOptionNode = psStringSelect->psChild;
2630 37205 : while (psOptionNode)
2631 : {
2632 22595 : if (psOptionNode->eType == CXT_Text &&
2633 21849 : EQUAL(psOptionNode->pszValue, pszValue))
2634 : {
2635 7239 : bMatchFound = true;
2636 7239 : break;
2637 : }
2638 15356 : if (psOptionNode->eType == CXT_Attribute &&
2639 746 : (EQUAL(psOptionNode->pszValue, "alias") ||
2640 15 : EQUAL(psOptionNode->pszValue,
2641 731 : "deprecated_alias")) &&
2642 731 : EQUAL(psOptionNode->psChild->pszValue,
2643 : pszValue))
2644 : {
2645 119 : bMatchFound = true;
2646 119 : break;
2647 : }
2648 15237 : psOptionNode = psOptionNode->psNext;
2649 : }
2650 21968 : if (bMatchFound)
2651 21968 : break;
2652 : }
2653 23524 : else if (psStringSelect->eType == CXT_Element &&
2654 0 : EQUAL(psStringSelect->pszValue, "OtherValues"))
2655 : {
2656 0 : bOtherValuesElementFound = true;
2657 : }
2658 38134 : psStringSelect = psStringSelect->psNext;
2659 : }
2660 7400 : if (!bMatchFound && !bOtherValuesElementFound)
2661 : {
2662 42 : CPLError(CE_Warning, CPLE_NotSupported,
2663 : "'%s' is an unexpected value for %s %s of type "
2664 : "string-select.",
2665 : pszValue, pszKey, pszErrorMessageOptionType);
2666 42 : bRet = false;
2667 : }
2668 : }
2669 6961 : else if (EQUAL(pszType, "STRING"))
2670 : {
2671 : const char *pszMaxSize =
2672 6961 : CPLGetXMLValue(psChildNode, "maxsize", nullptr);
2673 6961 : if (pszMaxSize != nullptr)
2674 : {
2675 30 : if (static_cast<int>(strlen(pszValue)) > atoi(pszMaxSize))
2676 : {
2677 1 : CPLError(CE_Warning, CPLE_NotSupported,
2678 : "'%s' is of size %d, whereas maximum size for "
2679 : "%s %s is %d.",
2680 1 : pszValue, static_cast<int>(strlen(pszValue)),
2681 : pszKey, pszErrorMessageOptionType,
2682 : atoi(pszMaxSize));
2683 1 : bRet = false;
2684 : }
2685 : }
2686 : }
2687 : else
2688 : {
2689 : /* Driver error */
2690 0 : CPLError(CE_Warning, CPLE_NotSupported,
2691 : "%s : type '%s' for %s %s is not recognized.",
2692 : pszErrorMessageContainerName, pszType, pszKey,
2693 : pszErrorMessageOptionType);
2694 : }
2695 : }
2696 : else
2697 : {
2698 : /* Driver error */
2699 0 : CPLError(CE_Warning, CPLE_NotSupported, "%s : no type for %s %s.",
2700 : pszErrorMessageContainerName, pszKey,
2701 : pszErrorMessageOptionType);
2702 : }
2703 27998 : CPLFree(pszKey);
2704 27998 : ++papszOptionsToValidate;
2705 : }
2706 :
2707 17457 : CPLDestroyXMLNode(psNode);
2708 17457 : return bRet ? TRUE : FALSE;
2709 : }
2710 :
2711 : /************************************************************************/
2712 : /* GDALIdentifyDriver() */
2713 : /************************************************************************/
2714 :
2715 : /**
2716 : * \brief Identify the driver that can open a dataset.
2717 : *
2718 : * This function will try to identify the driver that can open the passed file
2719 : * name by invoking the Identify method of each registered GDALDriver in turn.
2720 : * The first driver that successfully identifies the file name will be returned.
2721 : * If all drivers fail then NULL is returned.
2722 : *
2723 : * In order to reduce the need for such searches to touch the operating system
2724 : * file system machinery, it is possible to give an optional list of files.
2725 : * This is the list of all files at the same level in the file system as the
2726 : * target file, including the target file. The filenames will not include any
2727 : * path components, and are essentially just the output of VSIReadDir() on the
2728 : * parent directory. If the target object does not have filesystem semantics
2729 : * then the file list should be NULL.
2730 : *
2731 : * @param pszFilename the name of the file to access. In the case of
2732 : * exotic drivers this may not refer to a physical file, but instead contain
2733 : * information for the driver on how to access a dataset.
2734 : *
2735 : * @param papszFileList an array of strings, whose last element is the NULL
2736 : * pointer. These strings are filenames that are auxiliary to the main
2737 : * filename. The passed value may be NULL.
2738 : *
2739 : * @return A GDALDriverH handle or NULL on failure. For C++ applications
2740 : * this handle can be cast to a GDALDriver *.
2741 : */
2742 :
2743 26664 : GDALDriverH CPL_STDCALL GDALIdentifyDriver(const char *pszFilename,
2744 : CSLConstList papszFileList)
2745 :
2746 : {
2747 26664 : return GDALIdentifyDriverEx(pszFilename, 0, nullptr, papszFileList);
2748 : }
2749 :
2750 : /************************************************************************/
2751 : /* GDALIdentifyDriverEx() */
2752 : /************************************************************************/
2753 :
2754 : /**
2755 : * \brief Identify the driver that can open a dataset.
2756 : *
2757 : * This function will try to identify the driver that can open the passed file
2758 : * name by invoking the Identify method of each registered GDALDriver in turn.
2759 : * The first driver that successfully identifies the file name will be returned.
2760 : * If all drivers fail then NULL is returned.
2761 : *
2762 : * In order to reduce the need for such searches to touch the operating system
2763 : * file system machinery, it is possible to give an optional list of files.
2764 : * This is the list of all files at the same level in the file system as the
2765 : * target file, including the target file. The filenames will not include any
2766 : * path components, and are essentially just the output of VSIReadDir() on the
2767 : * parent directory. If the target object does not have filesystem semantics
2768 : * then the file list should be NULL.
2769 : *
2770 : * @param pszFilename the name of the file to access. In the case of
2771 : * exotic drivers this may not refer to a physical file, but instead contain
2772 : * information for the driver on how to access a dataset.
2773 : *
2774 : * @param nIdentifyFlags a combination of GDAL_OF_RASTER for raster drivers
2775 : * or GDAL_OF_VECTOR for vector drivers. If none of the value is specified,
2776 : * both kinds are implied.
2777 : *
2778 : * @param papszAllowedDrivers NULL to consider all candidate drivers, or a NULL
2779 : * terminated list of strings with the driver short names that must be
2780 : * considered.
2781 : *
2782 : * @param papszFileList an array of strings, whose last element is the NULL
2783 : * pointer. These strings are filenames that are auxiliary to the main
2784 : * filename. The passed value may be NULL.
2785 : *
2786 : * @return A GDALDriverH handle or NULL on failure. For C++ applications
2787 : * this handle can be cast to a GDALDriver *.
2788 : */
2789 :
2790 26754 : GDALDriverH CPL_STDCALL GDALIdentifyDriverEx(
2791 : const char *pszFilename, unsigned int nIdentifyFlags,
2792 : const char *const *papszAllowedDrivers, const char *const *papszFileList)
2793 : {
2794 26754 : GDALDriverManager *poDM = GetGDALDriverManager();
2795 26754 : CPLAssert(nullptr != poDM);
2796 :
2797 : // If no driver kind is specified, assume all are to be probed.
2798 26754 : if ((nIdentifyFlags & GDAL_OF_KIND_MASK) == 0)
2799 26711 : nIdentifyFlags |= GDAL_OF_KIND_MASK & ~GDAL_OF_MULTIDIM_RASTER;
2800 :
2801 53508 : GDALOpenInfo oOpenInfo(pszFilename, nIdentifyFlags, papszFileList);
2802 26754 : oOpenInfo.papszAllowedDrivers = papszAllowedDrivers;
2803 :
2804 53508 : CPLErrorStateBackuper oBackuper;
2805 26754 : CPLErrorSetState(CE_None, CPLE_AppDefined, "");
2806 :
2807 26754 : const int nDriverCount = poDM->GetDriverCount();
2808 :
2809 : // First pass: only use drivers that have a pfnIdentify implementation.
2810 53508 : std::vector<GDALDriver *> apoSecondPassDrivers;
2811 5844510 : for (int iDriver = 0; iDriver < nDriverCount; ++iDriver)
2812 : {
2813 5819040 : GDALDriver *poDriver = poDM->GetDriver(iDriver);
2814 5826370 : if (papszAllowedDrivers != nullptr &&
2815 7328 : CSLFindString(papszAllowedDrivers,
2816 : GDALGetDriverShortName(poDriver)) == -1)
2817 : {
2818 1093810 : continue;
2819 : }
2820 :
2821 5813050 : VALIDATE_POINTER1(poDriver, "GDALIdentifyDriver", nullptr);
2822 :
2823 5811760 : if (poDriver->pfnIdentify == nullptr &&
2824 1082690 : poDriver->pfnIdentifyEx == nullptr)
2825 : {
2826 1082690 : continue;
2827 : }
2828 :
2829 4729120 : if (papszAllowedDrivers != nullptr &&
2830 44 : CSLFindString(papszAllowedDrivers,
2831 : GDALGetDriverShortName(poDriver)) == -1)
2832 0 : continue;
2833 14182400 : if ((nIdentifyFlags & GDAL_OF_RASTER) != 0 &&
2834 4729580 : (nIdentifyFlags & GDAL_OF_VECTOR) == 0 &&
2835 508 : poDriver->GetMetadataItem(GDAL_DCAP_RASTER) == nullptr)
2836 141 : continue;
2837 14190100 : if ((nIdentifyFlags & GDAL_OF_VECTOR) != 0 &&
2838 4733840 : (nIdentifyFlags & GDAL_OF_RASTER) == 0 &&
2839 4912 : poDriver->GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr)
2840 3693 : continue;
2841 :
2842 4725240 : if (poDriver->pfnIdentifyEx)
2843 : {
2844 0 : if (poDriver->pfnIdentifyEx(poDriver, &oOpenInfo) > 0)
2845 0 : return poDriver;
2846 : }
2847 : else
2848 : {
2849 4725240 : const int nIdentifyRes = poDriver->pfnIdentify(&oOpenInfo);
2850 4725240 : if (nIdentifyRes > 0)
2851 1287 : return poDriver;
2852 4750620 : if (nIdentifyRes < 0 &&
2853 26666 : poDriver->GetMetadataItem("IS_NON_LOADED_PLUGIN"))
2854 : {
2855 : // Not loaded plugin
2856 37 : apoSecondPassDrivers.push_back(poDriver);
2857 : }
2858 : }
2859 : }
2860 :
2861 : // second pass: try loading plugin drivers
2862 25501 : for (auto poDriver : apoSecondPassDrivers)
2863 : {
2864 : // Force plugin driver loading
2865 35 : poDriver->GetMetadata();
2866 35 : if (poDriver->pfnIdentify(&oOpenInfo) > 0)
2867 1 : return poDriver;
2868 : }
2869 :
2870 : // third pass: slow method.
2871 5715200 : for (int iDriver = 0; iDriver < nDriverCount; ++iDriver)
2872 : {
2873 5689970 : GDALDriver *poDriver = poDM->GetDriver(iDriver);
2874 5693790 : if (papszAllowedDrivers != nullptr &&
2875 3824 : CSLFindString(papszAllowedDrivers,
2876 : GDALGetDriverShortName(poDriver)) == -1)
2877 : {
2878 3807 : continue;
2879 : }
2880 :
2881 5686160 : VALIDATE_POINTER1(poDriver, "GDALIdentifyDriver", nullptr);
2882 :
2883 17057000 : if ((nIdentifyFlags & GDAL_OF_RASTER) != 0 &&
2884 5686610 : (nIdentifyFlags & GDAL_OF_VECTOR) == 0 &&
2885 448 : poDriver->GetMetadataItem(GDAL_DCAP_RASTER) == nullptr)
2886 137 : continue;
2887 17058700 : if ((nIdentifyFlags & GDAL_OF_VECTOR) != 0 &&
2888 5687590 : (nIdentifyFlags & GDAL_OF_RASTER) == 0 &&
2889 1570 : poDriver->GetMetadataItem(GDAL_DCAP_VECTOR) == nullptr)
2890 953 : continue;
2891 :
2892 5685070 : if (poDriver->pfnIdentifyEx != nullptr)
2893 : {
2894 0 : if (poDriver->pfnIdentifyEx(poDriver, &oOpenInfo) == 0)
2895 0 : continue;
2896 : }
2897 5685070 : else if (poDriver->pfnIdentify != nullptr)
2898 : {
2899 4625470 : if (poDriver->pfnIdentify(&oOpenInfo) == 0)
2900 4599430 : continue;
2901 : }
2902 :
2903 : GDALDataset *poDS;
2904 1085640 : if (poDriver->pfnOpen != nullptr)
2905 : {
2906 1034970 : poDS = poDriver->pfnOpen(&oOpenInfo);
2907 1034970 : if (poDS != nullptr)
2908 : {
2909 95 : delete poDS;
2910 95 : return GDALDriver::ToHandle(poDriver);
2911 : }
2912 :
2913 1034870 : if (CPLGetLastErrorType() != CE_None)
2914 136 : return nullptr;
2915 : }
2916 50671 : else if (poDriver->pfnOpenWithDriverArg != nullptr)
2917 : {
2918 0 : poDS = poDriver->pfnOpenWithDriverArg(poDriver, &oOpenInfo);
2919 0 : if (poDS != nullptr)
2920 : {
2921 0 : delete poDS;
2922 0 : return GDALDriver::ToHandle(poDriver);
2923 : }
2924 :
2925 0 : if (CPLGetLastErrorType() != CE_None)
2926 0 : return nullptr;
2927 : }
2928 : }
2929 :
2930 25235 : return nullptr;
2931 : }
2932 :
2933 : /************************************************************************/
2934 : /* GetMetadataItem() */
2935 : /************************************************************************/
2936 :
2937 12933100 : const char *GDALDriver::GetMetadataItem(const char *pszName,
2938 : const char *pszDomain)
2939 : {
2940 12933100 : if (pszDomain == nullptr || pszDomain[0] == '\0')
2941 : {
2942 12932900 : if (EQUAL(pszName, GDAL_DMD_OVERVIEW_CREATIONOPTIONLIST))
2943 : {
2944 2196 : const char *pszVal = GDALMajorObject::GetMetadataItem(pszName, "");
2945 2196 : if (pszVal)
2946 1974 : return pszVal;
2947 222 : if (GetMetadataItem(GDAL_DCAP_RASTER))
2948 : {
2949 153 : auto poDM = GetGDALDriverManager();
2950 153 : auto poGTiffDrv = poDM->GetDriverByName("GTiff");
2951 153 : if (poGTiffDrv)
2952 : {
2953 : const char *pszXML =
2954 153 : poGTiffDrv->GetMetadataItem(pszName, "");
2955 153 : if (pszXML)
2956 : {
2957 306 : CPLString osXML(pszXML);
2958 153 : osXML.replaceAll("<Value>INTERNAL</Value>", "");
2959 153 : return CPLSPrintf("%s", osXML.c_str());
2960 : }
2961 : }
2962 : }
2963 : }
2964 : }
2965 12931000 : return GDALMajorObject::GetMetadataItem(pszName, pszDomain);
2966 : }
2967 :
2968 : /************************************************************************/
2969 : /* SetMetadataItem() */
2970 : /************************************************************************/
2971 :
2972 4487120 : CPLErr GDALDriver::SetMetadataItem(const char *pszName, const char *pszValue,
2973 : const char *pszDomain)
2974 :
2975 : {
2976 4487120 : if (pszDomain == nullptr || pszDomain[0] == '\0')
2977 : {
2978 : /* Automatically sets GDAL_DMD_EXTENSIONS from GDAL_DMD_EXTENSION */
2979 4678120 : if (EQUAL(pszName, GDAL_DMD_EXTENSION) &&
2980 196364 : GDALMajorObject::GetMetadataItem(GDAL_DMD_EXTENSIONS) == nullptr)
2981 : {
2982 196364 : GDALMajorObject::SetMetadataItem(GDAL_DMD_EXTENSIONS, pszValue);
2983 : }
2984 : /* and vice-versa if there is a single extension in GDAL_DMD_EXTENSIONS */
2985 8651020 : else if (EQUAL(pszName, GDAL_DMD_EXTENSIONS) &&
2986 4294310 : strchr(pszValue, ' ') == nullptr &&
2987 8917 : GDALMajorObject::GetMetadataItem(GDAL_DMD_EXTENSION) ==
2988 : nullptr)
2989 : {
2990 8917 : GDALMajorObject::SetMetadataItem(GDAL_DMD_EXTENSION, pszValue);
2991 : }
2992 : }
2993 4487120 : return GDALMajorObject::SetMetadataItem(pszName, pszValue, pszDomain);
2994 : }
2995 :
2996 : /************************************************************************/
2997 : /* InstantiateAlgorithm() */
2998 : /************************************************************************/
2999 :
3000 : //! @cond Doxygen_Suppress
3001 :
3002 : GDALAlgorithm *
3003 213 : GDALDriver::InstantiateAlgorithm(const std::vector<std::string> &aosPath)
3004 : {
3005 213 : pfnInstantiateAlgorithm = GetInstantiateAlgorithmCallback();
3006 213 : if (pfnInstantiateAlgorithm)
3007 213 : return pfnInstantiateAlgorithm(aosPath);
3008 0 : return nullptr;
3009 : }
3010 :
3011 : /************************************************************************/
3012 : /* DeclareAlgorithm() */
3013 : /************************************************************************/
3014 :
3015 10741 : void GDALDriver::DeclareAlgorithm(const std::vector<std::string> &aosPath)
3016 : {
3017 21482 : const std::string osDriverName = GetDescription();
3018 10741 : auto &singleton = GDALGlobalAlgorithmRegistry::GetSingleton();
3019 :
3020 21482 : if (!singleton.HasDeclaredSubAlgorithm({"driver"}))
3021 : {
3022 3162 : singleton.DeclareAlgorithm(
3023 : {"driver"},
3024 74 : []() -> std::unique_ptr<GDALAlgorithm>
3025 : {
3026 148 : return std::make_unique<GDALContainerAlgorithm>(
3027 74 : "driver", "Command for driver specific operations.");
3028 1581 : });
3029 : }
3030 :
3031 : std::vector<std::string> path = {"driver",
3032 64446 : CPLString(osDriverName).tolower()};
3033 10741 : if (!singleton.HasDeclaredSubAlgorithm(path))
3034 : {
3035 434 : auto lambda = [osDriverName]() -> std::unique_ptr<GDALAlgorithm>
3036 : {
3037 : auto poDriver =
3038 434 : GetGDALDriverManager()->GetDriverByName(osDriverName.c_str());
3039 434 : if (poDriver)
3040 : {
3041 : const char *pszHelpTopic =
3042 434 : poDriver->GetMetadataItem(GDAL_DMD_HELPTOPIC);
3043 868 : return std::make_unique<GDALContainerAlgorithm>(
3044 868 : CPLString(osDriverName).tolower(),
3045 868 : std::string("Command for ")
3046 434 : .append(osDriverName)
3047 : .append(" driver specific operations."),
3048 868 : pszHelpTopic ? std::string("/").append(pszHelpTopic)
3049 434 : : std::string());
3050 : }
3051 0 : return nullptr;
3052 9486 : };
3053 9486 : singleton.DeclareAlgorithm(path, std::move(lambda));
3054 : }
3055 :
3056 10741 : path.insert(path.end(), aosPath.begin(), aosPath.end());
3057 :
3058 213 : auto lambda = [osDriverName, aosPath]() -> std::unique_ptr<GDALAlgorithm>
3059 : {
3060 : auto poDriver =
3061 213 : GetGDALDriverManager()->GetDriverByName(osDriverName.c_str());
3062 213 : if (poDriver)
3063 : return std::unique_ptr<GDALAlgorithm>(
3064 213 : poDriver->InstantiateAlgorithm(aosPath));
3065 0 : return nullptr;
3066 21482 : };
3067 :
3068 10741 : singleton.DeclareAlgorithm(path, std::move(lambda));
3069 :
3070 10741 : CPL_IGNORE_RET_VAL(osDriverName);
3071 10741 : }
3072 :
3073 : //! @endcond
3074 :
3075 : /************************************************************************/
3076 : /* DoesDriverHandleExtension() */
3077 : /************************************************************************/
3078 :
3079 190903 : static bool DoesDriverHandleExtension(GDALDriverH hDriver, const char *pszExt)
3080 : {
3081 190903 : bool bRet = false;
3082 : const char *pszDriverExtensions =
3083 190903 : GDALGetMetadataItem(hDriver, GDAL_DMD_EXTENSIONS, nullptr);
3084 190903 : if (pszDriverExtensions)
3085 : {
3086 319740 : const CPLStringList aosTokens(CSLTokenizeString(pszDriverExtensions));
3087 159870 : const int nTokens = aosTokens.size();
3088 358576 : for (int j = 0; j < nTokens; ++j)
3089 : {
3090 203294 : if (EQUAL(pszExt, aosTokens[j]))
3091 : {
3092 4588 : bRet = true;
3093 4588 : break;
3094 : }
3095 : }
3096 : }
3097 190903 : return bRet;
3098 : }
3099 :
3100 : /************************************************************************/
3101 : /* IsOnlyExpectedGDBDrivers() */
3102 : /************************************************************************/
3103 :
3104 1 : static bool IsOnlyExpectedGDBDrivers(const CPLStringList &aosDriverNames)
3105 : {
3106 3 : for (const char *pszDrvName : aosDriverNames)
3107 : {
3108 2 : if (!EQUAL(pszDrvName, "OpenFileGDB") &&
3109 1 : !EQUAL(pszDrvName, "FileGDB") && !EQUAL(pszDrvName, "GPSBabel"))
3110 : {
3111 0 : return false;
3112 : }
3113 : }
3114 1 : return true;
3115 : }
3116 :
3117 : /************************************************************************/
3118 : /* GDALGetOutputDriversForDatasetName() */
3119 : /************************************************************************/
3120 :
3121 : /** Return a list of driver short names that are likely candidates for the
3122 : * provided output file name.
3123 : *
3124 : * @param pszDestDataset Output dataset name (might not exist).
3125 : * @param nDatasetTypeFlag GDAL_OF_RASTER, GDAL_OF_VECTOR, GDAL_OF_MULTIDIM_RASTER
3126 : * or a binary-or'ed combination of them
3127 : * @param bSingleMatch Whether a single match is desired, that is to say the
3128 : * returned list will contain at most one item, which will
3129 : * be the first driver in the order they are registered to
3130 : * match the output dataset name. Note that in this mode, if
3131 : * nDatasetTypeFlag==GDAL_OF_RASTER and pszDestDataset has
3132 : * no extension, GTiff will be selected.
3133 : * @param bEmitWarning Whether a warning should be emitted when bSingleMatch is
3134 : * true and there are more than 2 candidates.
3135 : * @return NULL terminated list of driver short names.
3136 : * To be freed with CSLDestroy()
3137 : * @since 3.9
3138 : */
3139 2686 : char **GDALGetOutputDriversForDatasetName(const char *pszDestDataset,
3140 : int nDatasetTypeFlag,
3141 : bool bSingleMatch, bool bEmitWarning)
3142 : {
3143 5372 : CPLStringList aosDriverNames;
3144 5372 : CPLStringList aosMissingDriverNames;
3145 :
3146 5372 : std::string osExt = CPLGetExtensionSafe(pszDestDataset);
3147 2686 : if (EQUAL(osExt.c_str(), "zip"))
3148 : {
3149 2 : const CPLString osLower(CPLString(pszDestDataset).tolower());
3150 1 : if (osLower.endsWith(".shp.zip"))
3151 : {
3152 1 : osExt = "shp.zip";
3153 : }
3154 0 : else if (osLower.endsWith(".gpkg.zip"))
3155 : {
3156 0 : osExt = "gpkg.zip";
3157 : }
3158 : }
3159 2685 : else if (EQUAL(osExt.c_str(), "json"))
3160 : {
3161 1 : const CPLString osLower(CPLString(pszDestDataset).tolower());
3162 1 : if (osLower.endsWith(".gdalg.json"))
3163 0 : return nullptr;
3164 : }
3165 :
3166 2686 : auto poDM = GetGDALDriverManager();
3167 2686 : const int nDriverCount = poDM->GetDriverCount(true);
3168 2686 : GDALDriver *poMissingPluginDriver = nullptr;
3169 5372 : std::string osMatchingPrefix;
3170 606641 : for (int i = 0; i < nDriverCount; i++)
3171 : {
3172 603955 : GDALDriver *poDriver = poDM->GetDriver(i, true);
3173 603955 : bool bOk = false;
3174 603955 : if ((poDriver->GetMetadataItem(GDAL_DCAP_CREATE) != nullptr ||
3175 367590 : poDriver->GetMetadataItem(GDAL_DCAP_CREATECOPY) != nullptr ||
3176 1207910 : poDriver->GetMetadataItem(GDAL_DCAP_UPDATE) != nullptr) &&
3177 316945 : (((nDatasetTypeFlag & GDAL_OF_RASTER) &&
3178 254407 : poDriver->GetMetadataItem(GDAL_DCAP_RASTER) != nullptr) ||
3179 153090 : ((nDatasetTypeFlag & GDAL_OF_VECTOR) &&
3180 61948 : poDriver->GetMetadataItem(GDAL_DCAP_VECTOR) != nullptr) ||
3181 123692 : ((nDatasetTypeFlag & GDAL_OF_MULTIDIM_RASTER) &&
3182 590 : poDriver->GetMetadataItem(GDAL_DCAP_MULTIDIM_RASTER) != nullptr)))
3183 : {
3184 193303 : bOk = true;
3185 : }
3186 414974 : else if (poDriver->GetMetadataItem(GDAL_DCAP_VECTOR_TRANSLATE_FROM) &&
3187 4322 : (nDatasetTypeFlag & GDAL_OF_VECTOR) != 0)
3188 : {
3189 0 : bOk = true;
3190 : }
3191 603955 : if (bOk)
3192 : {
3193 384206 : if (!osExt.empty() &&
3194 190903 : DoesDriverHandleExtension(GDALDriver::ToHandle(poDriver),
3195 : osExt.c_str()))
3196 : {
3197 4588 : if (poDriver->GetMetadataItem("MISSING_PLUGIN_FILENAME"))
3198 : {
3199 0 : poMissingPluginDriver = poDriver;
3200 0 : aosMissingDriverNames.AddString(poDriver->GetDescription());
3201 : }
3202 : else
3203 4588 : aosDriverNames.AddString(poDriver->GetDescription());
3204 : }
3205 : else
3206 : {
3207 : const char *pszPrefix =
3208 188715 : poDriver->GetMetadataItem(GDAL_DMD_CONNECTION_PREFIX);
3209 188715 : if (pszPrefix && STARTS_WITH_CI(pszDestDataset, pszPrefix))
3210 : {
3211 9 : if (poDriver->GetMetadataItem("MISSING_PLUGIN_FILENAME"))
3212 : {
3213 0 : osMatchingPrefix = pszPrefix;
3214 0 : poMissingPluginDriver = poDriver;
3215 : aosMissingDriverNames.AddString(
3216 0 : poDriver->GetDescription());
3217 : }
3218 : else
3219 9 : aosDriverNames.AddString(poDriver->GetDescription());
3220 : }
3221 : }
3222 : }
3223 : }
3224 :
3225 : // GMT is registered before netCDF for opening reasons, but we want
3226 : // netCDF to be used by default for output.
3227 2693 : if (EQUAL(osExt.c_str(), "nc") && aosDriverNames.size() == 2 &&
3228 2693 : EQUAL(aosDriverNames[0], "GMT") && EQUAL(aosDriverNames[1], "netCDF"))
3229 : {
3230 0 : aosDriverNames.Clear();
3231 0 : aosDriverNames.AddString("netCDF");
3232 0 : aosDriverNames.AddString("GMT");
3233 : }
3234 :
3235 2686 : if (bSingleMatch)
3236 : {
3237 2614 : if (nDatasetTypeFlag == GDAL_OF_RASTER)
3238 : {
3239 2156 : if (aosDriverNames.empty())
3240 : {
3241 12 : if (osExt.empty())
3242 : {
3243 8 : aosDriverNames.AddString("GTiff");
3244 : }
3245 : }
3246 2144 : else if (aosDriverNames.size() >= 2)
3247 : {
3248 3894 : if (bEmitWarning && !(EQUAL(aosDriverNames[0], "GTiff") &&
3249 1946 : EQUAL(aosDriverNames[1], "COG")))
3250 : {
3251 2 : CPLError(CE_Warning, CPLE_AppDefined,
3252 : "Several drivers matching %s extension. Using %s",
3253 : osExt.c_str(), aosDriverNames[0]);
3254 : }
3255 3896 : const std::string osDrvName = aosDriverNames[0];
3256 1948 : aosDriverNames.Clear();
3257 1948 : aosDriverNames.AddString(osDrvName.c_str());
3258 : }
3259 : }
3260 459 : else if (EQUAL(osExt.c_str(), "gdb") &&
3261 1 : IsOnlyExpectedGDBDrivers(aosDriverNames))
3262 : {
3263 : // Do not warn about that case given that FileGDB write support
3264 : // forwards to OpenFileGDB one. And also consider GPSBabel as too
3265 : // marginal to deserve the warning.
3266 1 : aosDriverNames.Clear();
3267 1 : aosDriverNames.AddString("OpenFileGDB");
3268 : }
3269 457 : else if (aosDriverNames.size() >= 2)
3270 : {
3271 0 : if (bEmitWarning)
3272 : {
3273 0 : CPLError(CE_Warning, CPLE_AppDefined,
3274 : "Several drivers matching %s %s. Using %s",
3275 0 : osMatchingPrefix.empty() ? osExt.c_str()
3276 0 : : osMatchingPrefix.c_str(),
3277 0 : osMatchingPrefix.empty() ? "extension" : "prefix",
3278 : aosDriverNames[0]);
3279 : }
3280 0 : const std::string osDrvName = aosDriverNames[0];
3281 0 : aosDriverNames.Clear();
3282 0 : aosDriverNames.AddString(osDrvName.c_str());
3283 : }
3284 : }
3285 :
3286 2717 : if (aosDriverNames.empty() && bEmitWarning &&
3287 2717 : aosMissingDriverNames.size() == 1 && poMissingPluginDriver)
3288 : {
3289 0 : CPLError(CE_Failure, CPLE_AppDefined,
3290 : "No installed driver matching %s %s, but %s driver is "
3291 : "known. However plugin %s",
3292 0 : osMatchingPrefix.empty() ? osExt.c_str()
3293 0 : : osMatchingPrefix.c_str(),
3294 0 : osMatchingPrefix.empty() ? "extension" : "prefix",
3295 0 : poMissingPluginDriver->GetDescription(),
3296 0 : GDALGetMessageAboutMissingPluginDriver(poMissingPluginDriver)
3297 : .c_str());
3298 : }
3299 2716 : else if (aosDriverNames.empty() && bEmitWarning &&
3300 30 : aosMissingDriverNames.empty())
3301 : {
3302 827 : for (const auto &sConnectionPrefix : asKnownConnectionPrefixes)
3303 : {
3304 798 : if (STARTS_WITH_CI(pszDestDataset, sConnectionPrefix.pszPrefix))
3305 : {
3306 1 : CPLError(CE_Failure, CPLE_AppDefined,
3307 : "Filename %s starts with the connection prefix of "
3308 : "driver %s, which is not enabled in this GDAL build. "
3309 : "If that filename is really intended, explicitly "
3310 : "specify its output format.",
3311 1 : pszDestDataset, sConnectionPrefix.pszDriverName);
3312 1 : break;
3313 : }
3314 : }
3315 : }
3316 :
3317 2686 : return aosDriverNames.StealList();
3318 : }
3319 :
3320 : /************************************************************************/
3321 : /* GDALGetMessageAboutMissingPluginDriver() */
3322 : /************************************************************************/
3323 :
3324 : std::string
3325 0 : GDALGetMessageAboutMissingPluginDriver(GDALDriver *poMissingPluginDriver)
3326 : {
3327 : std::string osMsg =
3328 0 : poMissingPluginDriver->GetMetadataItem("MISSING_PLUGIN_FILENAME");
3329 : osMsg += " is not available in your "
3330 0 : "installation.";
3331 0 : if (const char *pszInstallationMsg = poMissingPluginDriver->GetMetadataItem(
3332 0 : GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE))
3333 : {
3334 0 : osMsg += " ";
3335 0 : osMsg += pszInstallationMsg;
3336 : }
3337 :
3338 : VSIStatBuf sStat;
3339 0 : if (const char *pszGDALDriverPath =
3340 0 : CPLGetConfigOption("GDAL_DRIVER_PATH", nullptr))
3341 : {
3342 0 : if (VSIStat(pszGDALDriverPath, &sStat) != 0)
3343 : {
3344 0 : if (osMsg.back() != '.')
3345 0 : osMsg += ".";
3346 0 : osMsg += " Directory '";
3347 0 : osMsg += pszGDALDriverPath;
3348 0 : osMsg += "' pointed by GDAL_DRIVER_PATH does not exist.";
3349 : }
3350 : }
3351 : else
3352 : {
3353 0 : if (osMsg.back() != '.')
3354 0 : osMsg += ".";
3355 : #ifdef INSTALL_PLUGIN_FULL_DIR
3356 : if (VSIStat(INSTALL_PLUGIN_FULL_DIR, &sStat) != 0)
3357 : {
3358 : osMsg += " Directory '";
3359 : osMsg += INSTALL_PLUGIN_FULL_DIR;
3360 : osMsg += "' hardcoded in the GDAL library does not "
3361 : "exist and the GDAL_DRIVER_PATH "
3362 : "configuration option is not set.";
3363 : }
3364 : else
3365 : #endif
3366 : {
3367 : osMsg += " The GDAL_DRIVER_PATH configuration "
3368 0 : "option is not set.";
3369 : }
3370 : }
3371 0 : return osMsg;
3372 : }
3373 :
3374 : /************************************************************************/
3375 : /* GDALClearMemoryCaches() */
3376 : /************************************************************************/
3377 :
3378 : /**
3379 : * \brief Clear all GDAL-controlled in-memory caches.
3380 : *
3381 : * Iterates registered drivers and calls their pfnClearCaches callback if set,
3382 : * then calls VSICurlClearCache() to clear /vsicurl/ and related caches.
3383 : *
3384 : * Note that neither the global raster block cache or caches specific to open
3385 : * dataset objects are not cleared by this function (in its current implementation).
3386 : *
3387 : * Useful when remote datasets may have changed during the lifetime of a
3388 : * process.
3389 : *
3390 : * @since GDAL 3.13
3391 : */
3392 4 : void GDALClearMemoryCaches()
3393 : {
3394 4 : auto *poDM = GetGDALDriverManager();
3395 4 : if (poDM)
3396 : {
3397 904 : for (int i = 0; i < poDM->GetDriverCount(); i++)
3398 : {
3399 900 : auto *poDriver = poDM->GetDriver(i);
3400 900 : if (poDriver && poDriver->pfnClearCaches)
3401 4 : poDriver->pfnClearCaches(poDriver);
3402 : }
3403 : }
3404 4 : VSICurlClearCache();
3405 4 : }
|