Return fingerprint lookups through retval pointer, not struct
authorPanu Matilainen <pmatilai@redhat.com>
Thu, 6 Sep 2012 09:53:05 +0000 (12:53 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Thu, 6 Sep 2012 09:53:05 +0000 (12:53 +0300)
- Returning structs by value is a bit icky, pass in a fp pointer
  for fpLookup() to fill in instead. This leaves the actual return code
  free for handling errors (but ignoring that for now as we always have)
  The other option would be always mallocing the return, and we dont
  want to do that...
- Shouldn't change any actual functionality.

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

index a579f6f..261ad03 100644 (file)
@@ -62,14 +62,14 @@ static const struct fprintCacheEntry_s * cacheContainsDirectory(
     return NULL;
 }
 
-fingerPrint fpLookup(fingerPrintCache cache,
-               const char * dirName, const char * baseName, int scareMemory)
+int fpLookup(fingerPrintCache cache,
+            const char * dirName, const char * baseName, int scareMemory,
+            fingerPrint *fp)
 {
     char dir[PATH_MAX];
     const char * cleanDirName;
     size_t cdnl;
     char * end;                    /* points to the '\0' at the end of "buf" */
-    fingerPrint fp;
     struct stat sb;
     char *buf = NULL;
     char *cdnbuf = NULL;
@@ -117,9 +117,8 @@ fingerPrint fpLookup(fingerPrintCache cache,
            cdnl = end - dir;
        }
     }
-    fp.entry = NULL;
-    fp.subDir = NULL;
-    fp.baseName = NULL;
+
+    memset(fp, 0, sizeof(*fp));
     if (cleanDirName == NULL) goto exit; /* XXX can't happen */
 
     buf = xstrdup(cleanDirName);
@@ -139,31 +138,30 @@ fingerPrint fpLookup(fingerPrintCache cache,
        /* as we're stating paths here, we want to follow symlinks */
        cacheHit = cacheContainsDirectory(cache, fpDir, fpHash);
        if (cacheHit != NULL) {
-           fp.entry = cacheHit;
+           fp->entry = cacheHit;
        } else if (!stat(fpDir, &sb)) {
            struct fprintCacheEntry_s * newEntry = xmalloc(sizeof(* newEntry));
 
            newEntry->ino = sb.st_ino;
            newEntry->dev = sb.st_dev;
            newEntry->dirName = xstrdup(fpDir);
-           fp.entry = newEntry;
+           fp->entry = newEntry;
 
            rpmFpEntryHashAddHEntry(cache->ht,
-                                   newEntry->dirName, fpHash, fp.entry);
+                                   newEntry->dirName, fpHash, fp->entry);
        }
 
-        if (fp.entry) {
-           fp.subDir = cleanDirName + (end - buf);
-           if (fp.subDir[0] == '/' && fp.subDir[1] != '\0')
-               fp.subDir++;
-           if (fp.subDir[0] == '\0' ||
+        if (fp->entry) {
+           fp->subDir = cleanDirName + (end - buf);
+           if (fp->subDir[0] == '/' && fp->subDir[1] != '\0')
+               fp->subDir++;
+           if (fp->subDir[0] == '\0' ||
            /* XXX don't bother saving '/' as subdir */
-              (fp.subDir[0] == '/' && fp.subDir[1] == '\0'))
-               fp.subDir = NULL;
-           fp.baseName = baseName;
-           if (!scareMemory && fp.subDir != NULL)
-               fp.subDir = xstrdup(fp.subDir);
-       /* FIX: fp.entry.{dirName,dev,ino} undef @*/
+              (fp->subDir[0] == '/' && fp->subDir[1] == '\0'))
+               fp->subDir = NULL;
+           fp->baseName = baseName;
+           if (!scareMemory && fp->subDir != NULL)
+               fp->subDir = xstrdup(fp->subDir);
            goto exit;
        }
 
@@ -182,8 +180,8 @@ fingerPrint fpLookup(fingerPrintCache cache,
 exit:
     free(buf);
     free(cdnbuf);
-    /* FIX: fp.entry.{dirName,dev,ino} undef @*/
-    return fp;
+    /* XXX TODO: failure from eg realpath() should be returned and handled */
+    return 0;
 }
 
 unsigned int fpHashFunction(const fingerPrint * fp)
@@ -227,8 +225,8 @@ void fpLookupList(fingerPrintCache cache, const char ** dirNames,
            fpList[i].subDir = fpList[i - 1].subDir;
            fpList[i].baseName = baseNames[i];
        } else {
-           fpList[i] = fpLookup(cache, dirNames[dirIndexes[i]], baseNames[i],
-                                1);
+           fpLookup(cache,
+                    dirNames[dirIndexes[i]], baseNames[i], 1, &fpList[i]);
        }
     }
 }
@@ -290,7 +288,7 @@ void fpLookupSubdir(rpmFpHash symlinks, rpmFpHash fphash, fingerPrintCache fpc,
                        rstrscat(&link, endbasename+1, "/", NULL);
                   }
 
-                  *fp = fpLookup(fpc, link, fp->baseName, 0);
+                  fpLookup(fpc, link, fp->baseName, 0, fp);
 
                   free(link);
                   free(currentsubdir);
index f2ea6f5..9838e92 100644 (file)
@@ -110,11 +110,13 @@ fingerPrintCache fpCacheFree(fingerPrintCache cache);
  * @param dirName      leading directory name of file path
  * @param baseName     base name of file path
  * @param scareMemory
- * @return pointer to the finger print associated with a file path.
+ * @retval fp          pointer of fingerprint struct to fill out
+ * @return             0 on success
  */
 RPM_GNUC_INTERNAL
-fingerPrint fpLookup(fingerPrintCache cache, const char * dirName, 
-                       const char * baseName, int scareMemory);
+int fpLookup(fingerPrintCache cache,
+            const char * dirName, const char * baseName, int scareMemory,
+            fingerPrint *fp);
 
 /**
  * Return hash value for a finger print.
index d71dd03..7d906da 100644 (file)
@@ -959,7 +959,7 @@ static int rpmdbFindByFile(rpmdb db, dbiIndex dbi, const char *filespec,
 
     *matches = xcalloc(1, sizeof(**matches));
     fpc = fpCacheCreate(allMatches->count);
-    fp1 = fpLookup(fpc, dirName, baseName, 1);
+    fpLookup(fpc, dirName, baseName, 1, &fp1);
 
     i = 0;
     while (i < allMatches->count) {
@@ -997,8 +997,8 @@ static int rpmdbFindByFile(rpmdb db, dbiIndex dbi, const char *filespec,
            }
 
            if (!skip) {
-               fp2 = fpLookup(fpc, dirNames[dirIndexes[num]],
-                                   baseNames[num], 1);
+               fpLookup(fpc,
+                        dirNames[dirIndexes[num]], baseNames[num], 1, &fp2);
                if (FP_EQUAL(fp1, fp2)) {
                    struct dbiIndexItem rec = { 
                        .hdrNum = dbiIndexRecordOffset(allMatches, i),
index 4965a73..bbe29a5 100644 (file)
@@ -1016,7 +1016,7 @@ void checkInstalledFiles(rpmts ts, uint64_t fileCount, rpmFpHash ht, fingerPrint
                    /* directory is the same as last round */
                    fp.baseName = baseName;
                } else {
-                   fp = fpLookup(fpc, dirName, baseName, 1);
+                   fpLookup(fpc, dirName, baseName, 1, &fp);
                    oldDir = dirName;
                }
                fpp = &fp;