Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/apps/gdalalg_vsi_delete.cpp
Line
Count
Source
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
0
#define _(x) (x)
26
#endif
27
28
/************************************************************************/
29
/*           GDALVSIDeleteAlgorithm::GDALVSIDeleteAlgorithm()           */
30
/************************************************************************/
31
32
GDALVSIDeleteAlgorithm::GDALVSIDeleteAlgorithm()
33
0
    : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
34
0
{
35
0
    {
36
0
        auto &arg = AddArg("filename", 0, _("File or directory name to delete"),
37
0
                           &m_filename)
38
0
                        .SetPositional()
39
0
                        .SetMinCharCount(1)
40
0
                        .SetRequired();
41
0
        SetAutoCompleteFunctionForFilename(arg, 0);
42
0
    }
43
44
0
    AddArg("recursive", 'r', _("Delete directories recursively"), &m_recursive)
45
0
        .AddShortNameAlias('R');
46
0
}
47
48
/************************************************************************/
49
/*                  GDALVSIDeleteAlgorithm::RunImpl()                   */
50
/************************************************************************/
51
52
bool GDALVSIDeleteAlgorithm::RunImpl(GDALProgressFunc, void *)
53
0
{
54
0
    bool ret = false;
55
0
    VSIStatBufL sStat;
56
0
    VSIErrorReset();
57
0
    const auto nOldErrorNum = VSIGetLastErrorNo();
58
0
    if (VSIStatL(m_filename.c_str(), &sStat) != 0)
59
0
    {
60
0
        if (nOldErrorNum != VSIGetLastErrorNo())
61
0
        {
62
0
            ReportError(CE_Failure, CPLE_FileIO,
63
0
                        "'%s' cannot be accessed. %s: %s", m_filename.c_str(),
64
0
                        VSIErrorNumToString(VSIGetLastErrorNo()),
65
0
                        VSIGetLastErrorMsg());
66
0
        }
67
0
        else
68
0
        {
69
0
            ReportError(CE_Failure, CPLE_FileIO,
70
0
                        "'%s' does not exist or cannot be accessed",
71
0
                        m_filename.c_str());
72
0
        }
73
0
    }
74
0
    else
75
0
    {
76
0
        if (m_recursive)
77
0
        {
78
0
            ret = VSIRmdirRecursive(m_filename.c_str()) == 0;
79
0
        }
80
0
        else
81
0
        {
82
0
            ret = VSI_ISDIR(sStat.st_mode) ? VSIRmdir(m_filename.c_str()) == 0
83
0
                                           : VSIUnlink(m_filename.c_str()) == 0;
84
0
        }
85
0
        if (!ret)
86
0
        {
87
0
            ReportError(CE_Failure, CPLE_FileIO, "Cannot delete %s",
88
0
                        m_filename.c_str());
89
0
        }
90
0
    }
91
0
    return ret;
92
0
}
93
94
//! @endcond