LCOV - code coverage report
Current view: top level - apps - ogrinfo_bin.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 59 72 81.9 %
Date: 2024-05-03 15:49:35 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  OpenGIS Simple Features Reference Implementation
       4             :  * Purpose:  Simple client for viewing OGR driver data.
       5             :  * Author:   Frank Warmerdam, warmerdam@pobox.com
       6             :  *
       7             :  ******************************************************************************
       8             :  * Copyright (c) 1999, Frank Warmerdam
       9             :  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
      10             :  *
      11             :  * Permission is hereby granted, free of charge, to any person obtaining a
      12             :  * copy of this software and associated documentation files (the "Software"),
      13             :  * to deal in the Software without restriction, including without limitation
      14             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15             :  * and/or sell copies of the Software, and to permit persons to whom the
      16             :  * Software is furnished to do so, subject to the following conditions:
      17             :  *
      18             :  * The above copyright notice and this permission notice shall be included
      19             :  * in all copies or substantial portions of the Software.
      20             :  *
      21             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22             :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24             :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25             :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26             :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27             :  * DEALINGS IN THE SOFTWARE.
      28             :  ****************************************************************************/
      29             : 
      30             : #include "cpl_port.h"
      31             : 
      32             : #include "gdal_version.h"
      33             : #include "gdal_priv.h"
      34             : #include "gdal_utils_priv.h"
      35             : #include "ogr_p.h"
      36             : 
      37             : #include "commonutils.h"
      38             : 
      39             : /************************************************************************/
      40             : /*                               Usage()                                */
      41             : /************************************************************************/
      42             : 
      43           0 : static void Usage()
      44             : {
      45           0 :     fprintf(stderr, "%s\n", GDALVectorInfoGetParserUsage().c_str());
      46           0 :     exit(1);
      47             : }
      48             : 
      49             : /************************************************************************/
      50             : /*                                main()                                */
      51             : /************************************************************************/
      52             : 
      53          57 : MAIN_START(argc, argv)
      54             : 
      55             : {
      56             :     // Check strict compilation and runtime library version as we use C++ API.
      57          57 :     if (!GDAL_CHECK_VERSION(argv[0]))
      58           0 :         exit(1);
      59             : 
      60          57 :     EarlySetConfigOptions(argc, argv);
      61             : 
      62          57 :     OGRRegisterAll();
      63             : 
      64          57 :     argc = OGRGeneralCmdLineProcessor(argc, &argv, 0);
      65          57 :     if (argc < 1)
      66          11 :         exit(-argc);
      67             : 
      68             :     auto psOptionsForBinary =
      69          46 :         std::make_unique<GDALVectorInfoOptionsForBinary>();
      70             : 
      71             :     GDALVectorInfoOptions *psOptions =
      72          46 :         GDALVectorInfoOptionsNew(argv + 1, psOptionsForBinary.get());
      73          43 :     if (psOptions == nullptr)
      74           0 :         Usage();
      75             : 
      76             : /* -------------------------------------------------------------------- */
      77             : /*      Open dataset.                                                   */
      78             : /* -------------------------------------------------------------------- */
      79             : #ifdef __AFL_HAVE_MANUAL_CONTROL
      80             :     int iIter = 0;
      81             :     while (__AFL_LOOP(1000))
      82             :     {
      83             :         iIter++;
      84             : #endif
      85             :         /* --------------------------------------------------------------------
      86             :          */
      87             :         /*      Open data source. */
      88             :         /* --------------------------------------------------------------------
      89             :          */
      90          43 :         int nFlags = GDAL_OF_VECTOR;
      91          43 :         bool bMayRetryUpdateMode = false;
      92          43 :         if (psOptionsForBinary->bUpdate)
      93           0 :             nFlags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR;
      94          43 :         else if (psOptionsForBinary->bReadOnly)
      95           7 :             nFlags |= GDAL_OF_READONLY | GDAL_OF_VERBOSE_ERROR;
      96          36 :         else if (psOptionsForBinary->osSQLStatement.empty())
      97             :         {
      98          33 :             nFlags |= GDAL_OF_READONLY;
      99             :             // GDALIdentifyDriverEx() might emit an error message, e.g.
     100             :             // when opening "/vsizip/foo.zip/" and the zip has more than one
     101             :             // file. Cf https://github.com/OSGeo/gdal/issues/9459
     102          66 :             CPLErrorHandlerPusher oErrorHandler(CPLQuietErrorHandler);
     103          33 :             if (GDALIdentifyDriverEx(
     104          33 :                     psOptionsForBinary->osFilename.c_str(), GDAL_OF_VECTOR,
     105          66 :                     psOptionsForBinary->aosAllowInputDrivers.List(), nullptr))
     106             :             {
     107          29 :                 bMayRetryUpdateMode = true;
     108             :             }
     109             :             else
     110             :             {
     111             :                 // And an error Will be emitted
     112           4 :                 nFlags |= GDAL_OF_VERBOSE_ERROR;
     113             :             }
     114             :         }
     115             :         else
     116           3 :             nFlags |= GDAL_OF_UPDATE | GDAL_OF_VERBOSE_ERROR;
     117          43 :         GDALDataset *poDS = GDALDataset::Open(
     118          43 :             psOptionsForBinary->osFilename.c_str(), nFlags,
     119          43 :             psOptionsForBinary->aosAllowInputDrivers.List(),
     120          43 :             psOptionsForBinary->aosOpenOptions.List(), nullptr);
     121             : 
     122          48 :         if (poDS == nullptr && !psOptionsForBinary->bReadOnly &&
     123           5 :             !psOptionsForBinary->bUpdate)
     124             :         {
     125           5 :             if (psOptionsForBinary->osSQLStatement.empty() &&
     126             :                 bMayRetryUpdateMode)
     127             :             {
     128             :                 // In some cases (empty geopackage for example), opening in
     129             :                 // read-only mode fails, so retry in update mode
     130           1 :                 poDS = GDALDataset::Open(
     131           1 :                     psOptionsForBinary->osFilename.c_str(),
     132             :                     GDAL_OF_UPDATE | GDAL_OF_VECTOR,
     133           1 :                     psOptionsForBinary->aosAllowInputDrivers.List(),
     134           1 :                     psOptionsForBinary->aosOpenOptions.List(), nullptr);
     135             :             }
     136           4 :             else if (!psOptionsForBinary->osSQLStatement.empty())
     137             :             {
     138           0 :                 poDS = GDALDataset::Open(
     139           0 :                     psOptionsForBinary->osFilename.c_str(),
     140             :                     GDAL_OF_READONLY | GDAL_OF_VECTOR,
     141           0 :                     psOptionsForBinary->aosAllowInputDrivers.List(),
     142           0 :                     psOptionsForBinary->aosOpenOptions.List(), nullptr);
     143           0 :                 if (poDS != nullptr && psOptionsForBinary->bVerbose)
     144             :                 {
     145           0 :                     printf("Had to open data source read-only.\n");
     146             : #ifdef __AFL_HAVE_MANUAL_CONTROL
     147             :                     psOptionsForBinary->bReadOnly = true;
     148             : #endif
     149             :                 }
     150             :             }
     151             :         }
     152             : 
     153          43 :         int nRet = 0;
     154          43 :         if (poDS == nullptr)
     155             :         {
     156           4 :             nRet = 1;
     157             : 
     158             :             VSIStatBuf sStat;
     159           8 :             CPLString message;
     160             :             message.Printf("ogrinfo failed - unable to open '%s'.",
     161           4 :                            psOptionsForBinary->osFilename.c_str());
     162           4 :             if (VSIStat(psOptionsForBinary->osFilename.c_str(), &sStat) == 0)
     163             :             {
     164             :                 GDALDriverH drv =
     165           3 :                     GDALIdentifyDriverEx(psOptionsForBinary->osFilename.c_str(),
     166             :                                          GDAL_OF_RASTER, nullptr, nullptr);
     167           3 :                 if (drv)
     168             :                 {
     169           2 :                     message += " Did you intend to call gdalinfo?";
     170             :                 }
     171             :             }
     172           4 :             fprintf(stderr, "%s\n", message.c_str());
     173             :         }
     174             :         else
     175             :         {
     176             :             char *pszGDALVectorInfoOutput =
     177          39 :                 GDALVectorInfo(GDALDataset::ToHandle(poDS), psOptions);
     178             : 
     179          39 :             if (pszGDALVectorInfoOutput)
     180          38 :                 printf("%s", pszGDALVectorInfoOutput);
     181             :             else
     182           1 :                 nRet = 1;
     183             : 
     184          39 :             CPLFree(pszGDALVectorInfoOutput);
     185             :         }
     186             : 
     187          43 :         delete poDS;
     188             : #ifdef __AFL_HAVE_MANUAL_CONTROL
     189             :     }
     190             : #endif
     191             : 
     192          43 :     GDALVectorInfoOptionsFree(psOptions);
     193             : 
     194          43 :     CSLDestroy(argv);
     195             : 
     196          43 :     GDALDumpOpenDatasets(stderr);
     197             : 
     198          43 :     GDALDestroyDriverManager();
     199             : 
     200          43 :     CPLDumpSharedList(nullptr);
     201          43 :     GDALDestroy();
     202             : 
     203          43 :     exit(nRet);
     204             : }
     205             : 
     206           0 : MAIN_END

Generated by: LCOV version 1.14