dchroot-chroot-config.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-2007  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software; you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation; either version 2 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
00017  *
00018  *********************************************************************/
00019 
00020 #include <config.h>
00021 
00022 #include <sbuild/sbuild-chroot-plain.h>
00023 #include <sbuild/sbuild-log.h>
00024 #include <sbuild/sbuild-parse-error.h>
00025 
00026 #include "dchroot-chroot-config.h"
00027 
00028 #include <cerrno>
00029 #include <cstdlib>
00030 #include <cstring>
00031 #include <iostream>
00032 
00033 #include <boost/format.hpp>
00034 
00035 using std::endl;
00036 using sbuild::_;
00037 using sbuild::keyfile;
00038 using boost::format;
00039 using namespace dchroot;
00040 
00041 chroot_config::chroot_config ():
00042   sbuild::chroot_config()
00043 {
00044 }
00045 
00046 chroot_config::chroot_config (std::string const& file,
00047                               bool               active):
00048   sbuild::chroot_config(file, active)
00049 {
00050 }
00051 
00052 chroot_config::~chroot_config ()
00053 {
00054 }
00055 
00056 void
00057 chroot_config::parse_data (std::istream& stream,
00058                            bool          active)
00059 {
00060   active = false; // dchroot does not support sessions.
00061 
00062   size_t linecount = 0;
00063   std::string line;
00064   std::string chroot_name;
00065   std::string chroot_location;
00066   bool default_set = false;
00067 
00068   sbuild::keyfile kconfig;
00069 
00070   while (std::getline(stream, line))
00071     {
00072       linecount++;
00073 
00074       if (line[0] == '#')
00075         {
00076           // Comment line; do nothing.
00077         }
00078       else if (line.length() == 0)
00079         {
00080           // Empty line; do nothing.
00081         }
00082       else // Item
00083         {
00084           static const char *whitespace = " \t";
00085 
00086           // Get chroot name
00087           std::string::size_type cstart = line.find_first_not_of(whitespace);
00088           std::string::size_type cend = line.find_first_of(whitespace, cstart);
00089 
00090           // Get chroot location
00091           std::string::size_type lstart = line.find_first_not_of(whitespace,
00092                                                                  cend);
00093           std::string::size_type lend = line.find_first_of(whitespace, lstart);
00094 
00095           // Get chroot personality
00096           std::string::size_type pstart = line.find_first_not_of(whitespace,
00097                                                                  lend);
00098           std::string::size_type pend = line.find_first_of(whitespace, pstart);
00099 
00100           // Check for trailing non-whitespace.
00101           std::string::size_type tstart = line.find_first_not_of(whitespace,
00102                                                                  pend);
00103           if (cstart == std::string::npos ||
00104               tstart != std::string::npos)
00105             throw keyfile::error(linecount, keyfile::INVALID_LINE, line);
00106 
00107           std::string chroot_name = line.substr(cstart, cend - cstart);
00108 
00109           std::string location;
00110           if (lstart != std::string::npos)
00111             location = line.substr(lstart, lend - lstart);
00112 
00113           std::string personality;
00114           if (pstart != std::string::npos)
00115             personality = line.substr(pstart, pend - pstart);
00116 
00117           // Add details to keyfile.
00118           if (kconfig.has_group(chroot_name))
00119             throw keyfile::error(linecount, keyfile::DUPLICATE_GROUP,
00120                                  chroot_name);
00121           else
00122             kconfig.set_group(chroot_name, "", linecount);
00123 
00124           kconfig.set_value(chroot_name, "type", "plain", "", linecount);
00125 
00126           // TRANSLATORS: %1% = chroot name
00127           format fmt(_("%1% chroot (dchroot compatibility)"));
00128           fmt % chroot_name;
00129 
00130           kconfig.set_value(chroot_name, "description", fmt, "", linecount);
00131 
00132           if (lstart != std::string::npos)
00133             kconfig.set_value(chroot_name, "location", location, "", linecount);
00134 
00135           if (pstart != std::string::npos)
00136             kconfig.set_value(chroot_name, "personality", personality, "", linecount);
00137 
00138           if (chroot_name == "default")
00139             default_set = true;
00140 
00141           // Set default chroot.
00142           if (!default_set)
00143             {
00144               sbuild::string_list aliases;
00145               aliases.push_back("default");
00146               kconfig.set_list_value(chroot_name, "aliases",
00147                                      aliases.begin(), aliases.end(),
00148                                      "", linecount);
00149               default_set = true;
00150             }
00151 
00152         }
00153     }
00154 
00155   load_keyfile(kconfig, active);
00156 }

Generated on Sun Jul 8 21:23:54 2007 for schroot by  doxygen 1.5.2