[Buildd-tools-devel] [PATCH 11/22] Allow additional verification of chroot containers

Jan-Marek Glogowski glogow at fbihome.de
Thu Mar 26 21:13:49 UTC 2009


Stat the containers and omits missing ones in the output
or lists. Special handling for files and LVM sessions.
---
 sbuild/sbuild-chroot-config.cc |   90 +++++++++++++++++++++++++++++++++++----
 sbuild/sbuild-chroot-config.h  |   32 ++++++++++++--
 2 files changed, 107 insertions(+), 15 deletions(-)

diff --git a/sbuild/sbuild-chroot-config.cc b/sbuild/sbuild-chroot-config.cc
index fdf011d..87bd65e 100644
--- a/sbuild/sbuild-chroot-config.cc
+++ b/sbuild/sbuild-chroot-config.cc
@@ -21,6 +21,7 @@
 #include "sbuild-chroot.h"
 #include "sbuild-chroot-source.h"
 #include "sbuild-chroot-config.h"
+#include "sbuild-chroot-lvm-snapshot.h"
 #include "sbuild-dirstream.h"
 #include "sbuild-lock.h"
 
@@ -236,15 +237,61 @@ chroot_config::add (chroot::ptr&   chroot,
     }
 }
 
+bool
+chroot_config::stat_chroot_containers (chroot::ptr const& chroot) const
+{
+  std::string location;
+
+  if (chroot->get_active())
+    {
+      std::string type;
+      chroot->get_chroot_strings(&type, NULL, NULL);
+
+      /**
+       * File based session can be restored without the container.
+       * Not sure what happens with the LVM snapshot device, if the
+       * originating device is gone...
+       */
+      if (type == "file")
+	location = chroot->get_mount_location();
+      else
+	{
+	  if (type == "lvm-snapshot")
+	    location = (dynamic_cast<chroot_lvm_snapshot *>
+				(chroot.get()))->get_snapshot_device();
+	  else
+	    location = chroot->get_container();
+	}
+    }
+  else
+    location = chroot->get_container();
+
+  try
+    {
+      stat(location).check();
+    }
+  catch (sbuild::stat::error const& e)
+    {
+      return false;
+    }
+
+  return true;
+}
+
 chroot_config::chroot_list
-chroot_config::get_chroots () const
+chroot_config::get_chroots (bool stat_container) const
 {
   chroot_list ret;
 
   for (chroot_map::const_iterator pos = this->chroots.begin();
        pos != this->chroots.end();
        ++pos)
-    ret.push_back(pos->second);
+    {
+      if (stat_container && !stat_chroot_containers(pos->second))
+	continue;
+
+      ret.push_back(pos->second);
+    }
 
   std::sort(ret.begin(), ret.end(), chroot_alphasort);
 
@@ -280,14 +327,26 @@ chroot_config::find_alias (std::string const& name) const
 }
 
 string_list
