Skip to content

Commit

Permalink
Use dirname (3) for %dirname
Browse files Browse the repository at this point in the history
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: rpm-software-management#2928
  • Loading branch information
ffesti committed Mar 7, 2024
1 parent 8eac330 commit 77d02b7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
5 changes: 2 additions & 3 deletions rpmio/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdarg.h>
#include <pthread.h>
#include <errno.h>
#include <libgen.h>
#ifdef HAVE_SCHED_GETAFFINITY
#include <sched.h>
#endif
Expand Down Expand Up @@ -1306,9 +1307,7 @@ static void doFoo(rpmMacroBuf mb, rpmMacroEntry me, ARGV_t argv, size_t *parsed)
b++;
} else if (rstreq("dirname", me->name)) {
buf = xstrdup(argv[1]);
if ((b = strrchr(buf, '/')) != NULL)
*b = '\0';
b = buf;
b = dirname(buf);
} else if (rstreq("shrink", me->name)) {
/*
* shrink body by removing all leading and trailing whitespaces and
Expand Down
26 changes: 22 additions & 4 deletions tests/rpmmacro.at
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,15 @@ runroot rpm --eval "%{dirname:dir}"
runroot rpm --eval "%{dirname dir}"
runroot rpm --eval "%dirname"
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 --eval "%{uncompress}"
runroot rpm --eval "%{uncompress:}"
Expand All @@ -381,10 +390,19 @@ runroot rpm --eval "%shrink %%%%"
runroot rpm --eval "%verbose foo"
],
[0],
[
dir
dir
dir
[.
.
.
.
/
.
.
.
..
/
/
/foo
/foo
/hello/%%

bar
Expand Down

0 comments on commit 77d02b7

Please sign in to comment.