From 8f05990d5ae3bf62e725110f5228821f56cd765a Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Fri, 12 Nov 2021 15:28:05 -0800 Subject: [PATCH] Extend GetZipPaths to accept an optional prefix (#320) --- tool/net/help.txt | 5 +++-- tool/net/redbean.c | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tool/net/help.txt b/tool/net/help.txt index 2e7733745..5fae78252 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -746,9 +746,10 @@ FUNCTIONS minor (mm), and patch (pp) versions encoded. The version value 1.4 would be represented as 0x010400. - GetZipPaths() → {path:str,...} + GetZipPaths([prefix:str]) → {path:str,...} Returns paths of all assets in the zip central directory, prefixed - by a slash. + by a slash. If prefix parameter is provided, then only paths that + start with the prefix (case sensitive) are returned. HasParam(name:str) → bool Returns true if parameter with name was supplied in either the diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 07a79ee03..e73144d68 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -5014,7 +5014,8 @@ static int LuaIsHiddenPath(lua_State *L) { static int LuaGetZipPaths(lua_State *L) { char *path; uint8_t *zcf; - size_t i, n, pathlen; + size_t i, n, pathlen, prefixlen; + char *prefix = luaL_optlstring(L, 1, "", &prefixlen); lua_newtable(L); i = 0; n = GetZipCdirRecords(zcdir); @@ -5022,8 +5023,10 @@ static int LuaGetZipPaths(lua_State *L) { zcf += ZIP_CFILE_HDRSIZE(zcf)) { CHECK_EQ(kZipCfileHdrMagic, ZIP_CFILE_MAGIC(zcf)); path = GetAssetPath(zcf, &pathlen); - lua_pushlstring(L, path, pathlen); - lua_seti(L, -2, ++i); + if (prefixlen == 0 || startswith(path, prefix)) { + lua_pushlstring(L, path, pathlen); + lua_seti(L, -2, ++i); + } free(path); } return 1;