Change fpLookup() to return malloced memory (on first call)
authorPanu Matilainen <pmatilai@redhat.com>
Fri, 14 Sep 2012 05:41:01 +0000 (08:41 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Fri, 14 Sep 2012 10:40:24 +0000 (13:40 +0300)
- If the fingerprint pointer passed to it is NULL then allocate space
  for a new fingerprint, otherwise reuse the previous space. This should
  allow optimizing the case where repeatedly calling and directory
  doesn't change inside fpc so callers dont need special-case code
  for this. For now, we dont care about optimizations, other than
  making it possible later.

lib/fprint.c
lib/fprint.h
lib/rpmdb.c
lib/transaction.c

index a31e546..a0aef4a 100644 (file)
@@ -108,7 +108,7 @@ static const struct fprintCacheEntry_s * cacheContainsDirectory(
     return NULL;
 }
 
-int fpLookup(fingerPrintCache cache,
+static int doLookup(fingerPrintCache cache,
             const char * dirName, const char * baseName, int scareMemory,
             fingerPrint *fp)
 {
@@ -230,6 +230,15 @@ exit:
     return 0;
 }
 
+int fpLookup(fingerPrintCache cache,
+             const char * dirName, const char * baseName, int scareMemory,
+             fingerPrint **fp)
+{
+    if (*fp == NULL)
+       *fp = xcalloc(1, sizeof(**fp));
+    return doLookup(cache, dirName, baseName, scareMemory, *fp);
+}
+
 /**
  * Return hash value for a finger print.
  * Hash based on dev and inode only!
@@ -277,7 +286,7 @@ int fpLookupEquals(fingerPrintCache cache, fingerPrint *fp,
                  const char * dirName, const char * baseName)
 {
     struct fingerPrint_s ofp;
-    fpLookup(cache, dirName, baseName, 1, &ofp);
+    doLookup(cache, dirName, baseName, 1, &ofp);
     return FP_EQUAL(*fp, ofp);
 }
 
@@ -297,7 +306,7 @@ fingerPrint * fpLookupList(fingerPrintCache cache, rpmstrPool pool,
            fps[i].subDir = fps[i - 1].subDir;
            fps[i].baseName = rpmstrPoolStr(pool, baseNames[i]);
        } else {
-           fpLookup(cache,
+           doLookup(cache,
                     rpmstrPoolStr(pool, dirNames[dirIndexes[i]]),
                     rpmstrPoolStr(pool, baseNames[i]),
                     1, &fps[i]);
@@ -364,7 +373,7 @@ static void fpLookupSubdir(rpmFpHash symlinks, fingerPrintCache fpc, rpmte p, in
                        rstrscat(&link, endbasename+1, "/", NULL);
                   }
 
-                  fpLookup(fpc, link, fp->baseName, 0, fp);
+                  doLookup(fpc, link, fp->baseName, 0, fp);
 
                   free(link);
                   free(currentsubdir);
index 1486753..2a2b7cf 100644 (file)
@@ -84,7 +84,7 @@ dev_t fpEntryDev(fingerPrintCache cache, fingerPrint *fp);
 RPM_GNUC_INTERNAL
 int fpLookup(fingerPrintCache cache,
             const char * dirName, const char * baseName, int scareMemory,
-            fingerPrint *fp);
+            fingerPrint **fp);
 
 /**
  * Compare two finger print entries.
index a6600cd..22d2fe7 100644 (file)
@@ -933,7 +933,7 @@ static int rpmdbFindByFile(rpmdb db, dbiIndex dbi, const char *filespec,
     char * dirName = NULL;
     const char * baseName;
     fingerPrintCache fpc = NULL;
-    fingerPrint fp1;
+    fingerPrint * fp1 = NULL;
     dbiIndexSet allMatches = NULL;
     unsigned int i;
     int rc = -2; /* assume error */
@@ -997,7 +997,7 @@ static int rpmdbFindByFile(rpmdb db, dbiIndex dbi, const char *filespec,
 
            if (!skip) {
                const char *dirName = dirNames[dirIndexes[num]];
-               if (fpLookupEquals(fpc, &fp1, dirName, baseNames[num])) {
+               if (fpLookupEquals(fpc, fp1, dirName, baseNames[num])) {
                    struct dbiIndexItem rec = { 
                        .hdrNum = dbiIndexRecordOffset(allMatches, i),
                        .tagNum = dbiIndexRecordFileNumber(allMatches, i),
@@ -1020,6 +1020,7 @@ static int rpmdbFindByFile(rpmdb db, dbiIndex dbi, const char *filespec,
        headerFree(h);
     }
 
+    free(fp1);
     fpCacheFree(fpc);
 
     if ((*matches)->count == 0) {
index 8bcf5aa..1b00194 100644 (file)
@@ -971,7 +971,7 @@ void checkInstalledFiles(rpmts ts, uint64_t fileCount, fingerPrintCache fpc)
     while (h != NULL) {
        headerGetFlags hgflags = HEADERGET_MINMEM;
        struct rpmtd_s bnames, dnames, dindexes, ostates;
-       fingerPrint fp, *fpp;
+       fingerPrint *fpp = NULL;
        unsigned int installedPkg;
        int beingRemoved = 0;
        rpmfi otherFi = NULL;
@@ -1014,14 +1014,7 @@ void checkInstalledFiles(rpmts ts, uint64_t fileCount, fingerPrintCache fpc)
                dirName = rpmtdGetString(&dnames);
                baseName = rpmtdGetString(&bnames);
 
-               if (dirName == oldDir) {
-                   /* directory is the same as last round */
-                   fp.baseName = baseName;
-               } else {
-                   fpLookup(fpc, dirName, baseName, 1, &fp);
-                   oldDir = dirName;
-               }
-               fpp = &fp;
+               fpLookup(fpc, dirName, baseName, 1, &fpp);
            } else {
                fpp = rpmfiFpsIndex(otherFi, fileNum);
            }
@@ -1063,6 +1056,7 @@ void checkInstalledFiles(rpmts ts, uint64_t fileCount, fingerPrintCache fpc)
            rpmtdFreeData(&bnames);
            rpmtdFreeData(&dnames);
            rpmtdFreeData(&dindexes);
+           free(fpp);
        }
        headerFree(h);
        h = newheader;