Skip to content

Commit

Permalink
frmts/ ogr/ .cpp: replace all remaining uses of CPLGetDirname() with …
Browse files Browse the repository at this point in the history
…CPLGetDirnameSafe()
  • Loading branch information
rouault committed Jan 13, 2025
1 parent 85eb32a commit 9a9d549
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 76 deletions.
2 changes: 1 addition & 1 deletion frmts/aigrid/aigdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ GDALDataset *AIGDataset::Open(GDALOpenInfo *poOpenInfo)
if (osCoverName.size() > 4 &&
EQUAL(osCoverName.c_str() + osCoverName.size() - 4, ".adf"))
{
osCoverName = CPLGetDirname(poOpenInfo->pszFilename);
osCoverName = CPLGetDirnameSafe(poOpenInfo->pszFilename);
if (osCoverName == "")
osCoverName = ".";
}
Expand Down
3 changes: 1 addition & 2 deletions frmts/esric/esric_dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,7 @@ GDALDataset *ECDataset::Open(GDALOpenInfo *poOpenInfo,
return nullptr;
}
auto ds = new ECDataset();
ds->dname.Printf("%s/_alllayers",
CPLGetDirname(poOpenInfo->pszFilename));
ds->dname = CPLGetDirnameSafe(poOpenInfo->pszFilename) + "/_alllayers";
CPLErr error = ds->Initialize(CacheInfo);
CPLDestroyXMLNode(config);
if (CE_None != error)
Expand Down
6 changes: 3 additions & 3 deletions frmts/gtiff/gtiffdataset_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5770,12 +5770,12 @@ CSLConstList GTiffDataset::GetSiblingFiles()
m_bHasGotSiblingFiles = true;
const int nMaxFiles =
atoi(CPLGetConfigOption("GDAL_READDIR_LIMIT_ON_OPEN", "1000"));
CPLStringList aosSiblingFiles(
VSIReadDirEx(CPLGetDirnameSafe(m_pszFilename).c_str(), nMaxFiles));
const std::string osDirname = CPLGetDirnameSafe(m_pszFilename);
CPLStringList aosSiblingFiles(VSIReadDirEx(osDirname.c_str(), nMaxFiles));
if (nMaxFiles > 0 && aosSiblingFiles.size() > nMaxFiles)
{
CPLDebug("GTiff", "GDAL_READDIR_LIMIT_ON_OPEN reached on %s",
CPLGetDirname(m_pszFilename));
osDirname.c_str());
aosSiblingFiles.clear();
}
oOvManager.TransferSiblingFiles(aosSiblingFiles.StealList());
Expand Down
19 changes: 12 additions & 7 deletions frmts/jaxapalsar/jaxapalsardataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,15 +579,16 @@ GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo)

/* Try to read each of the polarizations */
const size_t nImgFileLen =
strlen(CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str()) +
strlen(pszSuffix) + 8;
CPLGetDirnameSafe(poOpenInfo->pszFilename).size() + strlen(pszSuffix) +
8;
char *pszImgFile = (char *)CPLMalloc(nImgFileLen);

int nBandNum = 1;

/* HH */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-HH%s",
CPLGetDirname(poOpenInfo->pszFilename), SEP_STRING, pszSuffix);
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpHH = VSIFOpenL(pszImgFile, "rb");
if (fpHH != nullptr)
{
Expand All @@ -597,7 +598,8 @@ GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo)

/* HV */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-HV%s",
CPLGetDirname(poOpenInfo->pszFilename), SEP_STRING, pszSuffix);
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpHV = VSIFOpenL(pszImgFile, "rb");
if (fpHV != nullptr)
{
Expand All @@ -607,7 +609,8 @@ GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo)

/* VH */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-VH%s",
CPLGetDirname(poOpenInfo->pszFilename), SEP_STRING, pszSuffix);
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpVH = VSIFOpenL(pszImgFile, "rb");
if (fpVH != nullptr)
{
Expand All @@ -617,7 +620,8 @@ GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo)

