Skip to content

Commit

Permalink
Make %dirname behave like dirname (3)
Browse files Browse the repository at this point in the history
Strip trailing '/' unless "/" itself
Keep "." untouched
Keep root dir for files in the root dir
Return "." for files without directory and empty strings

See man 3 dirname

Resolves: rpm-software-management#2928
  • Loading branch information
ffesti committed Mar 5, 2024
1 parent 233ddeb commit cbff994
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
18 changes: 16 additions & 2 deletions rpmio/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -1306,8 +1306,22 @@ 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';
if (!strcmp(buf, "")) {
buf = rstrcat(&buf, ".");
} else {
/* strip trailing / */
size_t l = strlen(buf);
if (l > 1 && buf[l-1] == '/')
buf[l-1] = '\0';
if ((b = strrchr(buf, '/')) != NULL)
if (b == buf)
/* keep root dir */
buf[1] = '\0';
else
*b = '\0';
else
strcpy(buf, ".");
}
b = buf;
} else if (rstreq("shrink", me->name)) {
/*
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 cbff994

Please sign in to comment.