Ensure directories have trailing slashes in doLookupId()
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 18 Sep 2012 06:39:38 +0000 (09:39 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 18 Sep 2012 07:00:15 +0000 (10:00 +0300)
- In the old days, stripping trailing slashes made sense as its useless
  as it is and stripping it saved a bit of memory, but now with the
  directories in the pool things are different: while not all paths
  we'll see here necessarily exist in the pool already, NONE of them
  exists without the trailing slash, so stripping it out wastes gobs
  of memory.  Simplifies the code a bit and saves several megs of memory
  on larger transactions, what's not to like?

lib/fprint.c

index 6ae3048..687561c 100644 (file)
@@ -140,14 +140,14 @@ static int doLookupId(fingerPrintCache cache,
     const char * dirName = rpmstrPoolStr(cache->pool, dirNameId);
     size_t cdnl = rpmstrPoolStrlen(cache->pool, dirNameId);;
     const char * cdn = NULL; /* cleaned directory path */
+    const char *rootDir = "/";
 
     if (*dirName == '/') {
-       char trailingslash = (dirName[cdnl-1] == '/');
        cdnbuf = xstrdup(dirName);
        cdnbuf = rpmCleanPath(cdnbuf);
-       if (trailingslash) {
+       /* leave my trailing slashes along you b**** */
+       if (cdnl > 1)
            cdnbuf = rstrcat(&cdnbuf, "/");
-       }
        cdn = cdnbuf;
        cdnl = strlen(cdn);
     } else {
@@ -183,15 +183,9 @@ static int doLookupId(fingerPrintCache cache,
     buf = xstrdup(cdn);
     end = buf + cdnl;
 
-    /* no need to pay attention to that extra little / at the end of dirName */
-    if (buf[1] && end[-1] == '/') {
-       end--;
-       *end = '\0';
-    }
-
     while (1) {
        /* buf contents change through end ptr, requiring rehash on each loop */
-       const char *fpDir = (*buf != '\0') ? buf : "/";
+       const char *fpDir = (*buf != '\0') ? buf : rootDir;
        rpmsid fpId = rpmstrPoolId(cache->pool, fpDir, 1);
 
        /* as we're stating paths here, we want to follow symlinks */
@@ -224,14 +218,11 @@ static int doLookupId(fingerPrintCache cache,
        }
 
         /* stat of '/' just failed! */
-       if (end == buf + 1)
+       if (fpDir == rootDir)
            abort();
 
        end--;
-       while ((end > buf) && *end != '/') end--;
-       if (end == buf)     /* back to stat'ing just '/' */
-           end++;
-
+       while ((end > buf) && *(end-1) != '/') end--;
        *end = '\0';
     }