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
|
/******************************************************************************
*
* Project: GDAL
* Purpose: gdal "vsi delete" subcommand
* Author: Even Rouault <even dot rouault at spatialys.com>
*
******************************************************************************
* Deleteright (c) 2025, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "gdalalg_vsi_delete.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "cpl_vsi.h"
#include "cpl_vsi_error.h"
#include <algorithm>
//! @cond Doxygen_Suppress
#ifndef _
#define _(x) (x)
#endif
/************************************************************************/
/* GDALVSIDeleteAlgorithm::GDALVSIDeleteAlgorithm() */
/************************************************************************/
GDALVSIDeleteAlgorithm::GDALVSIDeleteAlgorithm()
: GDALAlgorithm(NAME, DESCRIPTION, HELP_URL)
{
{
auto &arg = AddArg("filename", 0, _("File or directory name to delete"),
&m_filename)
.SetPositional()
.SetMinCharCount(1)
.SetRequired();
SetAutoCompleteFunctionForFilename(arg, 0);
}
AddArg("recursive", 'r', _("Delete directories recursively"), &m_recursive)
.AddShortNameAlias('R');
}
/************************************************************************/
/* GDALVSIDeleteAlgorithm::RunImpl() */
/************************************************************************/
bool GDALVSIDeleteAlgorithm::RunImpl(GDALProgressFunc, void *)
{
bool ret = false;
VSIStatBufL sStat;
VSIErrorReset();
const auto nOldErrorNum = VSIGetLastErrorNo();
if (VSIStatL(m_filename.c_str(), &sStat) != 0)
{
if (nOldErrorNum != VSIGetLastErrorNo())
{
ReportError(CE_Failure, CPLE_FileIO,
"'%s' cannot be accessed. %s: %s", m_filename.c_str(),
VSIErrorNumToString(VSIGetLastErrorNo()),
VSIGetLastErrorMsg());
}
else
{
ReportError(CE_Failure, CPLE_FileIO,
"'%s' does not exist or cannot be accessed",
m_filename.c_str());
}
}
else
{
if (m_recursive)
{
ret = VSIRmdirRecursive(m_filename.c_str()) == 0;
}
else
{
ret = VSI_ISDIR(sStat.st_mode) ? VSIRmdir(m_filename.c_str()) == 0
: VSIUnlink(m_filename.c_str()) == 0;
}
if (!ret)
{
ReportError(CE_Failure, CPLE_FileIO, "Cannot delete %s",
m_filename.c_str());
}
}
return ret;
}
//! @endcond
|