LCOV - code coverage report
Current view: top level - apps - gdalalg_vsi_delete.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 25 25 100.0 %
Date: 2025-06-19 12:30:01 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "vsi delete" subcommand
       5             :  * Author:   Even Rouault <even dot rouault at spatialys.com>
       6             :  *
       7             :  ******************************************************************************
       8             :  * Deleteright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
       9             :  *
      10             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #include "gdalalg_vsi_delete.h"
      14             : 
      15             : #include "cpl_conv.h"
      16             : #include "cpl_string.h"
      17             : #include "cpl_vsi.h"
      18             : #include "cpl_vsi_error.h"
      19             : 
      20             : #include <algorithm>
      21             : 
      22             : //! @cond Doxygen_Suppress
      23             : 
      24             : #ifndef _
      25             : #define _(x) (x)
      26             : #endif
      27             : 
      28             : /************************************************************************/
      29             : /*            GDALVSIDeleteAlgorithm::GDALVSIDeleteAlgorithm()          */
      30             : /************************************************************************/
      31             : 
      32           7 : GDALVSIDeleteAlgorithm::GDALVSIDeleteAlgorithm()
      33           7 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      34             : {
      35             :     {
      36             :         auto &arg = AddArg("filename", 0, _("File or directory name to delete"),
      37          14 :                            &m_filename)
      38           7 :                         .SetPositional()
      39           7 :                         .SetMinCharCount(1)
      40           7 :                         .SetRequired();
      41           7 :         SetAutoCompleteFunctionForFilename(arg, 0);
      42             :     }
      43             : 
      44          14 :     AddArg("recursive", 'r', _("Delete directories recursively"), &m_recursive)
      45           7 :         .AddShortNameAlias('R');
      46           7 : }
      47             : 
      48             : /************************************************************************/
      49             : /*                    GDALVSIDeleteAlgorithm::RunImpl()                 */
      50             : /************************************************************************/
      51             : 
      52           6 : bool GDALVSIDeleteAlgorithm::RunImpl(GDALProgressFunc, void *)
      53             : {
      54           6 :     bool ret = false;
      55             :     VSIStatBufL sStat;
      56           6 :     VSIErrorReset();
      57           6 :     const auto nOldErrorNum = VSIGetLastErrorNo();
      58           6 :     if (VSIStatL(m_filename.c_str(), &sStat) != 0)
      59             :     {
      60           2 :         if (nOldErrorNum != VSIGetLastErrorNo())
      61             :         {
      62           1 :             ReportError(CE_Failure, CPLE_FileIO,
      63             :                         "'%s' cannot be accessed. %s: %s", m_filename.c_str(),
      64             :                         VSIErrorNumToString(VSIGetLastErrorNo()),
      65             :                         VSIGetLastErrorMsg());
      66             :         }
      67             :         else
      68             :         {
      69           1 :             ReportError(CE_Failure, CPLE_FileIO,
      70             :                         "'%s' does not exist or cannot be accessed",
      71             :                         m_filename.c_str());
      72             :         }
      73             :     }
      74             :     else
      75             :     {
      76           4 :         if (m_recursive)
      77             :         {
      78           1 :             ret = VSIRmdirRecursive(m_filename.c_str()) == 0;
      79             :         }
      80             :         else
      81             :         {
      82           5 :             ret = VSI_ISDIR(sStat.st_mode) ? VSIRmdir(m_filename.c_str()) == 0
      83           2 :                                            : VSIUnlink(m_filename.c_str()) == 0;
      84             :         }
      85           4 :         if (!ret)
      86             :         {
      87           1 :             ReportError(CE_Failure, CPLE_FileIO, "Cannot delete %s",
      88             :                         m_filename.c_str());
      89             :         }
      90             :     }
      91           6 :     return ret;
      92             : }
      93             : 
      94             : //! @endcond

Generated by: LCOV version 1.14