Skip to content

Commit

Permalink
frmts/ ogr/ .cpp: replace all remaining uses of CPLGenerateTempFilena…
Browse files Browse the repository at this point in the history
…me() with CPLGenerateTempFilenameSafe()
  • Loading branch information
rouault committed Jan 14, 2025
1 parent dd632f9 commit 469e59d
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion frmts/ecw/gdal_ecw.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class VSIIOStream final : public CNCSJPCIOStream
struct stat sStatBuf;
if (!osPath.empty() && stat(osPath, &sStatBuf) != 0)
{
osFilenameUsed = CPLGenerateTempFilename(nullptr);
osFilenameUsed = CPLGenerateTempFilenameSafe(nullptr);
// try to preserve the extension.
const auto osExt = CPLGetExtensionSafe(pszFilename);
if (!osExt.empty())
Expand Down
2 changes: 1 addition & 1 deletion frmts/mrf/Tif_band.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static CPLString uniq_memfname(const char *prefix)
// Define MRF_LOCAL_TMP to use local files instead of RAM
// #define MRF_LOCAL_TMP
#if defined(MRF_LOCAL_TMP)
return CPLGenerateTempFilename(prefix);
return CPLGenerateTempFilenameSafe(prefix);
#else
return VSIMemGenerateHiddenFilename(prefix);
#endif
Expand Down
2 changes: 1 addition & 1 deletion frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8058,7 +8058,7 @@ GDALDataset *netCDFDataset::Open(GDALOpenInfo *poOpenInfo)
if (poDS->osFilename.empty())
{
poDS->bFileToDestroyAtClosing = true;
poDS->osFilename = CPLGenerateTempFilename("netcdf_tmp");
poDS->osFilename = CPLGenerateTempFilenameSafe("netcdf_tmp");
}
if (!netCDFDatasetCreateTempFile(eTmpFormat, poDS->osFilename,
poOpenInfo->fpL))
Expand Down
4 changes: 2 additions & 2 deletions ogr/ogrsf_frmts/flatgeobuf/ogrflatgeobuflayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2461,8 +2461,8 @@ std::string OGRFlatGeobufLayer::GetTempFilePath(const CPLString &fileName,
std::string osTempFile =
pszTempDir ? CPLFormFilename(pszTempDir, osBasename, nullptr)
: (STARTS_WITH(fileName, "/vsi") && !STARTS_WITH(fileName, "/vsimem/"))
? CPLGenerateTempFilename(osBasename)
: CPLFormFilename(osDirname, osBasename, nullptr);
? CPLGenerateTempFilenameSafe(osBasename)
: CPLFormFilenameSafe(osDirname, osBasename, nullptr);
osTempFile += "_temp.fgb";
return osTempFile;
}
Expand Down
2 changes: 1 addition & 1 deletion ogr/ogrsf_frmts/libkml/ogrlibkmldatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ bool OGRLIBKMLDataSource::WriteKmz()
EQUAL(CPLGetConfigOption("CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE", ""),
"FORCED"))
{
osTmpFilename = CPLGenerateTempFilename(
osTmpFilename = CPLGenerateTempFilenameSafe(
CPLGetBasenameSafe(GetDescription()).c_str());
}

Expand Down
8 changes: 4 additions & 4 deletions ogr/ogrsf_frmts/osm/ogrosmdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2896,7 +2896,7 @@ int OGROSMDataSource::Open(const char *pszFilename,
VSIUnlink(m_osNodesFilename);

m_bInMemoryNodesFile = false;
m_osNodesFilename = CPLGenerateTempFilename("osm_tmp_nodes");
m_osNodesFilename = CPLGenerateTempFilenameSafe("osm_tmp_nodes");

m_fpNodes = VSIFOpenL(m_osNodesFilename, "wb+");
if (m_fpNodes == nullptr)
Expand Down Expand Up @@ -3000,7 +3000,7 @@ bool OGROSMDataSource::CreateTempDB()

if (!bSuccess)
{
m_osTmpDBName = CPLGenerateTempFilename("osm_tmp");
m_osTmpDBName = CPLGenerateTempFilenameSafe("osm_tmp");
rc = sqlite3_open(m_osTmpDBName.c_str(), &m_hDB);

/* On Unix filesystems, you can remove a file even if it */
Expand Down Expand Up @@ -4072,7 +4072,7 @@ bool OGROSMDataSource::TransferToDiskIfNecesserary()
m_fpNodes = nullptr;

const std::string osNewTmpDBName(
CPLGenerateTempFilename("osm_tmp_nodes"));
CPLGenerateTempFilenameSafe("osm_tmp_nodes"));

CPLDebug("OSM",
"%s too big for RAM. Transferring it onto disk in %s",
Expand Down Expand Up @@ -4153,7 +4153,7 @@ bool OGROSMDataSource::TransferToDiskIfNecesserary()
CloseDB();

const std::string osNewTmpDBName(
CPLGenerateTempFilename("osm_tmp"));
CPLGenerateTempFilenameSafe("osm_tmp"));

CPLDebug("OSM",
"%s too big for RAM. Transferring it onto disk in %s",
Expand Down
84 changes: 42 additions & 42 deletions ogr/ogrsf_frmts/selafin/ogrselafinlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,19 +596,19 @@ OGRErr OGRSelafinLayer::ICreateFeature(OGRFeature *poFeature)
// Now comes the real insertion. Since values have to be inserted nearly
// everywhere in the file and we don't want to store everything in memory to
// overwrite it, we create a new copy of it where we write the new values
const char *pszTempfile = CPLGenerateTempFilename(nullptr);
VSILFILE *fpNew = VSIFOpenL(pszTempfile, "wb+");
const std::string osTempfile = CPLGenerateTempFilenameSafe(nullptr);
VSILFILE *fpNew = VSIFOpenL(osTempfile.c_str(), "wb+");
if (fpNew == nullptr)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open temporary file %s with write access, %s.",
pszTempfile, VSIStrerror(errno));
osTempfile.c_str(), VSIStrerror(errno));
return OGRERR_FAILURE;
}
if (Selafin::write_header(fpNew, poHeader) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int i = 0; i < poHeader->nSteps; ++i)
Expand All @@ -623,7 +623,7 @@ OGRErr OGRSelafinLayer::ICreateFeature(OGRFeature *poFeature)
Selafin::write_integer(fpNew, 4) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int j = 0; j < poHeader->nVar; ++j)
Expand All @@ -633,15 +633,15 @@ OGRErr OGRSelafinLayer::ICreateFeature(OGRFeature *poFeature)
poHeader->nFileSize) == -1)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
padfValues = (double *)CPLRealloc(
padfValues, sizeof(double) * poHeader->nPoints);
if (padfValues == nullptr)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
if (eType == POINTS)
Expand All @@ -654,7 +654,7 @@ OGRErr OGRSelafinLayer::ICreateFeature(OGRFeature *poFeature)
{
CPLFree(padfValues);
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
CPLFree(padfValues);
Expand All @@ -665,7 +665,7 @@ OGRErr OGRSelafinLayer::ICreateFeature(OGRFeature *poFeature)
// the old one. This way, even if something goes bad, we can still recover
// the layer. The copy process is format-agnostic.
MoveOverwrite(poHeader->fp, fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
poHeader->UpdateFileSize();
return OGRERR_NONE;
}
Expand Down Expand Up @@ -722,19 +722,19 @@ OGRErr OGRSelafinLayer::CreateField(const OGRFieldDefn *poField,
// Now comes the real insertion. Since values have to be inserted nearly
// everywhere in the file and we don't want to store everything in memory to
// overwrite it, we create a new copy of it where we write the new values
const char *pszTempfile = CPLGenerateTempFilename(nullptr);
VSILFILE *fpNew = VSIFOpenL(pszTempfile, "wb+");
const std::string osTempfile = CPLGenerateTempFilenameSafe(nullptr);
VSILFILE *fpNew = VSIFOpenL(osTempfile.c_str(), "wb+");
if (fpNew == nullptr)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open temporary file %s with write access, %s.",
pszTempfile, VSIStrerror(errno));
osTempfile.c_str(), VSIStrerror(errno));
return OGRERR_FAILURE;
}
if (Selafin::write_header(fpNew, poHeader) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int i = 0; i < poHeader->nSteps; ++i)
Expand All @@ -749,7 +749,7 @@ OGRErr OGRSelafinLayer::CreateField(const OGRFieldDefn *poField,
Selafin::write_integer(fpNew, 4) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
double *padfValues = nullptr;
Expand All @@ -759,15 +759,15 @@ OGRErr OGRSelafinLayer::CreateField(const OGRFieldDefn *poField,
poHeader->nFileSize) == -1)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
if (Selafin::write_floatarray(fpNew, padfValues,
poHeader->nPoints) == 0)
{
CPLFree(padfValues);
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
CPLFree(padfValues);
Expand All @@ -781,13 +781,13 @@ OGRErr OGRSelafinLayer::CreateField(const OGRFieldDefn *poField,
{
CPLFree(padfValues);
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
CPLFree(padfValues);
}
MoveOverwrite(poHeader->fp, fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
poHeader->UpdateFileSize();
return OGRERR_NONE;
}
Expand All @@ -813,19 +813,19 @@ OGRErr OGRSelafinLayer::DeleteField(int iField)
// Now comes the real deletion. Since values have to be deleted nearly
// everywhere in the file and we don't want to store everything in memory to
// overwrite it, we create a new copy of it where we write the new values
const char *pszTempfile = CPLGenerateTempFilename(nullptr);
VSILFILE *fpNew = VSIFOpenL(pszTempfile, "wb+");
const std::string osTempfile = CPLGenerateTempFilenameSafe(nullptr);
VSILFILE *fpNew = VSIFOpenL(osTempfile.c_str(), "wb+");
if (fpNew == nullptr)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open temporary file %s with write access, %s.",
pszTempfile, VSIStrerror(errno));
osTempfile.c_str(), VSIStrerror(errno));
return OGRERR_FAILURE;
}
if (Selafin::write_header(fpNew, poHeader) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int i = 0; i < poHeader->nSteps; ++i)
Expand All @@ -840,7 +840,7 @@ OGRErr OGRSelafinLayer::DeleteField(int iField)
Selafin::write_integer(fpNew, 4) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int j = 0; j < poHeader->nVar; ++j)
Expand All @@ -850,7 +850,7 @@ OGRErr OGRSelafinLayer::DeleteField(int iField)
poHeader->nFileSize) == -1)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
if (j != iField)
Expand All @@ -860,15 +860,15 @@ OGRErr OGRSelafinLayer::DeleteField(int iField)
{
CPLFree(padfValues);
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
}
CPLFree(padfValues);
}
}
MoveOverwrite(poHeader->fp, fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
poHeader->UpdateFileSize();
return OGRERR_NONE;
}
Expand All @@ -891,19 +891,19 @@ OGRErr OGRSelafinLayer::ReorderFields(int *panMap)
poFeatureDefn->ReorderFieldDefns(panMap);

