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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
/******************************************************************************
*
* Project: GDAL Utilities
* Purpose: Command line application to list info about a file.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
* ****************************************************************************
* Copyright (c) 1998, Frank Warmerdam
* Copyright (c) 2007-2015, Even Rouault <even.rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdal_version.h"
#include "gdal.h"
#include "cpl_string.h"
#include "cpl_multiproc.h"
#include "cpl_vsi_virtual.h"
#include "commonutils.h"
#include "gdal_utils_priv.h"
/************************************************************************/
/* GDALExit() */
/* This function exits and cleans up GDAL and OGR resources */
/* Perhaps it should be added to C api and used in all apps? */
/************************************************************************/
static int GDALExit(int nCode)
{
const char *pszDebug = CPLGetConfigOption("CPL_DEBUG", nullptr);
if (pszDebug && (EQUAL(pszDebug, "ON") || EQUAL(pszDebug, "")))
{
GDALDumpOpenDatasets(stderr);
CPLDumpSharedList(nullptr);
}
GDALDestroyDriverManager();
OGRCleanupAll();
exit(nCode);
}
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage()
{
fprintf(stderr, "%s\n", GDALInfoAppGetParserUsage().c_str());
GDALExit(1);
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(argc, argv)
{
EarlySetConfigOptions(argc, argv);
/* -------------------------------------------------------------------- */
/* Register standard GDAL drivers, and process generic GDAL */
/* command options. */
/* -------------------------------------------------------------------- */
GDALAllRegister();
argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
GDALExit(-argc);
/* -------------------------------------------------------------------- */
/* Parse command line */
/* -------------------------------------------------------------------- */
GDALInfoOptionsForBinary sOptionsForBinary;
if (CSLFindString(argv, "-stdout") < 0)
{
argv = CSLAddString(argv, "-stdout");
}
std::unique_ptr<GDALInfoOptions, decltype(&GDALInfoOptionsFree)> psOptions{
GDALInfoOptionsNew(argv + 1, &sOptionsForBinary), GDALInfoOptionsFree};
CSLDestroy(argv);
if (!psOptions)
{
Usage();
}
/* -------------------------------------------------------------------- */
/* Open dataset. */
/* -------------------------------------------------------------------- */
#ifdef __AFL_HAVE_MANUAL_CONTROL
int iIter = 0;
while (__AFL_LOOP(1000))
{
iIter++;
#endif
GDALDatasetH hDataset = GDALOpenEx(
sOptionsForBinary.osFilename.c_str(),
GDAL_OF_READONLY | GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
sOptionsForBinary.aosAllowedInputDrivers,
sOptionsForBinary.aosOpenOptions, nullptr);
if (hDataset == nullptr)
{
#ifdef __AFL_HAVE_MANUAL_CONTROL
continue;
#else
VSIStatBuf sStat;
CPLString message;
message.Printf("gdalinfo failed - unable to open '%s'.",
sOptionsForBinary.osFilename.c_str());
if (VSIStat(sOptionsForBinary.osFilename.c_str(), &sStat) == 0)
{
GDALDriverH drv =
GDALIdentifyDriverEx(sOptionsForBinary.osFilename.c_str(),
GDAL_OF_VECTOR, nullptr, nullptr);
if (drv)
{
message += " Did you intend to call ogrinfo?";
}
}
fprintf(stderr, "%s\n", message.c_str());
/* --------------------------------------------------------------------
*/
/* If argument is a VSIFILE, then print its contents */
/* --------------------------------------------------------------------
*/
if (VSIFileManager::GetHandler(sOptionsForBinary.osFilename.c_str())
->IsArchive(sOptionsForBinary.osFilename.c_str()))
{
const char *const apszOptions[] = {"NAME_AND_TYPE_ONLY=YES",
nullptr};
VSIDIR *psDir = VSIOpenDir(sOptionsForBinary.osFilename.c_str(), -1,
apszOptions);
if (psDir)
{
fprintf(stdout,
"Unable to open source `%s' directly.\n"
"The archive contains several files:\n",
sOptionsForBinary.osFilename.c_str());
int nCount = 0;
while (auto psEntry = VSIGetNextDirEntry(psDir))
{
if (VSI_ISDIR(psEntry->nMode) && psEntry->pszName[0] &&
psEntry->pszName[strlen(psEntry->pszName) - 1] != '/')
{
fprintf(stdout, " %s/%s/\n",
sOptionsForBinary.osFilename.c_str(),
psEntry->pszName);
}
else
{
fprintf(stdout, " %s/%s\n",
sOptionsForBinary.osFilename.c_str(),
psEntry->pszName);
}
nCount++;
if (nCount == 100)
{
fprintf(stdout, "[...trimmed...]\n");
break;
}
}
VSICloseDir(psDir);
}
}
GDALDumpOpenDatasets(stderr);
GDALDestroyDriverManager();
CPLDumpSharedList(nullptr);
exit(1);
#endif
}
/* --------------------------------------------------------------------
*/
/* Read specified subdataset if requested. */
/* --------------------------------------------------------------------
*/
if (sOptionsForBinary.nSubdataset > 0)
{
char **papszSubdatasets = GDALGetMetadata(hDataset, "SUBDATASETS");
const int nSubdatasets = CSLCount(papszSubdatasets) / 2;
if (nSubdatasets > 0 &&
sOptionsForBinary.nSubdataset <= nSubdatasets)
{
char szKeyName[1024];
char *pszSubdatasetName;
snprintf(szKeyName, sizeof(szKeyName), "SUBDATASET_%d_NAME",
sOptionsForBinary.nSubdataset);
szKeyName[sizeof(szKeyName) - 1] = '\0';
pszSubdatasetName =
CPLStrdup(CSLFetchNameValue(papszSubdatasets, szKeyName));
GDALClose(hDataset);
hDataset = GDALOpen(pszSubdatasetName, GA_ReadOnly);
CPLFree(pszSubdatasetName);
}
else
{
fprintf(stderr,
"gdalinfo warning: subdataset %d of %d requested. "
"Reading the main dataset.\n",
sOptionsForBinary.nSubdataset, nSubdatasets);
}
}
char *pszGDALInfoOutput = GDALInfo(hDataset, psOptions.get());
if (pszGDALInfoOutput)
printf("%s", pszGDALInfoOutput);
CPLFree(pszGDALInfoOutput);
GDALClose(hDataset);
#ifdef __AFL_HAVE_MANUAL_CONTROL
}
#endif
GDALDumpOpenDatasets(stderr);
GDALDestroyDriverManager();
CPLDumpSharedList(nullptr);
GDALDestroy();
exit(0);
}
MAIN_END
|