1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
/******************************************************************************
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Simple client for viewing OGR driver data.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 1999, Frank Warmerdam
* Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_version.h"
#include "gdal_priv.h"
#include "gdal_utils_priv.h"
#include "ogr_p.h"
#include "commonutils.h"
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
fprintf(stderr, "%s\n", GDALVectorInfoGetParserUsage().c_str());
exit(1);
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(argc, argv)
{
// Check strict compilation and runtime library version as we use C++ API.
if (!GDAL_CHECK_VERSION(argv[0]))
exit(1);
EarlySetConfigOptions(argc, argv);
OGRRegisterAll();
argc = OGRGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
exit(-argc);
auto psOptionsForBinary =
std::make_unique<GDALVectorInfoOptionsForBinary>();
GDALVectorInfoOptions *psOptions =
GDALVectorInfoOptionsNew(argv + 1, psOptionsForBinary.get());
if (psOptions == nullptr)
Usage();
/* -------------------------------------------------------------------- */
/* Open dataset. */
/* -------------------------------------------------------------------- */
#ifdef __AFL_HAVE_MANUAL_CONTROL
int iIter = 0;
while (__AFL_LOOP(1000))
{
iIter++;
#endif
/* --------------------------------------------------------------------
*/
/* Open data source. */
/* --------------------------------------------------------------------
*/
int nFlags = GDAL_OF_VECTOR;
bool bMayRetryUpdateMode = false;
if (psOptionsForBinary->bUpdate)
nFlags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR;
else if (psOptionsForBinary->bReadOnly)
nFlags |= GDAL_OF_READONLY | GDAL_OF_VERBOSE_ERROR;
else if (psOptionsForBinary->osSQLStatement.empty())
{
nFlags |= GDAL_OF_READONLY;
// GDALIdentifyDriverEx() might emit an error message, e.g.
// when opening "/vsizip/foo.zip/" and the zip has more than one
// file. Cf https://github.com/OSGeo/gdal/issues/9459
CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
if (GDALIdentifyDriverEx(
psOptionsForBinary->osFilename.c_str(), GDAL_OF_VECTOR,
psOptionsForBinary->aosAllowInputDrivers.List(), nullptr))
{
bMayRetryUpdateMode = true;
}
else
{
// And an error Will be emitted
nFlags |= GDAL_OF_VERBOSE_ERROR;
}
}
else
nFlags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR;
GDALDataset *poDS = GDALDataset::Open(
psOptionsForBinary->osFilename.c_str(), nFlags,
psOptionsForBinary->aosAllowInputDrivers.List(),
psOptionsForBinary->aosOpenOptions.List(), nullptr);
if (poDS == nullptr && !psOptionsForBinary->bReadOnly &&
!psOptionsForBinary->bUpdate)
{
if (psOptionsForBinary->osSQLStatement.empty() &&
bMayRetryUpdateMode)
{
// In some cases (empty geopackage for example), opening in
// read-only mode fails, so retry in update mode
poDS = GDALDataset::Open(
psOptionsForBinary->osFilename.c_str(),
GDAL_OF_UPDATE | GDAL_OF_VECTOR,
psOptionsForBinary->aosAllowInputDrivers.List(),
psOptionsForBinary->aosOpenOptions.List(), nullptr);
}
else if (!psOptionsForBinary->osSQLStatement.empty())
{
poDS = GDALDataset::Open(
psOptionsForBinary->osFilename.c_str(),
GDAL_OF_READONLY | GDAL_OF_VECTOR,
psOptionsForBinary->aosAllowInputDrivers.List(),
psOptionsForBinary->aosOpenOptions.List(), nullptr);
if (poDS != nullptr && psOptionsForBinary->bVerbose)
{
printf("Had to open data source read-only.\n");
#ifdef __AFL_HAVE_MANUAL_CONTROL
psOptionsForBinary->bReadOnly = true;
#endif
}
}
}
int nRet = 0;
if (poDS == nullptr)
{
nRet = 1;
VSIStatBuf sStat;
CPLString message;
message.Printf("ogrinfo failed - unable to open '%s'.",
psOptionsForBinary->osFilename.c_str());
if (VSIStat(psOptionsForBinary->osFilename.c_str(), &sStat) == 0)
{
GDALDriverH drv =
GDALIdentifyDriverEx(psOptionsForBinary->osFilename.c_str(),
GDAL_OF_RASTER, nullptr, nullptr);
if (drv)
{
message += " Did you intend to call gdalinfo?";
}
}
fprintf(stderr, "%s\n", message.c_str());
}
else
{
char *pszGDALVectorInfoOutput =
GDALVectorInfo(GDALDataset::ToHandle(poDS), psOptions);
if (pszGDALVectorInfoOutput)
printf("%s", pszGDALVectorInfoOutput);
else
nRet = 1;
CPLFree(pszGDALVectorInfoOutput);
}
delete poDS;
#ifdef __AFL_HAVE_MANUAL_CONTROL
}
#endif
GDALVectorInfoOptionsFree(psOptions);
CSLDestroy(argv);
GDALDumpOpenDatasets(stderr);
GDALDestroyDriverManager();
CPLDumpSharedList(nullptr);
GDALDestroy();
exit(nRet);
}
MAIN_END
|