diff --git a/lib/query.cc b/lib/query.cc index 7b36ee015e..761725fcf6 100644 --- a/lib/query.cc +++ b/lib/query.cc @@ -5,6 +5,9 @@ #include "system.h" +#include +#include + #include #include #include @@ -16,11 +19,12 @@ #include #include #include -#include /* rpmCleanPath */ +#include #include #include "rpmgi.hh" #include "manifest.hh" +#include "rpmmacro_internal.hh" #include "debug.h" @@ -405,43 +409,40 @@ static rpmdbMatchIterator initQueryIterator(QVA_t qva, rpmts ts, const char * ar /* fallthrough on absolute and relative paths */ case RPMQV_PATH: case RPMQV_PATH_ALL: - { char * fn; + { std::string fn; for (s = arg; *s != '\0'; s++) if (!(*s == '.' || *s == '/')) break; if (*s == '\0') { - fn = realpath(arg, NULL); - if (!fn) - fn = xstrdup(arg); + std::error_code ec {}; + fn = std::filesystem::canonical(arg); + if (ec) + fn = rpm::normalize_path(arg); } else if (*arg != '/') { - char *curDir = rpmGetCwd(); - fn = (char *) rpmGetPath(curDir, "/", arg, NULL); - free(curDir); - } else - fn = xstrdup(arg); - (void) rpmCleanPath(fn); + fn = rpm::join_path({std::filesystem::current_path(), arg}, false); + } else { + fn = rpm::normalize_path(arg); + } rpmDbiTagVal tag = RPMDBI_INSTFILENAMES; if (qva->qva_source == RPMQV_PATH_ALL) tag = RPMDBI_BASENAMES; - mi = rpmtsInitIterator(ts, tag, fn, 0); + mi = rpmtsInitIterator(ts, tag, fn.c_str(), 0); if (mi == NULL) - mi = rpmtsInitIterator(ts, RPMDBI_PROVIDENAME, fn, 0); + mi = rpmtsInitIterator(ts, RPMDBI_PROVIDENAME, fn.c_str(), 0); if (mi == NULL) { struct stat sb; - char * full_fn = rpmGetPath(rpmtsRootDir(ts), fn, NULL); - if (lstat(full_fn, &sb) != 0) - rpmlog(RPMLOG_ERR, _("file %s: %s\n"), fn, strerror(errno)); + auto full_fn = rpm::join_path({rpmtsRootDir(ts), fn}, false); + if (lstat(full_fn.c_str(), &sb) != 0) + rpmlog(RPMLOG_ERR, _("file %s: %s\n"), fn.c_str(), strerror(errno)); else rpmlog(RPMLOG_NOTICE, - _("file %s is not owned by any package\n"), fn); - free(full_fn); + _("file %s is not owned by any package\n"), fn.c_str()); } - free(fn); } break; case RPMQV_DBOFFSET: