The following commit has been merged in the master branch:
commit 4985c686c30b2d0682aab0885d32c36007a7998d
Author: David Benjamin <[email protected]>
Date:   Sat Aug 29 19:59:20 2009 -0400

    dpkg: Refactor file addition into package files to a new function
    
    Added private function pkg_files_add_file for inserting a file to a
    package's entries. The function takes a file_tail to avoid an O(n^2)
    loop when adding to the end of the list. (This is what the original code
    does, so I have mirrored its behavior.)
    
    Signed-off-by: David Benjamin <[email protected]>
    Signed-off-by: Guillem Jover <[email protected]>

diff --git a/src/filesdb.c b/src/filesdb.c
index e3bc669..2703d49 100644
--- a/src/filesdb.c
+++ b/src/filesdb.c
@@ -122,6 +122,54 @@ pkg_files_blank(struct pkginfo *pkg)
   pkg->clientdata->files = NULL;
 }
 
+struct fileinlist **
+pkg_files_add_file(struct pkginfo *pkg, const char *filename,
+                   enum fnnflags flags, struct fileinlist **file_tail)
+{
+  struct fileinlist *newent, *current;
+  struct filepackages *packageslump;
+  int putat = 0;
+
+  ensure_package_clientdata(pkg);
+
+  if (file_tail == NULL)
+    file_tail = &pkg->clientdata->files;
+
+  /* Make sure we're at the end. */
+  while ((*file_tail) != NULL) {
+    file_tail = &((*file_tail)->next);
+  }
+
+  /* Create a new node. */
+  newent = nfmalloc(sizeof(struct fileinlist));
+  newent->namenode = findnamenode(filename, flags);
+  newent->next = NULL;
+  *file_tail = newent;
+  file_tail = &newent->next;
+
+  /* Add pkg to newent's package list. */
+  packageslump = newent->namenode->packages;
+  putat = 0;
+  if (packageslump) {
+    while (putat < PERFILEPACKAGESLUMP && packageslump->pkgs[putat])
+       putat++;
+    if (putat >= PERFILEPACKAGESLUMP)
+      packageslump = NULL;
+  }
+  if (!packageslump) {
+    packageslump = nfmalloc(sizeof(struct filepackages));
+    packageslump->more = newent->namenode->packages;
+    newent->namenode->packages = packageslump;
+    putat = 0;
+  }
+  packageslump->pkgs[putat]= pkg;
+  if (++putat < PERFILEPACKAGESLUMP)
+    packageslump->pkgs[putat] = NULL;
+
+  /* Return the position for the next guy. */
+  return file_tail;
+}
+
 /**
  * Load the list of files in this package into memory, or update the
  * list if it is there but stale.
@@ -131,9 +179,7 @@ ensure_packagefiles_available(struct pkginfo *pkg)
 {
   int fd;
   const char *filelistfile;
-  struct fileinlist **lendp, *newent, *current;
-  struct filepackages *packageslump;
-  int putat;
+  struct fileinlist **lendp;
   struct stat stat_buf;
   char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
 
@@ -195,11 +241,7 @@ ensure_packagefiles_available(struct pkginfo *pkg)
       if (ptr == thisline)
         ohshit(_("files list file for package `%.250s' contains empty 
filename"),pkg->name);
       *ptr = '\0';
-      newent= nfmalloc(sizeof(struct fileinlist));
-      newent->namenode= findnamenode(thisline, fnn_nocopy);
-      newent->next = NULL;
-      *lendp= newent;
-      lendp= &newent->next;
+      lendp = pkg_files_add_file(pkg, thisline, fnn_nocopy, lendp);
       thisline = nextline;
     }
   }
@@ -209,25 +251,6 @@ ensure_packagefiles_available(struct pkginfo *pkg)
 
   onerr_abort--;
 
-  for (newent= pkg->clientdata->files; newent; newent= newent->next) {
-    packageslump= newent->namenode->packages;
-    putat= 0;
-    if (packageslump) {
-      for (; putat < PERFILEPACKAGESLUMP && packageslump->pkgs[putat];
-           putat++);
-      if (putat >= PERFILEPACKAGESLUMP)
-        packageslump = NULL;
-    }
-    if (!packageslump) {
-      packageslump= nfmalloc(sizeof(struct filepackages));
-      packageslump->more= newent->namenode->packages;
-      newent->namenode->packages= packageslump;
-      putat= 0;
-    }
-    packageslump->pkgs[putat]= pkg;
-    if (++putat < PERFILEPACKAGESLUMP)
-      packageslump->pkgs[putat] = NULL;
-  }      
   pkg->clientdata->fileslistvalid= 1;
 }
 

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to