From 15261161925e8dc2c2afa80e74686b771ab28e4c Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Mon, 15 Nov 2021 17:19:23 -0800 Subject: [PATCH] Fix StoreAsset update for existing assets in redbean --- tool/net/redbean.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 2faab285e..300c93a17 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -3303,11 +3303,15 @@ static int LuaStoreAsset(lua_State *L) { oldcdirsize = GetZipCdirSize(zcdir); oldcdiroffset = GetZipCdirOffset(zcdir); if (a) { + // to remove an existing asset, + // first copy the central directory part before its record v[4].iov_base = zbase + oldcdiroffset; v[4].iov_len = a->cf - oldcdiroffset; - v[5].iov_base = zbase + oldcdiroffset + ZIP_CFILE_HDRSIZE(zbase + a->cf); - v[5].iov_len = - oldcdirsize - v[4].iov_len - ZIP_CFILE_HDRSIZE(zbase + a->cf); + // and then the rest of the central directory + v[5].iov_base = zbase + oldcdiroffset + + (v[4].iov_len + ZIP_CFILE_HDRSIZE(zbase + a->cf)); + v[5].iov_len = oldcdirsize - + (v[4].iov_len + ZIP_CFILE_HDRSIZE(zbase + a->cf)); } else { v[4].iov_base = zbase + oldcdiroffset; v[4].iov_len = oldcdirsize;