From a9169c46abddfe620c6ae83f3064b50c8bb35852 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Wed, 5 Nov 2008 15:44:35 +0200 Subject: [PATCH] Calculate filename buffer len lazily in rpmfiFN() as needed - avoids having to separately calculate in genCpioListAndHeader() - avoids unnecessary fnlen field in rpmfi struct - avoids having to calculate it at all if rpmfiFN() is never called --- build/files.c | 4 ---- lib/rpmfi.c | 29 +++++++++++++---------------- lib/rpmfi_internal.h | 1 - 3 files changed, 13 insertions(+), 21 deletions(-) diff --git a/build/files.c b/build/files.c index 5d5856421..088c8b3af 100644 --- a/build/files.c +++ b/build/files.c @@ -1039,7 +1039,6 @@ static void genCpioListAndHeader(FileList fl, size_t apathlen = 0; size_t dpathlen = 0; size_t skipLen = 0; - size_t fnlen; FileListRec flp; char buf[BUFSIZ]; int i; @@ -1298,9 +1297,6 @@ static void genCpioListAndHeader(FileList fl, continue; } - if ((fnlen = strlen(flp->diskPath) + 1) > fi->fnlen) - fi->fnlen = fnlen; - /* Create disk directory and base name. */ fi->dil[i] = i; /* FIX: artifact of spoofing headerGetEntry */ diff --git a/lib/rpmfi.c b/lib/rpmfi.c index 5f25f4b76..cb7f08905 100644 --- a/lib/rpmfi.c +++ b/lib/rpmfi.c @@ -117,8 +117,18 @@ const char * rpmfiFN(rpmfi fi) if (fi != NULL && fi->i >= 0 && fi->i < fi->fc) { char * t; - if (fi->fn == NULL) - fi->fn = xmalloc(fi->fnlen); + if (fi->fn == NULL) { + size_t dnlmax = 0, bnlmax = 0, len; + for (int i = 0; i < fi->dc; i++) { + if ((len = strlen(fi->dnl[i])) > dnlmax) + dnlmax = len; + } + for (int i = 0; i < fi->fc; i++) { + if ((len = strlen(fi->bnl[i])) > bnlmax) + bnlmax = len; + } + fi->fn = xmalloc(dnlmax + bnlmax + 1); + } FN = t = fi->fn; *t = '\0'; t = stpcpy(t, fi->dnl[fi->dil[fi->i]]); @@ -1185,15 +1195,12 @@ rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, rpmfiFlags flags) rpmfi fi = NULL; const char * Type; rpm_loff_t *asize = NULL; - int dnlmax, bnlmax; unsigned char * t; struct rpmtd_s fdigests, digalgo; struct rpmtd_s td; headerGetFlags scareFlags = (flags & RPMFI_KEEPHEADER) ? HEADERGET_MINMEM : HEADERGET_ALLOC; headerGetFlags defFlags = HEADERGET_ALLOC; - int len; - int i; if (tagN == RPMTAG_BASENAMES) { Type = "Files"; @@ -1343,17 +1350,7 @@ rpmfi rpmfiNew(const rpmts ts, Header h, rpmTag tagN, rpmfiFlags flags) fi->h = headerFree(fi->h); } - dnlmax = -1; - for (i = 0; i < fi->dc; i++) { - if ((len = strlen(fi->dnl[i])) > dnlmax) - dnlmax = len; - } - bnlmax = -1; - for (i = 0; i < fi->fc; i++) { - if ((len = strlen(fi->bnl[i])) > bnlmax) - bnlmax = len; - } - fi->fnlen = dnlmax + bnlmax + 1; + /* lazily alloced from rpmfiFN() */ fi->fn = NULL; exit: diff --git a/lib/rpmfi_internal.h b/lib/rpmfi_internal.h index 349d1116b..104903617 100644 --- a/lib/rpmfi_internal.h +++ b/lib/rpmfi_internal.h @@ -96,7 +96,6 @@ struct rpmfi_s { int transscripts; /*!< pre/posttrans script existence */ char * fn; /*!< File name buffer. */ - size_t fnlen; /*!< FIle name buffer length. */ size_t astriplen; size_t striplen;