|
| 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 | +GDALVFSDeleteAlgorithm::GDALVFSDeleteAlgorithm() |
| 32 | + : GDALAlgorithm(NAME, DESCRIPTION, HELP_URL) |
| 33 | +{ |
| 34 | + { |
| 35 | + auto &arg = AddArg("filename", 0, _("File or directory name to delete"), |
| 36 | + &m_filename) |
| 37 | + .SetPositional() |
| 38 | + .SetRequired(); |
| 39 | + SetAutoCompleteFunctionForFilename(arg, 0); |
| 40 | + arg.AddValidationAction( |
| 41 | + [this]() |
| 42 | + { |
| 43 | + if (m_filename.empty()) |
| 44 | + { |
| 45 | + ReportError(CE_Failure, CPLE_IllegalArg, |
| 46 | + "Filename cannot be empty"); |
| 47 | + return false; |
| 48 | + } |
| 49 | + return true; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + AddArg("recursive", 'r', _("Delete directories recursively"), &m_recursive) |
| 54 | + .AddShortNameAlias('R'); |
| 55 | +} |
| 56 | + |
| 57 | +/************************************************************************/ |
| 58 | +/* GDALVFSDeleteAlgorithm::RunImpl() */ |
| 59 | +/************************************************************************/ |
| 60 | + |
| 61 | +bool GDALVFSDeleteAlgorithm::RunImpl(GDALProgressFunc, void *) |
| 62 | +{ |
| 63 | + bool ret = false; |
| 64 | + VSIStatBufL sStat; |
| 65 | + if (VSIStatL(m_filename.c_str(), &sStat) != 0) |
| 66 | + { |
| 67 | + ReportError(CE_Failure, CPLE_FileIO, "%s does not exist", |
| 68 | + m_filename.c_str()); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + if (m_recursive) |
| 73 | + { |
| 74 | + ret = VSIRmdirRecursive(m_filename.c_str()) == 0; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + ret = VSI_ISDIR(sStat.st_mode) ? VSIRmdir(m_filename.c_str()) == 0 |
| 79 | + : VSIUnlink(m_filename.c_str()) == 0; |
| 80 | + } |
| 81 | + if (!ret) |
| 82 | + { |
| 83 | + ReportError(CE_Failure, CPLE_FileIO, "Cannot delete %s", |
| 84 | + m_filename.c_str()); |
| 85 | + } |
| 86 | + } |
| 87 | + return ret; |
| 88 | +} |
0 commit comments