Bläddra i källkod

dpkg: Refactor specific infodb traversal logic into their own functions

This will allow further refactoring now that the infodb traversal code
is the same eveyrwhere.
Guillem Jover 15 år sedan
förälder
incheckning
fc8b6ecf05
3 ändrade filer med 76 tillägg och 42 borttagningar
  1. 40 15
      src/processarc.c
  2. 17 18
      src/querycmd.c
  3. 19 9
      src/remove.c

+ 40 - 15
src/processarc.c

@@ -172,6 +172,8 @@ struct match_node {
   char *filename;
   char *filename;
 };
 };
 
 
+static struct match_node *match_head = NULL;
+
 static struct match_node *
 static struct match_node *
 match_node_new(const char *name, const char *type, struct match_node *next)
 match_node_new(const char *name, const char *type, struct match_node *next)
 {
 {
@@ -193,6 +195,33 @@ match_node_free(struct match_node *node)
   free(node);
   free(node);
 }
 }
 
 
+static void
+pkg_infodb_update_file(const char *filename, const char *filetype)
+{
+  if (strlen(filetype) > MAXCONTROLFILENAME)
+    ohshit(_("old version of package has overly-long info file name starting `%.250s'"),
+           filename);
+
+  /* We do the list separately. */
+  if (strcmp(filetype, LISTFILE) == 0)
+    return;
+
+  /* We keep files to rename in a list as doing the rename immediately
+   * might influence the current readdir(), the just renamed file might
+   * be returned a second time as it's actually a new file from the
+   * point of view of the filesystem. */
+  match_head = match_node_new(filename, filetype, match_head);
+}
+
+static void
+pkg_infodb_remove_file(const char *filename, const char *filetype)
+{
+  if (unlink(filename))
+    ohshite(_("unable to delete control info file `%.250s'"), filename);
+
+  debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
+}
+
 void process_archive(const char *filename) {
 void process_archive(const char *filename) {
   static const struct tar_operations tf = {
   static const struct tar_operations tf = {
     .read = tarfileread,
     .read = tarfileread,
@@ -235,7 +264,7 @@ void process_archive(const char *filename) {
   struct dirent *de;
   struct dirent *de;
   struct stat stab, oldfs;
   struct stat stab, oldfs;
   struct pkg_deconf_list *deconpil, *deconpiltemp;
   struct pkg_deconf_list *deconpil, *deconpiltemp;
-  struct match_node *match_head = NULL, *match_node = NULL;
+  struct match_node *match_node;
 
 
   cleanup_pkg_failed= cleanup_conflictor_failed= 0;
   cleanup_pkg_failed= cleanup_conflictor_failed= 0;
 
 
@@ -896,6 +925,12 @@ void process_archive(const char *filename) {
    * them as appropriate; then we go through the new scripts
    * them as appropriate; then we go through the new scripts
    * (any that are left) and install them. */
    * (any that are left) and install them. */
   debug(dbg_general, "process_archive updating info directory");
   debug(dbg_general, "process_archive updating info directory");
+
+  /* Deallocate the match list in case we aborted previously. */
+  while ((match_node = match_head)) {
+    match_head = match_node->next;
+    match_node_free(match_node);
+  }
   varbuf_reset(&infofnvb);
   varbuf_reset(&infofnvb);
   varbuf_add_str(&infofnvb, pkgadmindir());
   varbuf_add_str(&infofnvb, pkgadmindir());
   infodirlen= infofnvb.used;
   infodirlen= infofnvb.used;
@@ -920,21 +955,12 @@ void process_archive(const char *filename) {
 
 
     /* Skip past the full stop. */
     /* Skip past the full stop. */
     p++;
     p++;
-    /* We do the list separately. */
-    if (!strcmp(p, LISTFILE))
-      continue;
-    if (strlen(p) > MAXCONTROLFILENAME)
-      ohshit(_("old version of package has overly-long info file name starting `%.250s'"),
-             de->d_name);
+
     varbuf_trunc(&infofnvb, infodirlen);
     varbuf_trunc(&infofnvb, infodirlen);
     varbuf_add_str(&infofnvb, de->d_name);
     varbuf_add_str(&infofnvb, de->d_name);
     varbuf_end_str(&infofnvb);
     varbuf_end_str(&infofnvb);
 
 
-    /* We keep files to rename in a list as doing the rename immediately
-     * might influence the current readdir(), the just renamed file might
-     * be returned a second time as it's actually a new file from the
-     * point of view of the filesystem. */
-    match_head = match_node_new(infofnvb.buf, p, match_head);
+    pkg_infodb_update_file(infofnvb.buf, p);
   }
   }
   pop_cleanup(ehflag_normaltidy); /* closedir */
   pop_cleanup(ehflag_normaltidy); /* closedir */
 
 
@@ -1188,9 +1214,8 @@ void process_archive(const char *filename) {
       varbuf_trunc(&fnvb, infodirbaseused);
       varbuf_trunc(&fnvb, infodirbaseused);
       varbuf_add_str(&fnvb, de->d_name);
       varbuf_add_str(&fnvb, de->d_name);
       varbuf_end_str(&fnvb);
       varbuf_end_str(&fnvb);
-      if (unlink(fnvb.buf))
-        ohshite(_("unable to delete disappearing control info file `%.250s'"),fnvb.buf);
-      debug(dbg_scripts, "process_archive info unlinked %s",fnvb.buf);
+
+      pkg_infodb_remove_file(fnvb.buf, p + 1);
     }
     }
     pop_cleanup(ehflag_normaltidy); /* closedir */
     pop_cleanup(ehflag_normaltidy); /* closedir */
 
 

+ 17 - 18
src/querycmd.c

@@ -4,7 +4,7 @@
  *
  *
  * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
  * Copyright © 1995,1996 Ian Jackson <ian@chiark.greenend.org.uk>
  * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
  * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
- * Copyright © 2006-2009 Guillem Jover <guillem@debian.org>
+ * Copyright © 2006-2011 Guillem Jover <guillem@debian.org>
  *
  *
  * This is free software; you can redistribute it and/or modify
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * it under the terms of the GNU General Public License as published by
@@ -514,26 +514,33 @@ showpackages(const char *const *argv)
   return failures;
   return failures;
 }
 }
 
 
+static void
+pkg_infodb_print_filename(const char *filename, const char *filetype)
+{
+  /* Do not expose internal database files. */
+  if (strcmp(filetype, LISTFILE) == 0 ||
+      strcmp(filetype, CONFFILESFILE) == 0)
+    return;
+
+  if (strlen(filetype) > MAXCONTROLFILENAME)
+    return;
+
+  printf("%s\n", filename);
+}
+
 static void
 static void
 control_path_file(struct pkginfo *pkg, const char *control_file)
 control_path_file(struct pkginfo *pkg, const char *control_file)
 {
 {
   const char *control_path;
   const char *control_path;
   struct stat st;
   struct stat st;
 
 
-  /* Do not expose internal database files. */
-  if (strcmp(control_file, LISTFILE) == 0 ||
-      strcmp(control_file, CONFFILESFILE) == 0)
-    return;
-
   control_path = pkgadminfile(pkg, control_file);
   control_path = pkgadminfile(pkg, control_file);
-
   if (stat(control_path, &st) < 0)
   if (stat(control_path, &st) < 0)
     return;
     return;
-
   if (!S_ISREG(st.st_mode))
   if (!S_ISREG(st.st_mode))
     return;
     return;
 
 
-  printf("%s\n", control_path);
+  pkg_infodb_print_filename(control_path, control_file);
 }
 }
 
 
 static void
 static void
@@ -574,19 +581,11 @@ control_path_pkg(struct pkginfo *pkg)
     /* Skip past the full stop. */
     /* Skip past the full stop. */
     p++;
     p++;
 
 
-    /* Do not expose internal database files. */
-    if (strcmp(p, LISTFILE) == 0 ||
-        strcmp(p, CONFFILESFILE) == 0)
-      continue;
-
-    if (strlen(p) > MAXCONTROLFILENAME)
-      continue;
-
     varbuf_trunc(&db_path, db_path_len);
     varbuf_trunc(&db_path, db_path_len);
     varbuf_add_str(&db_path, db_de->d_name);
     varbuf_add_str(&db_path, db_de->d_name);
     varbuf_end_str(&db_path);
     varbuf_end_str(&db_path);
 
 
-    printf("%s\n", db_path.buf);
+    pkg_infodb_print_filename(db_path.buf, p);
   }
   }
   pop_cleanup(ehflag_normaltidy); /* closedir */
   pop_cleanup(ehflag_normaltidy); /* closedir */
 
 