/* VV */
snprintf(pszImgFile, nImgFileLen, "%s%sIMG-VV%s",
CPLGetDirname(poOpenInfo->pszFilename), SEP_STRING, pszSuffix);
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);
VSILFILE *fpVV = VSIFOpenL(pszImgFile, "rb");
if (fpVV != nullptr)
{
Expand Down Expand Up @@ -656,7 +660,8 @@ GDALDataset *PALSARJaxaDataset::Open(GDALOpenInfo *poOpenInfo)
strlen(pszSuffix) + 5;
char *pszLeaderFilename = (char *)CPLMalloc(nLeaderFilenameLen);
snprintf(pszLeaderFilename, nLeaderFilenameLen, "%s%sLED%s",
CPLGetDirname(poOpenInfo->pszFilename), SEP_STRING, pszSuffix);
CPLGetDirnameSafe(poOpenInfo->pszFilename).c_str(), SEP_STRING,
pszSuffix);

VSILFILE *fpLeader = VSIFOpenL(pszLeaderFilename, "rb");
/* check if the leader is actually present */
Expand Down
5 changes: 3 additions & 2 deletions frmts/netcdf/netcdfdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9223,8 +9223,9 @@ netCDFDataset *netCDFDataset::CreateLL(const char *pszFilename, int nXSize,
// Works around bug of msys2 netCDF 4.9.0 package where nc_create()
// crashes
VSIStatBuf sStat;
const char *pszDir = CPLGetDirname(osFilenameForNCCreate.c_str());
if (VSIStat(pszDir, &sStat) != 0)
const std::string osDirname =
CPLGetDirnameSafe(osFilenameForNCCreate.c_str());
if (VSIStat(osDirname.c_str(), &sStat) != 0)
{
CPLError(CE_Failure, CPLE_OpenFailed,
"Unable to create netCDF file %s: non existing output "
Expand Down
37 changes: 19 additions & 18 deletions frmts/nitf/ecrgtocdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ bool ECRGTOCSource::ValidateOpenedBand(GDALRasterBand *poBand) const
/* BuildFullName() */
/************************************************************************/

static const char *BuildFullName(const char *pszTOCFilename,
static std::string BuildFullName(const char *pszTOCFilename,
const char *pszFramePath,
const char *pszFrameName)
{
Expand All @@ -486,24 +486,25 @@ static const char *BuildFullName(const char *pszTOCFilename,
if (pszPath[i] == '\\')
pszPath[i] = '/';
}
const char *pszName = CPLFormFilename(pszPath, pszFrameName, nullptr);
const std::string osName =
CPLFormFilenameSafe(pszPath, pszFrameName, nullptr);
CPLFree(pszPath);
pszPath = nullptr;
const char *pszTOCPath = CPLGetDirname(pszTOCFilename);
const char *pszFirstSlashInName = strchr(pszName, '/');
if (pszFirstSlashInName != nullptr)
std::string osTOCPath = CPLGetDirnameSafe(pszTOCFilename);
const auto nPosFirstSlashInName = osName.find('/');
if (nPosFirstSlashInName != std::string::npos)
{
int nFirstDirLen = static_cast<int>(pszFirstSlashInName - pszName);
if (static_cast<int>(strlen(pszTOCPath)) >= nFirstDirLen + 1 &&
(pszTOCPath[strlen(pszTOCPath) - (nFirstDirLen + 1)] == '/' ||
pszTOCPath[strlen(pszTOCPath) - (nFirstDirLen + 1)] == '\\') &&
strncmp(pszTOCPath + strlen(pszTOCPath) - nFirstDirLen, pszName,
nFirstDirLen) == 0)
if (osTOCPath.size() >= nPosFirstSlashInName + 1 &&
(osTOCPath[osTOCPath.size() - (nPosFirstSlashInName + 1)] == '/' ||
osTOCPath[osTOCPath.size() - (nPosFirstSlashInName + 1)] ==
'\\') &&
strncmp(osTOCPath.c_str() + osTOCPath.size() - nPosFirstSlashInName,
osName.c_str(), nPosFirstSlashInName) == 0)
{
pszTOCPath = CPLGetDirname(pszTOCPath);
osTOCPath = CPLGetDirnameSafe(osTOCPath.c_str());
}
}
return CPLProjectRelativeFilename(pszTOCPath, pszName);
return CPLProjectRelativeFilenameSafe(osTOCPath.c_str(), osName.c_str());
}

/************************************************************************/
Expand Down Expand Up @@ -571,7 +572,7 @@ GDALDataset *ECRGTOCSubDataset::Build(

for (int i = 0; i < static_cast<int>(aosFrameDesc.size()); i++)
{
const char *pszName = BuildFullName(
const std::string osName = BuildFullName(
pszTOCFilename, aosFrameDesc[i].pszPath, aosFrameDesc[i].pszName);

double dfMinX = 0.0;
Expand All @@ -590,7 +591,7 @@ GDALDataset *ECRGTOCSubDataset::Build(
static_cast<int>((dfMaxY - dfMinY) / dfPixelYSize + 0.5);

poVirtualDS->papszFileList =
CSLAddString(poVirtualDS->papszFileList, pszName);
CSLAddString(poVirtualDS->papszFileList, osName.c_str());

for (int j = 0; j < 3; j++)
{
Expand All @@ -599,7 +600,7 @@ GDALDataset *ECRGTOCSubDataset::Build(
poVirtualDS->GetRasterBand(j + 1));
/* Place the raster band at the right position in the VRT */
auto poSource = new ECRGTOCSource(
pszName, j + 1, nFrameXSize, nFrameYSize,
osName.c_str(), j + 1, nFrameXSize, nFrameYSize,
static_cast<int>((dfMinX - dfGlobalMinX) / dfGlobalPixelXSize +
0.5),
static_cast<int>((dfGlobalMaxY - dfMaxY) / dfGlobalPixelYSize +
Expand Down Expand Up @@ -849,10 +850,10 @@ GDALDataset *ECRGTOCDataset::Build(const char *pszTOCFilename,

nValidFrames++;

const char *pszFullName = BuildFullName(
const std::string osFullName = BuildFullName(
pszTOCFilename, pszFramePath, pszFrameName);
poDS->papszFileList =
CSLAddString(poDS->papszFileList, pszFullName);
CSLAddString(poDS->papszFileList, osFullName.c_str());

if (!bGlobalExtentValid)
{
Expand Down
10 changes: 5 additions & 5 deletions frmts/sentinel2/sentinel2dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ static bool SENTINEL2GetGranuleList(
const int nOffset =
std::min(nBytes, static_cast<int>(sizeof(szPointerFilename) - 1));
szPointerFilename[nOffset] = '\0';
osDirname = CPLGetDirname(szPointerFilename);
osDirname = CPLGetDirnameSafe(szPointerFilename);
}
#endif

Expand Down Expand Up @@ -2337,7 +2337,7 @@ static bool SENTINEL2GetGranuleList_L1CSafeCompact(
const int nOffset =
std::min(nBytes, static_cast<int>(sizeof(szPointerFilename) - 1));
szPointerFilename[nOffset] = '\0';
osDirname = CPLGetDirname(szPointerFilename);
osDirname = CPLGetDirnameSafe(szPointerFilename);
}
#endif

Expand Down Expand Up @@ -2374,7 +2374,7 @@ static bool SENTINEL2GetGranuleList_L1CSafeCompact(
// --> GRANULE/L1C_T30TXT_A007999_20170102T111441/MTD_TL.xml
oDesc.osMTDTLPath =
osDirname + chSeparator +
CPLGetDirname(CPLGetDirnameSafe(pszImageFile).c_str()) +
CPLGetDirnameSafe(CPLGetDirnameSafe(pszImageFile).c_str()) +
chSeparator + "MTD_TL.xml";
osList.push_back(oDesc);
}
Expand Down Expand Up @@ -2429,7 +2429,7 @@ static bool SENTINEL2GetGranuleList_L2ASafeCompact(
const int nOffset =
std::min(nBytes, static_cast<int>(sizeof(szPointerFilename) - 1));
szPointerFilename[nOffset] = '\0';
osDirname = CPLGetDirname(szPointerFilename);
osDirname = CPLGetDirnameSafe(szPointerFilename);
}
#endif

Expand Down Expand Up @@ -2475,7 +2475,7 @@ static bool SENTINEL2GetGranuleList_L2ASafeCompact(
// --> GRANULE/L1C_T30TXT_A007999_20170102T111441/MTD_TL.xml
oDesc.osMTDTLPath =
osDirname + chSeparator +
CPLGetDirname(CPLGetDirnameSafe(pszImageFile).c_str());
CPLGetDirnameSafe(CPLGetDirnameSafe(pszImageFile).c_str());
if (oDesc.osMTDTLPath.size() < 9)
{
CPLDebug("SENTINEL2", "MTDTL path too short");
Expand Down
23 changes: 13 additions & 10 deletions frmts/vrt/vrtdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,8 @@ GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo)
pszXML = reinterpret_cast<char *>(pabyOut);

char *pszCurDir = CPLGetCurrentDir();
const char *currentVrtFilename =
CPLProjectRelativeFilename(pszCurDir, poOpenInfo->pszFilename);
std::string currentVrtFilename =
CPLProjectRelativeFilenameSafe(pszCurDir, poOpenInfo->pszFilename);
CPLString osInitialCurrentVrtFilename(currentVrtFilename);
CPLFree(pszCurDir);

Expand All @@ -808,7 +808,7 @@ GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo)
while (true)
{
VSIStatBuf statBuffer;
int lstatCode = lstat(currentVrtFilename, &statBuffer);
int lstatCode = lstat(currentVrtFilename.c_str(), &statBuffer);
if (lstatCode == -1)
{
if (errno == ENOENT)
Expand All @@ -821,7 +821,7 @@ GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo)
CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
CPLFree(pszXML);
CPLError(CE_Failure, CPLE_FileIO, "Failed to lstat %s: %s",
currentVrtFilename, VSIStrerror(errno));
currentVrtFilename.c_str(), VSIStrerror(errno));
return nullptr;
}
}
Expand All @@ -831,25 +831,27 @@ GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo)
break;
}

const int bufferSize = static_cast<int>(readlink(
currentVrtFilename, filenameBuffer, sizeof(filenameBuffer)));
const int bufferSize = static_cast<int>(
readlink(currentVrtFilename.c_str(), filenameBuffer,
sizeof(filenameBuffer)));
if (bufferSize != -1)
{
filenameBuffer[std::min(
bufferSize, static_cast<int>(sizeof(filenameBuffer)) - 1)] =
0;
// The filename in filenameBuffer might be a relative path
// from the linkfile resolve it before looping
currentVrtFilename = CPLProjectRelativeFilename(
CPLGetDirname(currentVrtFilename), filenameBuffer);
currentVrtFilename = CPLProjectRelativeFilenameSafe(
CPLGetDirnameSafe(currentVrtFilename.c_str()).c_str(),
filenameBuffer);
}
else
{
CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
CPLFree(pszXML);
CPLError(CE_Failure, CPLE_FileIO,
"Failed to read filename from symlink %s: %s",
currentVrtFilename, VSIStrerror(errno));
currentVrtFilename.c_str(), VSIStrerror(errno));
return nullptr;
}
}
Expand All @@ -859,7 +861,8 @@ GDALDataset *VRTDataset::Open(GDALOpenInfo *poOpenInfo)
pszVRTPath =
CPLStrdup(CPLGetPathSafe(poOpenInfo->pszFilename).c_str());
else
pszVRTPath = CPLStrdup(CPLGetPathSafe(currentVrtFilename).c_str());
pszVRTPath =
CPLStrdup(CPLGetPathSafe(currentVrtFilename.c_str()).c_str());

CPL_IGNORE_RET_VAL(VSIFCloseL(fp));
}
Expand Down
3 changes: 1 addition & 2 deletions frmts/wms/gdalwmscache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ class GDALWMSFileCache : public GDALWMSCacheImpl
return;
}
// Recursive makedirs, ignoring errors
const char *pszDirPath = CPLGetDirname(pszPath);
MakeDirs(pszDirPath);
MakeDirs(CPLGetDirnameSafe(pszPath).c_str());

VSIMkdir(pszPath, 0744);
}
Expand Down
6 changes: 3 additions & 3 deletions frmts/zarr/zarr_sharedresource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ std::shared_ptr<ZarrGroupBase> ZarrSharedResource::OpenRootGroup()
{
// If opening a NCZarr array, initialize its group from NCZarr
// metadata.
const std::string osGroupFilename(CPLFormFilename(
CPLGetDirname(m_osRootDirectoryName.c_str()), ".zgroup",
nullptr));
const std::string osGroupFilename(CPLFormFilenameSafe(
CPLGetDirnameSafe(m_osRootDirectoryName.c_str()).c_str(),
".zgroup", nullptr));
if (VSIStatL(osGroupFilename.c_str(), &sStat) == 0)
{
CPLJSONDocument oDocGroup;
Expand Down
16 changes: 9 additions & 7 deletions frmts/zarr/zarr_v2_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ void ZarrV2Array::Flush()

CPLJSONDocument oDoc;
oDoc.SetRoot(oAttrs);
const std::string osAttrFilename = CPLFormFilename(
CPLGetDirname(m_osFilename.c_str()), ".zattrs", nullptr);
const std::string osAttrFilename =
CPLFormFilenameSafe(CPLGetDirnameSafe(m_osFilename.c_str()).c_str(),
".zattrs", nullptr);
oDoc.Save(osAttrFilename);
m_poSharedResource->SetZMetadataItem(osAttrFilename, oAttrs);
}
Expand Down Expand Up @@ -987,8 +988,8 @@ std::string ZarrV2Array::BuildTileFilename(const uint64_t *tileIndices) const
}
}

return CPLFormFilename(CPLGetDirnameSafe(m_osFilename.c_str()).c_str(),
osFilename.c_str(), nullptr);
return CPLFormFilenameSafe(CPLGetDirnameSafe(m_osFilename.c_str()).c_str(),
osFilename.c_str(), nullptr);
}

/************************************************************************/
Expand All @@ -997,7 +998,7 @@ std::string ZarrV2Array::BuildTileFilename(const uint64_t *tileIndices) const

std::string ZarrV2Array::GetDataDirectory() const
{
return std::string(CPLGetDirnameSafe(m_osFilename.c_str()));
return CPLGetDirnameSafe(m_osFilename.c_str());
}

/************************************************************************/
Expand Down Expand Up @@ -1328,8 +1329,9 @@ ZarrV2Group::LoadArray(const std::string &osArrayName,
if (!bLoadedFromZMetadata)
{
CPLJSONDocument oDoc;
const std::string osZattrsFilename(CPLFormFilename(
CPLGetDirname(osZarrayFilename.c_str()), ".zattrs", nullptr));
const std::string osZattrsFilename(CPLFormFilenameSafe(
CPLGetDirnameSafe(osZarrayFilename.c_str()).c_str(), ".zattrs",
nullptr));
CPLErrorStateBackuper oErrorStateBackuper(CPLQuietErrorHandler);
if (oDoc.Load(osZattrsFilename))
{
Expand Down
6 changes: 3 additions & 3 deletions ogr/ogrsf_frmts/gmlas/ogrgmlasxsdcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ VSILFILE *GMLASXSDCache::Open(const std::string &osResource,
STARTS_WITH(osResourceModified.c_str(), "..\\")) &&
!osBasePathModified.empty())
{
osBasePathModified = CPLGetDirname(osBasePathModified.c_str());
osBasePathModified = CPLGetDirnameSafe(osBasePathModified.c_str());
osResourceModified = osResourceModified.substr(3);
}

osOutFilename = CPLFormFilename(osBasePathModified.c_str(),
osResourceModified.c_str(), nullptr);
osOutFilename = CPLFormFilenameSafe(
osBasePathModified.c_str(), osResourceModified.c_str(), nullptr);
}

CPLDebug("GMLAS", "Resolving %s (%s) to %s", osResource.c_str(),
Expand Down
Loading

0 comments on commit 9a9d549

Please sign in to comment.