From c2fdf4387278ca6b40cf14adc6359eaed09ff8f6 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Tue, 12 Nov 2024 12:23:10 +0200 Subject: [PATCH] Eliminate uses of realpath() and rpmCleanPath() in fingerprint canonDir() Use filesystem::canonical() instead of realpath() and the new rpm::join_path() and rpm::normalize_path() for the other path manipulation. Also return a C++ string to the sole caller. Drop some bogus checks and comments: canonDir() could not return NULL for a long long time. One could argue that there's more than one change here but going from C strings to C++ strings tends to have a bit of a domino effect. No functional changes intended. --- lib/fprint.cc | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/fprint.cc b/lib/fprint.cc index 436d607f77..26a3662272 100644 --- a/lib/fprint.cc +++ b/lib/fprint.cc @@ -4,9 +4,11 @@ #include "system.h" +#include +#include #include -#include /* for rpmCleanPath */ +#include #include #include #include @@ -14,6 +16,7 @@ #include "rpmdb_internal.hh" #include "rpmfi_internal.hh" #include "rpmte_internal.hh" +#include "rpmmacro_internal.hh" #include "fprint.hh" #include "misc.hh" #include "debug.h" @@ -134,32 +137,31 @@ static const struct fprintCacheEntry_s * cacheContainsDirectory( return NULL; } -static char * canonDir(rpmstrPool pool, rpmsid dirNameId) +static std::string canonDir(rpmstrPool pool, rpmsid dirNameId) { const char * dirName = rpmstrPoolStr(pool, dirNameId); - char *cdnbuf = NULL; + std::string cdnbuf; if (*dirName == '/') { - cdnbuf = xstrdup(dirName); + cdnbuf = dirName; } else { - /* Using realpath on the arg isn't correct if the arg is a symlink, + /* Using canonical on the arg isn't correct if the arg is a symlink, * especially if the symlink is a dangling link. What we - * do instead is use realpath() on `.' and then append arg to + * do instead is use canonical() on `.' and then append arg to * the result. */ /* if the current directory doesn't exist, we might fail. oh well. */ - if ((cdnbuf = realpath(".", NULL)) != NULL) - cdnbuf = rstrscat(&cdnbuf, "/", dirName, NULL); + std::error_code ec {}; + cdnbuf = std::filesystem::canonical(".", ec); + if (ec) + cdnbuf += rpm::join_path({cdnbuf, dirName}, false); } /* ensure canonical path with a trailing slash */ - if (cdnbuf) { - size_t cdnl = strlen(cdnbuf); - if (cdnl > 1) { - cdnbuf = rpmCleanPath(cdnbuf); - cdnbuf = rstrcat(&cdnbuf, "/"); - } + if (cdnbuf.empty() == false) { + cdnbuf = rpm::normalize_path(cdnbuf); + cdnbuf += '/'; } return cdnbuf; } @@ -171,12 +173,11 @@ static int doLookupId(fingerPrintCache cache, { struct stat sb; const struct fprintCacheEntry_s * cacheHit; - char *cdn = canonDir(cache->pool, dirNameId); + std::string dir = canonDir(cache->pool, dirNameId); + const char *cdn = dir.c_str(); rpmsid fpId; size_t fpLen; - if (cdn == NULL) goto exit; /* XXX only if realpath() above fails */ - memset(fp, 0, sizeof(*fp)); fpId = rpmstrPoolId(cache->pool, cdn, 1); fpLen = rpmstrPoolStrlen(cache->pool, fpId);; @@ -219,8 +220,7 @@ static int doLookupId(fingerPrintCache cache, } exit: - free(cdn); - /* XXX TODO: failure from eg realpath() should be returned and handled */ + /* XXX TODO: failure from eg canonical() should be returned and handled */ return 0; }