From 553f0cefde56fb274abd2362800aabc0f37d83cb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= <ondrej@sury.org>
Date: Wed, 11 May 2011 09:45:51 +0200
Subject: [PATCH] Add conffiles in second loop.

---
 src/help.c   |   26 ++++++++++++++++++++++++++
 src/main.h   |    2 ++
 src/remove.c |   45 +++++++++++++++++++++++++++++++++++++--------
 3 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/src/help.c b/src/help.c
index 4a2256f..5b0fd39 100644
--- a/src/help.c
+++ b/src/help.c
@@ -512,6 +512,32 @@ dir_is_used_by_others(struct filenamenode *file, struct pkginfo *pkg)
   return false;
 }
 
+bool
+dir_is_used_by_pkg(struct filenamenode *file, struct pkginfo *pkg,
+                   struct fileinlist *list)
+{
+  struct fileinlist *node;
+  size_t namelen;
+
+  debug(dbg_veryverbose, "dir_is_used_by_pkg '%s' (by %s)",
+        file->name, pkg ? pkg->name : "<none>");
+
+  namelen = strlen(file->name);
+
+  for (node = list; node; node = node->next) {
+    debug(dbg_veryverbose, "dir_is_used_by_pkg considering %s ...",
+          node->namenode->name);
+
+    if (strncmp(file->name, node->namenode->name, namelen) == 0 &&
+        node->namenode->name[namelen] == '/')
+      return true;
+  }
+
+  debug(dbg_veryverbose, "dir_is_used_by_pkg no");
+
+  return false;
+}
+
 void oldconffsetflags(const struct conffile *searchconff) {
   struct filenamenode *namenode;
 
diff --git a/src/main.h b/src/main.h
index 12d12e7..3b8e094 100644
--- a/src/main.h
+++ b/src/main.h
@@ -249,6 +249,8 @@ void post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status);
 
 void clear_istobes(void);
 bool dir_is_used_by_others(struct filenamenode *namenode, struct pkginfo *pkg);
+bool dir_is_used_by_pkg(struct filenamenode *namenode, struct pkginfo *pkg,
+                        struct fileinlist *list);
 bool dir_has_conffiles(struct filenamenode *namenode, struct pkginfo *pkg);
 
 void log_action(const char *action, struct pkginfo *pkg);
diff --git a/src/remove.c b/src/remove.c
index a33518d..06ec05c 100644
--- a/src/remove.c
+++ b/src/remove.c
@@ -219,12 +219,11 @@ removal_bulk_remove_files(struct pkginfo *pkg)
     while ((namenode= reversefilelist_next(&rlistit))) {
       struct filenamenode *usenode;
 
-      debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
-            namenode->name, namenode->flags);
       if (namenode->flags & fnnf_old_conff) {
-        push_leftover(&leftover,namenode);
         continue;
       }
+      debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
+            namenode->name, namenode->flags);
 
       usenode = namenodetouse(namenode, pkg);
       trig_file_activate(usenode, pkg);
@@ -257,8 +256,13 @@ removal_bulk_remove_files(struct pkginfo *pkg)
 	  push_leftover(&leftover,namenode);
 	  continue;
 	}
-        if (dir_is_used_by_others(namenode, pkg))
+        if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
+          push_leftover(&leftover, namenode);
           continue;
+        }
+        if (dir_is_used_by_others(namenode, pkg)) {
+          continue;
+        }
       }
       debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
       if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
@@ -284,6 +288,17 @@ removal_bulk_remove_files(struct pkginfo *pkg)
       if (secure_unlink(fnvb.buf))
         ohshite(_("unable to securely remove '%.250s'"), fnvb.buf);
     }
+
+    reversefilelist_init(&rlistit,pkg->clientdata->files);
+    while ((namenode= reversefilelist_next(&rlistit))) {
+      debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
+            namenode->name, namenode->flags);
+      if (namenode->flags & fnnf_old_conff) {
+        push_leftover(&leftover,namenode);
+        continue;
+      }
+    }
+
     write_filelist_except(pkg,leftover,0);
     maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
                                 "remove", NULL);
@@ -313,12 +328,11 @@ static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
   while ((namenode= reversefilelist_next(&rlistit))) {
     struct filenamenode *usenode;
 
-    debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
-          namenode->name, namenode->flags);
     if (namenode->flags & fnnf_old_conff) {
-      push_leftover(&leftover,namenode);
       continue;
     }
+    debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
+          namenode->name, namenode->flags);
 
     usenode = namenodetouse(namenode, pkg);
     trig_file_activate(usenode, pkg);
@@ -337,8 +351,13 @@ static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
 	push_leftover(&leftover,namenode);
 	continue;
       }
-      if (dir_is_used_by_others(namenode, pkg))
+      if (dir_is_used_by_pkg(namenode, pkg, leftover)) {
+        push_leftover(&leftover, namenode);
+        continue;
+      }
+      if (dir_is_used_by_others(namenode, pkg)) {
         continue;
+      }
     }
 
     debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
@@ -365,6 +384,16 @@ static void removal_bulk_remove_leftover_dirs(struct pkginfo *pkg) {
     push_leftover(&leftover,namenode);
     continue;
   }
+
+  reversefilelist_init(&rlistit,pkg->clientdata->files);
+  while ((namenode= reversefilelist_next(&rlistit))) {
+    debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
+          namenode->name, namenode->flags);
+    if (namenode->flags & fnnf_old_conff) {
+      push_leftover(&leftover,namenode);
+      continue;
+    }
+  }
   write_filelist_except(pkg,leftover,0);
 
   modstatdb_note(pkg);
-- 
1.7.2.5

