Skip to content

Commit 93dcd87

Browse files
committed
Lua: Return empty result for no matches for glob()
If "c" is not given in the a second parameter glob should not return any results if the pattern doesn't match any files. Fix the condition that checks this parameter. Also don't error out if rpmGlob returns GLOB_NOMATCH which is expected in this case. Add a test case. Resolves: rpm-software-management#3794
1 parent ef948df commit 93dcd87

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

rpmio/rpmlua.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdarg.h>
1818
#include <errno.h>
1919
#include <fcntl.h>
20+
#include <glob.h>
2021

2122
#include <rpm/rpmio.h>
2223
#include <rpm/rpmmacro.h>
@@ -937,19 +938,21 @@ static int rpm_glob(lua_State *L)
937938
const char *pat = luaL_checkstring(L, 1);
938939
rpmglobFlags flags = RPMGLOB_NONE;
939940
int argc = 0;
941+
int rc;
940942
ARGV_t argv = NULL;
941943

942-
if (luaL_optstring(L, 2, "c"))
944+
if (strchr(luaL_optstring(L, 2, ""), 'c'))
943945
flags |= RPMGLOB_NOCHECK;
944946

945-
if (rpmGlobPath(pat, flags, &argc, &argv) == 0) {
947+
rc = rpmGlobPath(pat, flags, &argc, &argv);
948+
if (rc == 0) {
946949
lua_createtable(L, 0, argc);
947950
for (int i = 0; i < argc; i++) {
948951
lua_pushstring(L, argv[i]);
949952
lua_rawseti(L, -2, i + 1);
950953
}
951954
argvFree(argv);
952-
} else {
955+
} else if (rc != GLOB_NOMATCH) {
953956
luaL_error(L, "glob %s failed: %s", pat, strerror(errno));
954957
}
955958

tests/rpmmacro.at

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ RPMTEST_SETUP([lua glob])
10091009
RPMTEST_CHECK([
10101010
mkdir -p aaa/{123,223,323,322,321}
10111011
rpm --eval "%{lua:for i,p in ipairs(rpm.glob('aaa/3*')) do print(p..'\\n') end}"
1012+
rpm --eval "%{lua:for i,p in ipairs(rpm.glob('aaa/b*')) do print(p..'\\n') end}"
10121013
rpm --eval "%{lua:for i,p in ipairs(rpm.glob('aaa/b*', 'c')) do print(p..'\\n') end}"
10131014
],
10141015
[0],

0 commit comments

Comments
 (0)