Skip to content

Commit 0454d8e

Browse files
authored
Merge pull request OSGeo#12226 from rouault/fix_12225
gdal vector convert: do not emit error message when outputting to /vsistdout/
2 parents c83d628 + 2cc42f6 commit 0454d8e

16 files changed

+108
-99
lines changed

apps/gdalalg_abstract_pipeline.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "cpl_conv.h"
1919
#include "cpl_json.h"
2020
#include "gdalalgorithm.h"
21+
#include "gdal_priv.h"
2122

2223
#include <algorithm>
2324

@@ -172,8 +173,8 @@ bool GDALAbstractPipelineAlgorithm<StepAlgorithm>::RunStep(
172173
const auto &filename =
173174
outputArg->GDALAlgorithmArg::template Get<GDALArgDatasetValue>()
174175
.GetName();
175-
VSIStatBufL sStat;
176-
if (VSIStatL(filename.c_str(), &sStat) == 0)
176+
const char *pszType = "";
177+
if (GDALDoesFileOrDatasetExist(filename.c_str(), &pszType))
177178
{
178179
const auto overwriteArg =
179180
m_steps.back()->GetArg(GDAL_ARG_NAME_OVERWRITE);
@@ -182,9 +183,9 @@ bool GDALAbstractPipelineAlgorithm<StepAlgorithm>::RunStep(
182183
if (!overwriteArg->GDALAlgorithmArg::template Get<bool>())
183184
{
184185
CPLError(CE_Failure, CPLE_AppDefined,
185-
"File '%s' already exists. Specify the "
186+
"%s '%s' already exists. Specify the "
186187
"--overwrite option to overwrite it.",
187-
filename.c_str());
188+
pszType, filename.c_str());
188189
return false;
189190
}
190191
}

apps/gdalalg_raster_calc.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,14 @@ bool GDALRasterCalcAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
576576
return false;
577577
}
578578

579-
VSIStatBufL sStat;
579+
const char *pszType = "";
580580
if (!m_overwrite && !m_outputDataset.GetName().empty() &&
581-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
582-
std::unique_ptr<GDALDataset>(
583-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
581+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
584582
{
585583
ReportError(CE_Failure, CPLE_AppDefined,
586-
"File '%s' already exists. Specify the --overwrite "
584+
"%s '%s' already exists. Specify the --overwrite "
587585
"option to overwrite it.",
588-
m_outputDataset.GetName().c_str());
586+
pszType, m_outputDataset.GetName().c_str());
589587
return false;
590588
}
591589

apps/gdalalg_raster_contour.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,14 @@ bool GDALRasterContourAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
177177
aosOptions.AddString(m_outputLayerName);
178178
}
179179

180-
VSIStatBufL sStat;
180+
const char *pszType = "";
181181
if (!m_overwrite && !m_outputDataset.GetName().empty() &&
182-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
183-
std::unique_ptr<GDALDataset>(
184-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
182+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
185183
{
186184
ReportError(CE_Failure, CPLE_AppDefined,
187-
"File '%s' already exists. Specify the --overwrite "
185+
"%s '%s' already exists. Specify the --overwrite "
188186
"option to overwrite it.",
189-
m_outputDataset.GetName().c_str());
187+
pszType, m_outputDataset.GetName().c_str());
190188
return false;
191189
}
192190

apps/gdalalg_raster_create.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,16 @@ bool GDALRasterCreateAlgorithm::RunImpl(GDALProgressFunc /* pfnProgress */,
9494
return false;
9595
}
9696

97-
VSIStatBufL sStat;
97+
const char *pszType = "";
9898
if (!m_append && !m_outputDataset.GetName().empty() &&
99-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
100-
std::unique_ptr<GDALDataset>(
101-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
99+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
102100
{
103101
if (!m_overwrite)
104102
{
105103
ReportError(CE_Failure, CPLE_AppDefined,
106-
"File '%s' already exists. Specify the --overwrite "
104+
"%s '%s' already exists. Specify the --overwrite "
107105
"option to overwrite it, or --append to append to it.",
108-
m_outputDataset.GetName().c_str());
106+
pszType, m_outputDataset.GetName().c_str());
109107
return false;
110108
}
111109
else

apps/gdalalg_raster_fillnodata.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,14 @@ bool GDALRasterFillNodataAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
9999
void *pProgressData)
100100
{
101101

102-
VSIStatBufL sStat;
102+
const char *pszType = "";
103103
if (!m_overwrite && !m_outputDataset.GetName().empty() &&
104-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
105-
std::unique_ptr<GDALDataset>(
106-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
104+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
107105
{
108106
ReportError(CE_Failure, CPLE_AppDefined,
109-
"File '%s' already exists. Specify the --overwrite "
107+
"%s '%s' already exists. Specify the --overwrite "
110108
"option to overwrite it.",
111-
m_outputDataset.GetName().c_str());
109+
pszType, m_outputDataset.GetName().c_str());
112110
return false;
113111
}
114112

apps/gdalalg_raster_mosaic.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,14 @@ bool GDALRasterMosaicAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
180180
return false;
181181
}
182182

183-
VSIStatBufL sStat;
183+
const char *pszType = "";
184184
if (!m_overwrite && !m_outputDataset.GetName().empty() &&
185-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
186-
std::unique_ptr<GDALDataset>(
187-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
185+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
188186
{
189187
ReportError(CE_Failure, CPLE_AppDefined,
190-
"File '%s' already exists. Specify the --overwrite "
188+
"%s '%s' already exists. Specify the --overwrite "
191189
"option to overwrite it.",
192-
m_outputDataset.GetName().c_str());
190+
pszType, m_outputDataset.GetName().c_str());
193191
return false;
194192
}
195193

apps/gdalalg_raster_polygonize.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,16 @@ bool GDALRasterPolygonizeAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
9090
auto poSrcDS = m_inputDataset.GetDatasetRef();
9191
CPLAssert(poSrcDS);
9292

93-
VSIStatBufL sStat;
93+
const char *pszType = "";
9494
if (!m_update && !m_outputDataset.GetName().empty() &&
95-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
96-
std::unique_ptr<GDALDataset>(
97-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
95+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
9896
{
9997
if (!m_overwrite)
10098
{
10199
ReportError(CE_Failure, CPLE_AppDefined,
102-
"File '%s' already exists. Specify the --overwrite "
100+
"%s '%s' already exists. Specify the --overwrite "
103101
"option to overwrite it, or --update to update it.",
104-
m_outputDataset.GetName().c_str());
102+
pszType, m_outputDataset.GetName().c_str());
105103
return false;
106104
}
107105
else

apps/gdalalg_raster_stack.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,14 @@ bool GDALRasterStackAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
175175
return false;
176176
}
177177

178-
VSIStatBufL sStat;
178+
const char *pszType = "";
179179
if (!m_overwrite && !m_outputDataset.GetName().empty() &&
180-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
181-
std::unique_ptr<GDALDataset>(
182-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
180+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
183181
{
184182
ReportError(CE_Failure, CPLE_AppDefined,
185-
"File '%s' already exists. Specify the --overwrite "
183+
"%s '%s' already exists. Specify the --overwrite "
186184
"option to overwrite it.",
187-
m_outputDataset.GetName().c_str());
185+
pszType, m_outputDataset.GetName().c_str());
188186
return false;
189187
}
190188

apps/gdalalg_raster_viewshed.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,14 @@ bool GDALRasterViewshedAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
127127
return false;
128128
}
129129

130-
VSIStatBufL sStat;
130+
const char *pszType = "";
131131
if (!m_overwrite && !m_outputDataset.GetName().empty() &&
132-
(VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0 ||
133-
std::unique_ptr<GDALDataset>(
134-
GDALDataset::Open(m_outputDataset.GetName().c_str()))))
132+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
135133
{
136134
ReportError(CE_Failure, CPLE_AppDefined,
137-
"File '%s' already exists. Specify the --overwrite "
135+
"%s '%s' already exists. Specify the --overwrite "
138136
"option to overwrite it.",
139-
m_outputDataset.GetName().c_str());
137+
pszType, m_outputDataset.GetName().c_str());
140138
return false;
141139
}
142140

apps/gdalalg_vector_grid.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -307,40 +307,21 @@ bool GDALVectorGridAbstractAlgorithm::RunImpl(GDALProgressFunc pfnProgress,
307307

308308
GDALGridOptionsSetProgress(psOptions.get(), pfnProgress, pProgressData);
309309

310-
VSIStatBufL sStat;
311-
std::unique_ptr<GDALDataset> poDstDS;
312-
const bool fileExists =
313-
VSIStatL(m_outputDataset.GetName().c_str(), &sStat) == 0;
314-
315-
{
316-
CPLErrorStateBackuper oCPLErrorHandlerPusher(CPLQuietErrorHandler);
317-
poDstDS.reset(GDALDataset::Open(m_outputDataset.GetName().c_str(),
318-
GDAL_OF_RASTER | GDAL_OF_UPDATE,
319-
nullptr, nullptr, nullptr));
320-
}
321-
322-
if (poDstDS)
310+
const char *pszType = "";
311+
if (!m_outputDataset.GetName().empty() &&
312+
GDALDoesFileOrDatasetExist(m_outputDataset.GetName().c_str(), &pszType))
323313
{
324314
if (!m_overwrite)
325315
{
326-
CPLError(CE_Failure, CPLE_AppDefined,
327-
"Dataset '%s' already exists. Specify the --overwrite "
328-
"option to overwrite it",
329-
m_outputDataset.GetName().c_str());
316+
ReportError(CE_Failure, CPLE_AppDefined,
317+
"%s '%s' already exists. Specify the --overwrite "
318+
"option to overwrite it, or --update to update it.",
319+
pszType, m_outputDataset.GetName().c_str());
330320
return false;
331321
}
332-
else if (fileExists)
322+
else
333323
{
334-
poDstDS.reset();
335-
336-
// Delete the existing file
337-
if (VSIUnlink(m_outputDataset.GetName().c_str()) != 0)
338-
{
339-
CPLError(CE_Failure, CPLE_AppDefined,
340-
"Failed to delete existing dataset '%s'.",
341-
m_outputDataset.GetName().c_str());
342-
return false;
343-
}
324+
VSIUnlink(m_outputDataset.GetName().c_str());
344325
}
345326
}
346327

0 commit comments

Comments
 (0)