-chroot_config::get_chroot_list () const
+chroot_config::get_chroot_list (bool stat_container) const
 {
   string_list ret;
 
-  for (string_map::const_iterator pos = this->aliases.begin();
-       pos != this->aliases.end();
+  for (chroot_map::const_iterator pos = this->chroots.begin();
+       pos != this->chroots.end();
        ++pos)
-    ret.push_back(pos->first);
+    {
+      if (stat_container && !stat_chroot_containers(pos->second))
+	continue;
+
+      ret.push_back(pos->first);
+      string_list const& aliases = pos->second->get_aliases();
+      for (string_list::const_iterator alias = aliases.begin();
+	   alias != aliases.end();
+	   ++alias)
+	{
+	  ret.push_back(*alias);
+	}
+    }
 
   std::sort(ret.begin(), ret.end());
 
@@ -295,9 +354,10 @@ chroot_config::get_chroot_list () const
 }
 
 void
-chroot_config::print_chroot_list (std::ostream& stream) const
+chroot_config::print_chroot_list (std::ostream& stream, 
+                                  bool          stat_container) const
 {
-  string_list chroots = get_chroot_list();
+  string_list chroots = get_chroot_list(stat_container);
 
   for (string_list::const_iterator pos = chroots.begin();
        pos != chroots.end();
@@ -307,7 +367,8 @@ chroot_config::print_chroot_list (std::ostream& stream) const
 }
 
 void
-chroot_config::print_chroot_list_simple (std::ostream& stream) const
+chroot_config::print_chroot_list_simple (std::ostream& stream, 
+                                         bool          stat_container) const
 {
   stream << _("Available chroots: ");
 
@@ -315,6 +376,9 @@ chroot_config::print_chroot_list_simple (std::ostream& stream) const
        pos != this->chroots.end();
        ++pos)
     {
+      if (stat_container && !stat_chroot_containers(pos->second))
+	continue;
+
       stream << pos->second->get_name();
       string_list const& aliases = pos->second->get_aliases();
       if (!aliases.empty())
@@ -413,7 +477,8 @@ chroot_config::print_chroot_config (string_list const& chroots,
 }
 
 string_list
-chroot_config::validate_chroots (string_list const& chroots) const
+chroot_config::validate_chroots (string_list const& chroots,
+                                 bool               stat_container) const
 {
   string_list bad_chroots;
 
@@ -424,6 +489,11 @@ chroot_config::validate_chroots (string_list const& chroots) const
       const chroot::ptr chroot = find_alias(*pos);
       if (!chroot)
 	bad_chroots.push_back(*pos);
+      else
+	{ 
+	  if (stat_container && !stat_chroot_containers(chroot))
+	    bad_chroots.push_back(*pos);
+	}
     }
 
   return bad_chroots;
diff --git a/sbuild/sbuild-chroot-config.h b/sbuild/sbuild-chroot-config.h
index 47f8798..4636af6 100644
--- a/sbuild/sbuild-chroot-config.h
+++ b/sbuild/sbuild-chroot-config.h
@@ -141,11 +141,12 @@ namespace sbuild
     /**
      * Get a list of available chroots.
      *
+     * @param stat_container just return stat'able containers.
      * @returns a list of available chroots.  The list will be empty
      * if no chroots are available.
      */
     chroot_list
-    get_chroots () const;
+    get_chroots (bool stat_container = false) const;
 
     /**
      * Find a chroot by its name.
@@ -169,28 +170,33 @@ namespace sbuild
      * Get the names (including aliases) of all the available chroots,
      * sorted in alphabetical order.
      *
+     * @param stat_container just return stat'able containers.
      * @returns the list.  The list will be empty if no chroots are
      * available.
      */
     string_list
-    get_chroot_list () const;
+    get_chroot_list (bool stat_container = false) const;
 
     /**
      * Print all the available chroots to the specified stream.
      *
      * @param stream the stream to output to.
+     * @param stat_container just print stat'able containers.
      */
     void
-    print_chroot_list (std::ostream& stream) const;
+    print_chroot_list (std::ostream& stream, 
+                       bool          stat_container = false) const;
 
     /**
      * Print a single line of all the available chroots to the
      * specified stream.
      *
      * @param stream the stream to output to.
+     * @param stat_container just print stat'able containers.
      */
     void
-    print_chroot_list_simple (std::ostream& stream) const;
+    print_chroot_list_simple (std::ostream& stream, 
+                              bool          stat_container = false) const;
 
     /**
      * Print information about the specified chroots to the specified
@@ -228,12 +234,17 @@ namespace sbuild
     /**
      * Check that all the chroots specified exist.
      *
+     * This checks just the session and config files, unless stat_container
+     * is set, which additionally tries to stat the container.
+     *
      * @param chroots a list of chroots to validate.
+     * @param stat_container additionally do a stat to test the container.
      * @returns a list of invalid chroots.  The list will be empty if
      * all chroots are valid.
      */
     string_list
-    validate_chroots (string_list const& chroots) const;
+    validate_chroots (string_list const& chroots,
+		      bool               stat_container = false) const;
 
   private:
     /**
@@ -274,6 +285,17 @@ namespace sbuild
     load_keyfile (keyfile& kconfig,
 		  bool     active);
 
+    /**
+     * Basic chroot container verification.
+     *
+     * This just stats the container, or if a session the main required
+     * directory or device.
+     *
+     * @param chroot the chroot to verify.
+     */
+    bool
+    stat_chroot_containers (chroot::ptr const& chroot) const;
+
     /// A list of chroots (name->chroot mapping).
     chroot_map chroots;
     /// A list of aliases (alias->name mapping).
-- 
1.6.2.1




More information about the Buildd-tools-devel mailing list