Use dirname (3) for %dirname

This changes the behaviour of %dirname to something sane. No longer
return the argument unchanged if there is no / found. Also handle
several cornercases properly.

Resolves: #2928
This commit is contained in:
Florian Festi 2024-03-05 11:40:19 +01:00
parent 8eac330426
commit 9571e3d9a2
2 changed files with 24 additions and 7 deletions

View File

@ -6,6 +6,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <pthread.h> #include <pthread.h>
#include <errno.h> #include <errno.h>
#include <libgen.h>
#ifdef HAVE_SCHED_GETAFFINITY #ifdef HAVE_SCHED_GETAFFINITY
#include <sched.h> #include <sched.h>
#endif #endif
@ -1306,9 +1307,7 @@ static void doFoo(rpmMacroBuf mb, rpmMacroEntry me, ARGV_t argv, size_t *parsed)
b++; b++;
} else if (rstreq("dirname", me->name)) { } else if (rstreq("dirname", me->name)) {
buf = xstrdup(argv[1]); buf = xstrdup(argv[1]);
if ((b = strrchr(buf, '/')) != NULL) b = dirname(buf);
*b = '\0';
b = buf;
} else if (rstreq("shrink", me->name)) { } else if (rstreq("shrink", me->name)) {
/* /*
* shrink body by removing all leading and trailing whitespaces and * shrink body by removing all leading and trailing whitespaces and

View File

@ -364,6 +364,15 @@ runroot rpm --eval "%{dirname:dir}"
runroot rpm --eval "%{dirname dir}" runroot rpm --eval "%{dirname dir}"
runroot rpm --eval "%dirname" runroot rpm --eval "%dirname"
runroot rpm --eval "%dirname dir" runroot rpm --eval "%dirname dir"
runroot rpm --eval "%dirname /"
runroot rpm --eval "%dirname ./"
runroot rpm --eval "%dirname .."
runroot rpm --eval "%dirname ../"
runroot rpm --eval "%dirname ../foo"
runroot rpm --eval "%dirname /foo"
runroot rpm --eval "%dirname /foo/"
runroot rpm --eval "%dirname /foo/foobar"
runroot rpm --eval "%dirname /foo/foobar/"
runroot rpm --define '%xxx /hello/%%%%/world' --eval '%{dirname:%xxx}' runroot rpm --define '%xxx /hello/%%%%/world' --eval '%{dirname:%xxx}'
runroot rpm --eval "%{uncompress}" runroot rpm --eval "%{uncompress}"
runroot rpm --eval "%{uncompress:}" runroot rpm --eval "%{uncompress:}"
@ -381,10 +390,19 @@ runroot rpm --eval "%shrink %%%%"
runroot rpm --eval "%verbose foo" runroot rpm --eval "%verbose foo"
], ],
[0], [0],
[ [.
dir .
dir .
dir .
/
.
.
.
..
/
/
/foo
/foo
/hello/%% /hello/%%
bar bar