// Now comes the real change.
const char *pszTempfile = CPLGenerateTempFilename(nullptr);
VSILFILE *fpNew = VSIFOpenL(pszTempfile, "wb+");
const std::string osTempfile = CPLGenerateTempFilenameSafe(nullptr);
VSILFILE *fpNew = VSIFOpenL(osTempfile.c_str(), "wb+");
if (fpNew == nullptr)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open temporary file %s with write access, %s.",
pszTempfile, VSIStrerror(errno));
osTempfile.c_str(), VSIStrerror(errno));
return OGRERR_FAILURE;
}
if (Selafin::write_header(fpNew, poHeader) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
double *padfValues = nullptr;
Expand All @@ -919,7 +919,7 @@ OGRErr OGRSelafinLayer::ReorderFields(int *panMap)
Selafin::write_integer(fpNew, 4) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int j = 0; j < poHeader->nVar; ++j)
Expand All @@ -930,22 +930,22 @@ OGRErr OGRSelafinLayer::ReorderFields(int *panMap)
poHeader->nFileSize) == -1)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
if (Selafin::write_floatarray(fpNew, padfValues,
poHeader->nPoints) == 0)
{
CPLFree(padfValues);
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
CPLFree(padfValues);
}
}
MoveOverwrite(poHeader->fp, fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
poHeader->UpdateFileSize();
return OGRERR_NONE;
}
Expand Down Expand Up @@ -1016,19 +1016,19 @@ OGRErr OGRSelafinLayer::DeleteFeature(GIntBig nFID)
}

