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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/******************************************************************************
*
* Project: GDAL Utilities
* Purpose: Command line utility for GDAL identify, delete, rename and copy
* (by file) operations.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
* ****************************************************************************
* Copyright (c) 2007, Frank Warmerdam
* Copyright (c) 2008-2009, Even Rouault <even dot rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_string.h"
#include "cpl_conv.h"
#include "gdal_version.h"
#include "gdal.h"
#include "commonutils.h"
#include "gdalargumentparser.h"
/************************************************************************/
/* GDALManageOptions() */
/************************************************************************/
struct GDALManageOptions
{
bool bRecursive = false;
bool bForceRecurse = false;
bool bReportFailures = false;
std::string osNewName;
std::string osDatasetName;
std::vector<std::string> aosDatasetNames;
std::string osDriverName;
};
/************************************************************************/
/* ProcessIdentifyTarget() */
/************************************************************************/
static void ProcessIdentifyTarget(const char *pszTarget,
char **papszSiblingList, bool bRecursive,
bool bReportFailures, bool bForceRecurse)
{
GDALDriverH hDriver;
VSIStatBufL sStatBuf;
int i;
hDriver = GDALIdentifyDriver(pszTarget, papszSiblingList);
if (hDriver != nullptr)
printf("%s: %s\n", pszTarget, GDALGetDriverShortName(hDriver));
else if (bReportFailures)
printf("%s: unrecognized\n", pszTarget);
if (!bForceRecurse && (!bRecursive || hDriver != nullptr))
return;
if (VSIStatL(pszTarget, &sStatBuf) != 0 || !VSI_ISDIR(sStatBuf.st_mode))
return;
papszSiblingList = VSIReadDir(pszTarget);
for (i = 0; papszSiblingList && papszSiblingList[i]; i++)
{
if (EQUAL(papszSiblingList[i], "..") || EQUAL(papszSiblingList[i], "."))
continue;
const CPLString osSubTarget =
CPLFormFilenameSafe(pszTarget, papszSiblingList[i], nullptr);
ProcessIdentifyTarget(osSubTarget, papszSiblingList, bRecursive,
bReportFailures, bForceRecurse);
}
CSLDestroy(papszSiblingList);
}
/************************************************************************/
/* GDALManageAppOptionsGetParser() */
/************************************************************************/
static std::unique_ptr<GDALArgumentParser>
GDALManageAppOptionsGetParser(GDALManageOptions *psOptions)
{
auto argParser = std::make_unique<GDALArgumentParser>(
"gdalmanage", /* bForBinary */ true);
argParser->add_description(
_("Identify, delete, rename and copy raster data files."));
argParser->add_epilog(_("For more details, consult the full documentation "
"for the gdalmanage utility "
"https://gdal.org/programs/gdalmanage.html"));
auto addCommonOptions =
[psOptions](GDALArgumentParser *subParser, const char *helpMessageSrc)
{
subParser->add_argument("-f")
.metavar("<format>")
.store_into(psOptions->osDriverName)
.help(_("Specify format of raster file if unknown by the "
"application."));
subParser->add_argument("datasetname")
.metavar("<datasetname>")
.store_into(psOptions->osDatasetName)
.help(helpMessageSrc);
subParser->add_argument("newdatasetname")
.metavar("<newdatasetname>")
.store_into(psOptions->osNewName)
.help(_("Name of the new file."));
};
// Identify
auto identifyParser =
argParser->add_subparser("identify", /* bForBinary */ true);
identifyParser->add_description(_("List data format of file(s)."));
identifyParser->add_argument("-r")
.flag()
.store_into(psOptions->bRecursive)
.help(_("Recursively scan files/folders for raster files."));
identifyParser->add_argument("-fr")
.flag()
.store_into(psOptions->bRecursive)
.store_into(psOptions->bForceRecurse)
.help(_("Recursively scan folders for raster files, forcing "
"recursion in folders recognized as valid formats."));
identifyParser->add_argument("-u")
.flag()
.store_into(psOptions->bReportFailures)
.help(_("Report failures if file type is unidentified."));
// Note: this accepts multiple files
identifyParser->add_argument("datasetname")
.metavar("<datasetname>")
.store_into(psOptions->aosDatasetNames)
.remaining()
.help(_("Name(s) of the file(s) to identify."));
// Copy
auto copyParser = argParser->add_subparser("copy", /* bForBinary */ true);
copyParser->add_description(
_("Create a copy of the raster file with a new name."));
addCommonOptions(copyParser, _("Name of the file to copy."));
// Rename
auto renameParser =
argParser->add_subparser("rename", /* bForBinary */ true);
renameParser->add_description(_("Change the name of the raster file."));
addCommonOptions(renameParser, _("Name of the file to rename."));
// Delete
auto deleteParser =
argParser->add_subparser("delete", /* bForBinary */ true);
deleteParser->add_description(_("Delete the raster file(s)."));
// Note: this accepts multiple files
deleteParser->add_argument("datasetname")
.metavar("<datasetname>")
.store_into(psOptions->aosDatasetNames)
.remaining()
.help(_("Name(s) of the file(s) to delete."));
deleteParser->add_argument("-f")
.metavar("<format>")
.store_into(psOptions->osDriverName)
.help(
_("Specify format of raster file if unknown by the application."));
return argParser;
}
/************************************************************************/
/* main() */
/************************************************************************/
MAIN_START(argc, argv)
{
EarlySetConfigOptions(argc, argv);
argc = GDALGeneralCmdLineProcessor(argc, &argv, 0);
if (argc < 1)
exit(-argc);
/* -------------------------------------------------------------------- */
/* Parse arguments. */
/* -------------------------------------------------------------------- */
if (argc < 2)
{
try
{
GDALManageOptions sOptions;
auto argParser = GDALManageAppOptionsGetParser(&sOptions);
fprintf(stderr, "%s\n", argParser->usage().c_str());
}
catch (const std::exception &err)
{
CPLError(CE_Failure, CPLE_AppDefined, "Unexpected exception: %s",
err.what());
}
CSLDestroy(argv);
exit(1);
}
GDALAllRegister();
GDALManageOptions psOptions;
auto argParser = GDALManageAppOptionsGetParser(&psOptions);
try
{
argParser->parse_args_without_binary_name(argv + 1);
CSLDestroy(argv);
}
catch (const std::exception &error)
{
argParser->display_error_and_usage(error);
CSLDestroy(argv);
exit(1);
}
// For some obscure reason datasetname is parsed as mandatory
// if used with remaining() in a subparser
if (psOptions.aosDatasetNames.empty() && psOptions.osDatasetName.empty())
{
std::invalid_argument error(
_("No dataset name provided. At least one dataset "
"name is required."));
argParser->display_error_and_usage(error);
exit(1);
}
GDALDriverH hDriver = nullptr;
if (!psOptions.osDriverName.empty())
{
hDriver = GDALGetDriverByName(psOptions.osDriverName.c_str());
if (hDriver == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined, "Failed to find driver '%s'.",
psOptions.osDriverName.c_str());
exit(1);
}
}
/* -------------------------------------------------------------------- */
/* Split out based on operation. */
/* -------------------------------------------------------------------- */
if (argParser->is_subcommand_used("identify"))
{
// Process all files in aosDatasetName
for (const auto &datasetName : psOptions.aosDatasetNames)
{
ProcessIdentifyTarget(
datasetName.c_str(), nullptr, psOptions.bRecursive,
psOptions.bReportFailures, psOptions.bForceRecurse);
}
}
else if (argParser->is_subcommand_used("copy"))
{
GDALCopyDatasetFiles(hDriver, psOptions.osNewName.c_str(),
psOptions.osDatasetName.c_str());
}
else if (argParser->is_subcommand_used("rename"))
{
GDALRenameDataset(hDriver, psOptions.osNewName.c_str(),
psOptions.osDatasetName.c_str());
}
else if (argParser->is_subcommand_used("delete"))
{
// Process all files in aosDatasetName
for (const auto &datasetName : psOptions.aosDatasetNames)
{
GDALDeleteDataset(hDriver, datasetName.c_str());
}
}
/* -------------------------------------------------------------------- */
/* Cleanup */
/* -------------------------------------------------------------------- */
GDALDestroy();
exit(0);
}
MAIN_END
|