[Aptitude-svn-commit] r4315 - in branches/aptitude-0.3/aptitude: . src

Daniel Burrows dburrows at costa.debian.org
Tue Sep 27 21:08:08 UTC 2005


Author: dburrows
Date: Tue Sep 27 21:08:05 2005
New Revision: 4315

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/ui.cc
   branches/aptitude-0.3/aptitude/src/ui.h
Log:
Remove old temporary directories on startup.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Tue Sep 27 21:08:05 2005
@@ -2,6 +2,12 @@
 
 	* src/ui.cc:
 
+	  Check for the existence of the old temporary directory on
+	  startup and display a one-time prompt asking whether it should
+	  be removed (currently defaults to "no").
+
+	* src/ui.cc:
+
 	  When su-ing to root, use temp::* instead of making the needed
 	  temporary names manually.
 

Modified: branches/aptitude-0.3/aptitude/src/ui.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/ui.cc	(original)
+++ branches/aptitude-0.3/aptitude/src/ui.cc	Tue Sep 27 21:08:05 2005
@@ -32,10 +32,12 @@
 #include <apt-pkg/error.h>
 #include <apt-pkg/packagemanager.h>
 
+#include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 
 #include <fstream>
 #include <utility>
@@ -889,6 +891,98 @@
   popup_widget(w);
 }
 
+/** Remove the given file/directory and all its children.  Behaves
+ *  like rm -fr.
+ *
+ *  \todo this belongs in util.cc
+ */
+static bool recursive_remdir(const std::string &dirname)
+{
+  struct stat stbuf;
+
+  if(lstat(dirname.c_str(), &stbuf) != 0)
+    _error->Errno("recursive_rmdir", _("Unable to stat \"%s\""), dirname.c_str());
+
+  if(S_ISLNK(stbuf.st_mode) || !S_ISDIR(stbuf.st_mode))
+    {
+      if(unlink(dirname.c_str()) != 0)
+	{
+	  _error->Errno("recursive_rmdir", _("Unable to remove \"%s\""), dirname.c_str());
+	  return false;
+	}
+      else
+	return true;
+    }
+
+  DIR *dir = opendir(dirname.c_str());
+  if(dir == NULL)
+    {
+      _error->Errno("recursive_rmdir", _("Unable to list files in \"%s\""), dirname.c_str());
+      return false;
+    }
+
+  bool rval = true;
+
+  for(dirent *dent = readdir(dir); dent != NULL; dent = readdir(dir))
+    if(strcmp(dent->d_name, ".") != 0 &&
+       strcmp(dent->d_name, "..") != 0)
+      rval = (rval && recursive_remdir(dirname + "/" + dent->d_name));
+
+  if(closedir(dir) != 0)
+    {
+      _error->Errno("recursive_rmdir", _("Failure closing directory \"%s\""), dirname.c_str());
+      rval = false;
+    }
+
+  if(rmdir(dirname.c_str()) != 0)
+    {
+      _error->Errno("recursive_rmdir", _("Unable to remove directory \"%s\""), dirname.c_str());
+      rval = false;
+    }
+
+  return rval;
+}
+
+static void do_kill_old_tmp(const std::string old_tmpdir)
+{
+  if(!recursive_remdir(old_tmpdir))
+    show_message(ssprintf(_("Unable to remove the old temporary directory; you should remove %s by hand."), old_tmpdir.c_str()));
+}
+
+static void cancel_kill_old_tmp(const std::string old_tmpdir)
+{
+  show_message(ssprintf(_("Will not remove %s; you should examine the files in it and remove them by hand."), old_tmpdir.c_str()));
+  aptcfg->Set(PACKAGE "::Ignore-Old-Tmp", "true");
+  apt_dumpcfg(PACKAGE);
+}
+
+static void maybe_show_old_tmpdir_message()
+{
+  std::string tmpdir_path = get_homedir() + "/.aptitude/.tmp";
+
+  if(aptcfg->FindB(PACKAGE "::Ignore-Old-Tmp", false))
+    {
+      // Watch for the reappearance of this directory.
+      if(access(tmpdir_path.c_str(), F_OK) != 0)
+	{
+	  aptcfg->Set(PACKAGE "::Ignore-Old-Tmp", "false");
+	  apt_dumpcfg(PACKAGE);
+	}
+
+      return;
+    }
+
+  // Remove it silently if it's empty.
+  if(rmdir(tmpdir_path.c_str()) == 0)
+    return;
+
+  if(access(tmpdir_path.c_str(), F_OK) == 0)
+    prompt_yesno_popup(wrapbox(fragf(_("It appears that a previous version of aptitude left files behind in %s.  These files are probably useless and safe to delete.%n%nDo you want to remove this directory and all its contents?  If you select \"No\", you will not see this message again."), tmpdir_path.c_str())),
+		       false,
+		       arg(sigc::bind(sigc::ptr_fun(do_kill_old_tmp), tmpdir_path)),
+		       arg(sigc::bind(sigc::ptr_fun(cancel_kill_old_tmp), tmpdir_path)));
+}
+
 // There are some circular references because of the code to test
 // for consistency before doing a package run; the last routine called before
 // the program starts downloading will verify that the selections are
@@ -2373,6 +2467,8 @@
     b->show();
   else
     b->hide();
+
+  maybe_show_old_tmpdir_message();
 }
 
 void ui_main()
@@ -2671,6 +2767,18 @@
 				     true);
 }
 
+void prompt_yesno_popup(fragment *prompt,
+			bool deflt,
+			slot0arg yesslot,
+			slot0arg noslot)
+{
+  main_stacked->add_visible_widget(vs_dialog_yesno(prompt,
+						   yesslot,
+						   noslot,
+						   deflt),
+				   true);
+}
+
 void prompt_yesno(const std::string &prompt,
 		  bool deflt,
 		  slot0arg yesslot,

Modified: branches/aptitude-0.3/aptitude/src/ui.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/ui.h	(original)
+++ branches/aptitude-0.3/aptitude/src/ui.h	Tue Sep 27 21:08:05 2005
@@ -243,6 +243,14 @@
 		  slot0arg yesslot,
 		  slot0arg noslot);
 
+/** Display a popup dialog for a yes-no prompt.  Meant for prompts
+ *  with large quantities of text.
+ */
+void prompt_yesno_popup(fragment *f,
+			bool deflt,
+			slot0arg yesslot,
+			slot0arg noslot);
+
 /** Display the given message, either in a popup dialog box or as a
  *  "transient" message at the bottom of the screen.  The message
  *  should be expected to be relatively short (ie, short enough to not



More information about the Aptitude-svn-commit mailing list