// Now we perform the deletion by creating a new temporary layer
const char *pszTempfile = CPLGenerateTempFilename(nullptr);
VSILFILE *fpNew = VSIFOpenL(pszTempfile, "wb+");
const std::string osTempfile = CPLGenerateTempFilenameSafe(nullptr);
VSILFILE *fpNew = VSIFOpenL(osTempfile.c_str(), "wb+");
if (fpNew == nullptr)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Failed to open temporary file %s with write access, %s.",
pszTempfile, VSIStrerror(errno));
osTempfile.c_str(), VSIStrerror(errno));
return OGRERR_FAILURE;
}
if (Selafin::write_header(fpNew, poHeader) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int i = 0; i < poHeader->nSteps; ++i)
Expand All @@ -1043,7 +1043,7 @@ OGRErr OGRSelafinLayer::DeleteFeature(GIntBig nFID)
Selafin::write_integer(fpNew, 4) == 0)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
for (int j = 0; j < poHeader->nVar; ++j)
Expand All @@ -1053,7 +1053,7 @@ OGRErr OGRSelafinLayer::DeleteFeature(GIntBig nFID)
poHeader->nFileSize) == -1)
{
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
if (eType == POINTS)
Expand All @@ -1066,7 +1066,7 @@ OGRErr OGRSelafinLayer::DeleteFeature(GIntBig nFID)
{
CPLFree(padfValues);
VSIFCloseL(fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
return OGRERR_FAILURE;
}
CPLFree(padfValues);
Expand All @@ -1077,7 +1077,7 @@ OGRErr OGRSelafinLayer::DeleteFeature(GIntBig nFID)
// the old one. This way, even if something goes bad, we can still recover
// the layer. The copy process is format-agnostic.
MoveOverwrite(poHeader->fp, fpNew);
VSIUnlink(pszTempfile);
VSIUnlink(osTempfile.c_str());
poHeader->UpdateFileSize();

return OGRERR_NONE;
Expand Down
Loading

0 comments on commit 469e59d

Please sign in to comment.