dchroot-chroot-config.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  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::keyfile;
00037 using boost::format;
00038 using namespace dchroot;
00039 
00040 chroot_config::chroot_config ():
00041   sbuild::chroot_config()
00042 {
00043 }
00044 
00045 chroot_config::chroot_config (std::string const& file,
00046                               bool               active):
00047   sbuild::chroot_config(file, active)
00048 {
00049 }
00050 
00051 chroot_config::~chroot_config ()
00052 {
00053 }
00054 
00055 void
00056 chroot_config::parse_data (std::istream& stream,
00057                            bool          active)
00058 {
00059   active = false; // dchroot does not support sessions.
00060 
00061   size_t linecount = 0;
00062   std::string line;
00063   std::string chroot_name;
00064   std::string chroot_location;
00065   bool default_set = false;
00066 
00067   sbuild::keyfile kconfig;
00068 
00069   while (std::getline(stream, line))
00070     {
00071       linecount++;
00072 
00073       if (line[0] == '#')
00074         {
00075           // Comment line; do nothing.
00076         }
00077       else if (line.length() == 0)
00078         {
00079           // Empty line; do nothing.
00080         }
00081       else // Item
00082         {
00083           static const char *whitespace = " \t";
00084 
00085           // Get chroot name
00086           std::string::size_type cstart = line.find_first_not_of(whitespace);
00087           std::string::size_type cend = line.find_first_of(whitespace, cstart);
00088 
00089           // Get chroot location
00090           std::string::size_type lstart = line.find_first_not_of(whitespace,
00091                                                                  cend);
00092           std::string::size_type lend = line.find_first_of(whitespace, lstart);
00093 
00094           // Get chroot personality
00095           std::string::size_type pstart = line.find_first_not_of(whitespace,
00096                                                                  lend);
00097           std::string::size_type pend = line.find_first_of(whitespace, pstart);
00098 
00099           // Check for trailing non-whitespace.
00100           std::string::size_type tstart = line.find_first_not_of(whitespace,
00101                                                                  pend);
00102           if (cstart == std::string::npos ||
00103               tstart != std::string::npos)
00104             throw keyfile::error(linecount, keyfile::INVALID_LINE, line);
00105 
00106           std::string chroot_name = line.substr(cstart, cend - cstart);
00107 
00108           std::string location;
00109           if (lstart != std::string::npos)
00110             location = line.substr(lstart, lend - lstart);
00111 
00112           std::string personality;
00113           if (pstart != std::string::npos)
00114             personality = line.substr(pstart, pend - pstart);
00115 
00116           // Add details to keyfile.
00117           if (kconfig.has_group(chroot_name))
00118             throw keyfile::error(linecount, keyfile::DUPLICATE_GROUP,
00119                                  chroot_name);
00120           else
00121             kconfig.set_group(chroot_name, "", linecount);
00122 
00123           kconfig.set_value(chroot_name, "type", "plain", "", linecount);
00124 
00125           // TRANSLATORS: %1% = chroot name
00126           format fmt(_("%1% chroot (dchroot compatibility)"));
00127           fmt % chroot_name;
00128 
00129           kconfig.set_value(chroot_name, "description", fmt, "", linecount);
00130 
00131           if (lstart != std::string::npos)
00132             kconfig.set_value(chroot_name, "location", location, "", linecount);
00133 
00134           if (pstart != std::string::npos)
00135             kconfig.set_value(chroot_name, "personality", personality, "", linecount);
00136 
00137           if (chroot_name == "default")
00138             default_set = true;
00139 
00140           // Set default chroot.
00141           if (!default_set)
00142             {
00143               sbuild::string_list aliases;
00144               aliases.push_back("default");
00145               kconfig.set_list_value(chroot_name, "aliases",
00146                                      aliases.begin(), aliases.end(),
00147                                      "", linecount);
00148               default_set = true;
00149             }
00150 
00151         }
00152     }
00153 
00154   load_keyfile(kconfig, active);
00155 }

Generated on Sat Jan 27 16:11:03 2007 for schroot by  doxygen 1.5.1