+ 19 - 9
src/remove.c

@@ -184,6 +184,22 @@ static void push_leftover(struct fileinlist **leftoverp,
   *leftoverp= newentry;
   *leftoverp= newentry;
 }
 }
 
 
+static void
+removal_bulk_remove_file(const char *filename, const char *filetype)
+{
+  /* We need the postrm and list files for --purge. */
+  if (strcmp(filetype, LISTFILE) == 0 ||
+      strcmp(filetype, POSTRMFILE) == 0)
+    return;
+
+  debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
+
+  if (unlink(filename))
+    ohshite(_("unable to delete control info file `%.250s'"), filename);
+
+  debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
+}
+
 static void
 static void
 removal_bulk_remove_files(struct pkginfo *pkg)
 removal_bulk_remove_files(struct pkginfo *pkg)
 {
 {
@@ -291,18 +307,12 @@ removal_bulk_remove_files(struct pkginfo *pkg)
       if (strlen(pkg->name) != (size_t)(p-de->d_name) ||
       if (strlen(pkg->name) != (size_t)(p-de->d_name) ||
           strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
           strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
       debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
       debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
-      /* We need the postrm and list files for --purge. */
-      if (!strcmp(p+1,LISTFILE)) continue;
-      if (!strcmp(p + 1, POSTRMFILE)) {
-        continue;
-      }
-      debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
+
       varbuf_trunc(&fnvb, infodirbaseused);
       varbuf_trunc(&fnvb, infodirbaseused);
       varbuf_add_str(&fnvb, de->d_name);
       varbuf_add_str(&fnvb, de->d_name);
       varbuf_end_str(&fnvb);
       varbuf_end_str(&fnvb);
-      if (unlink(fnvb.buf))
-        ohshite(_("unable to delete control info file `%.250s'"),fnvb.buf);
-      debug(dbg_scripts, "removal_bulk info unlinked %s",fnvb.buf);
+
+      removal_bulk_remove_file(fnvb.buf, p + 1);
     }
     }
     pop_cleanup(ehflag_normaltidy); /* closedir */
     pop_cleanup(ehflag_normaltidy); /* closedir */