LCOV - code coverage report
Current view: top level - apps - gdalalg_vfs_delete.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 26 26 100.0 %
Date: 2025-04-16 00:42:22 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /******************************************************************************
       2             :  *
       3             :  * Project:  GDAL
       4             :  * Purpose:  gdal "vfs 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_vfs_delete.h"
      14             : 
      15             : #include "cpl_conv.h"
      16             : #include "cpl_string.h"
      17             : #include "cpl_vsi.h"
      18             : 
      19             : #include <algorithm>
      20             : 
      21             : //! @cond Doxygen_Suppress
      22             : 
      23             : #ifndef _
      24             : #define _(x) (x)
      25             : #endif
      26             : 
      27             : /************************************************************************/
      28             : /*            GDALVFSDeleteAlgorithm::GDALVFSDeleteAlgorithm()          */
      29             : /************************************************************************/
      30             : 
      31           7 : GDALVFSDeleteAlgorithm::GDALVFSDeleteAlgorithm()
      32           7 :     : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
      33             : {
      34             :     {
      35             :         auto &arg = AddArg("filename", 0, _("File or directory name to delete"),
      36          14 :                            &m_filename)
      37           7 :                         .SetPositional()
      38           7 :                         .SetRequired();
      39           7 :         SetAutoCompleteFunctionForFilename(arg, 0);
      40             :         arg.AddValidationAction(
      41           7 :             [this]()
      42             :             {
      43           6 :                 if (m_filename.empty())
      44             :                 {
      45           1 :                     ReportError(CE_Failure, CPLE_IllegalArg,
      46             :                                 "Filename cannot be empty");
      47           1 :                     return false;
      48             :                 }
      49           5 :                 return true;
      50           7 :             });
      51             :     }
      52             : 
      53          14 :     AddArg("recursive", 'r', _("Delete directories recursively"), &m_recursive)
      54           7 :         .AddShortNameAlias('R');
      55           7 : }
      56             : 
      57             : /************************************************************************/
      58             : /*                    GDALVFSDeleteAlgorithm::RunImpl()                 */
      59             : /************************************************************************/
      60             : 
      61           5 : bool GDALVFSDeleteAlgorithm::RunImpl(GDALProgressFunc, void *)
      62             : {
      63           5 :     bool ret = false;
      64             :     VSIStatBufL sStat;
      65           5 :     if (VSIStatL(m_filename.c_str(), &sStat) != 0)
      66             :     {
      67           1 :         ReportError(CE_Failure, CPLE_FileIO, "%s does not exist",
      68             :                     m_filename.c_str());
      69             :     }
      70             :     else
      71             :     {
      72           4 :         if (m_recursive)
      73             :         {
      74           1 :             ret = VSIRmdirRecursive(m_filename.c_str()) == 0;
      75             :         }
      76             :         else
      77             :         {
      78           5 :             ret = VSI_ISDIR(sStat.st_mode) ? VSIRmdir(m_filename.c_str()) == 0
      79           2 :                                            : VSIUnlink(m_filename.c_str()) == 0;
      80             :         }
      81           4 :         if (!ret)
      82             :         {
      83           1 :             ReportError(CE_Failure, CPLE_FileIO, "Cannot delete %s",
      84             :                         m_filename.c_str());
      85             :         }
      86             :     }
      87           5 :     return ret;
      88             : }
      89             : 
      90             : //! @endcond

Generated by: LCOV version 1.14