diff options
author | Roger Leigh <rleigh@debian.org> | 2013-04-03 23:06:55 +0100 |
---|---|---|
committer | Roger Leigh <rleigh@debian.org> | 2013-04-03 23:06:55 +0100 |
commit | fef21ee5fc82751b898d91bcddcf318ba9241452 (patch) | |
tree | 2df6ccc780c5cbf64d2f54c39bfe57917f735585 /bin | |
parent | 0b82e035557c6e33af84f16a1044ba87381e6f56 (diff) | |
download | schroot-fef21ee5fc82751b898d91bcddcf318ba9241452.tar.gz |
lib: Move libschroot to schroot-common
Diffstat (limited to 'bin')
-rw-r--r-- | bin/schroot/main-base.cc | 398 | ||||
-rw-r--r-- | bin/schroot/main-base.h | 159 | ||||
-rw-r--r-- | bin/schroot/main.cc | 77 | ||||
-rw-r--r-- | bin/schroot/main.h | 69 | ||||
-rw-r--r-- | bin/schroot/options-base.cc | 271 | ||||
-rw-r--r-- | bin/schroot/options-base.h | 154 |
6 files changed, 0 insertions, 1128 deletions
diff --git a/bin/schroot/main-base.cc b/bin/schroot/main-base.cc deleted file mode 100644 index 2b15372b..00000000 --- a/bin/schroot/main-base.cc +++ /dev/null @@ -1,398 +0,0 @@ -/* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org> - * - * schroot is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * schroot is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - * - *********************************************************************/ - -#include <config.h> - -#include "main-base.h" - -#include <sbuild/config.h> -#ifdef SBUILD_FEATURE_PAM -#include <sbuild/auth-pam.h> -#include <sbuild/auth-pam-conv.h> -#include <sbuild/auth-pam-conv-tty.h> -#endif // SBUILD_FEATURE_PAM -#include <sbuild/keyfile-writer.h> - -#include <cstdlib> -#include <ctime> -#include <iostream> -#include <locale> - -#include <termios.h> -#include <unistd.h> - -#include <boost/format.hpp> - -using std::endl; -using boost::format; -using sbuild::_; -using sbuild::N_; -using namespace schroot; - -template<> -sbuild::error<main_base::error_code>::map_type -sbuild::error<main_base::error_code>::error_strings = - { - // TRANSLATORS: %4% = file - {main_base::CHROOT_FILE, N_("No chroots are defined in ‘%4%’")}, - // TRANSLATORS: %4% = file - // TRANSLATORS: %5% = file - {main_base::CHROOT_FILE2, N_("No chroots are defined in ‘%4%’ or ‘%5%’")}, - // TRANSLATORS: %1% = file - {main_base::CHROOT_NOTDEFINED, N_("The specified chroots are not defined in ‘%1%’")}, - {main_base::SESSION_INVALID, N_("%1%: Invalid session name")} - }; - -main_base::main_base (std::string const& program_name, - std::string const& program_usage, - options_base::ptr& options, - bool use_syslog): - bin_common::main(program_name, program_usage, - std::static_pointer_cast<bin_common::options>(options), - use_syslog), - options(options) -{ -} - -main_base::~main_base () -{ -} - -void -main_base::action_version (std::ostream& stream) -{ - bin_common::main::action_version(stream); - - format feature(" %1$-12s %2%\n"); - - stream << '\n' - << _("Available chroot types:") << '\n'; -#ifdef SBUILD_FEATURE_BLOCKDEV - stream << feature % "BLOCKDEV" % _("Support for ‘block-device’ chroots"); -#endif -#ifdef SBUILD_FEATURE_BTRFSSNAP - stream << feature % "BTRFSSNAP" % _("Support for ‘btrfs-snapshot’ chroots"); -#endif - stream << feature % "CUSTOM" % _("Support for ‘custom’ chroots"); - stream << feature % "DIRECTORY" % _("Support for ‘directory’ chroots"); - stream << feature % "FILE" % _("Support for ‘file’ chroots"); -#ifdef SBUILD_FEATURE_LOOPBACK - stream << feature % "LOOPBACK" % _("Support for ‘loopback’ chroots"); -#endif -#ifdef SBUILD_FEATURE_LVMSNAP - stream << feature % "LVMSNAP" % _("Support for ‘lvm-snapshot’ chroots"); -#endif - stream << feature % "PLAIN" % _("Support for ‘plain’ chroots"); - stream << std::flush; -} - -void -main_base::action_info () -{ - for(auto chroot_name = this->chroot_names.begin(); - chroot_name != this->chroot_names.end(); - ++chroot_name) - { - // This should never fail, so no error handling here--we already - // validated everything when we got the chroot map. - sbuild::chroot_config::chroot_map::const_iterator c = this->chroots.find(*chroot_name); - assert(c->second); - - std::cout << c->second; - if (chroot_name + 1 != this->chroot_names.end()) - std::cout << '\n'; - } - - std::cout << std::flush; -} - -void -main_base::action_location () -{ - for(const auto& chroot_name : this->chroot_names) - { - // This should never fail, so no error handling here--we already - // validated everything when we got the chroot map. - const auto chroot = this->chroots.find(chroot_name); - assert(chroot->second); - - std::cout << chroot->second->get_path() << '\n'; - } - - std::cout << std::flush; -} - -void -main_base::action_config () -{ - std::cout << "# " - // TRANSLATORS: %1% = program name - // TRANSLATORS: %2% = program version - // TRANSLATORS: %3% = current date - << format(_("schroot configuration generated by %1% %2% on %3%")) - % this->program_name % VERSION % sbuild::date(time(0)) - << endl; - std::cout << endl; - - sbuild::keyfile info; - - for(const auto& chroot_name : this->chroot_names) - { - // This should never fail, so no error handling here--we already - // validated everything when we got the chroot map. - const auto chroot = this->chroots.find(chroot_name); - assert(chroot->second); - - // Generated chroots (e.g. source chroots) are not printed. - if (chroot->second->get_original()) - info << chroot->second; - } - - std::cout << sbuild::keyfile_writer(info) << std::flush; -} - -void -main_base::get_chroot_options () -{ - - if (this->options->all_chroots == true || - this->options->all_sessions == true || - this->options->all_source_chroots == true) - { - if (this->options->all_chroots) - { - sbuild::string_list chroots; - if (this->options->action == options_base::ACTION_LIST && - !this->options->exclude_aliases) - chroots = this->config->get_alias_list("chroot"); - else - chroots = this->config->get_chroot_list("chroot"); - this->chroot_names.insert(this->chroot_names.end(), chroots.begin(), chroots.end()); - } - if (this->options->all_sessions) - { - sbuild::string_list sessions; - if (this->options->action == options_base::ACTION_LIST && - !this->options->exclude_aliases) - sessions = this->config->get_alias_list("session"); - else - sessions = this->config->get_chroot_list("session"); - this->chroot_names.insert(this->chroot_names.end(), sessions.begin(), sessions.end()); - } - if (this->options->all_source_chroots) - { - sbuild::string_list sources; - if (this->options->action == options_base::ACTION_LIST && - !this->options->exclude_aliases) - sources = this->config->get_alias_list("source"); - else - sources = this->config->get_chroot_list("source"); - this->chroot_names.insert(this->chroot_names.end(), sources.begin(), sources.end()); - } - - // Validate and normalise - this->chroots = this->config->validate_chroots("", this->chroot_names); - } - else - { - // Search in the appropriate namespace. - std::string chroot_namespace("chroot"); - if (this->options->action == options_base::ACTION_SESSION_RECOVER || - this->options->action == options_base::ACTION_SESSION_RUN || - this->options->action == options_base::ACTION_SESSION_END) - chroot_namespace = "session"; - - // Validate and normalise - this->chroot_names = this->options->chroots; - this->chroots = this->config->validate_chroots - (chroot_namespace, this->chroot_names); - } -} - -void -main_base::load_config () -{ - this->config = sbuild::chroot_config::ptr(new sbuild::chroot_config); - /* The normal chroot list is used when starting a session or running - any chroot type or session, or displaying chroot information. */ - if (this->options->load_chroots == true) - { - this->config->add("chroot", SCHROOT_CONF); - this->config->add("chroot", SCHROOT_CONF_CHROOT_D); - } - /* The session chroot list is used when running or ending an - existing session, or displaying chroot information. */ - if (this->options->load_sessions == true) - this->config->add("session", SCHROOT_SESSION_DIR); -} - -int -main_base::run_impl () -{ - if (this->options->action == options_base::ACTION_HELP) - { - action_help(std::cout); - return EXIT_SUCCESS; - } - - if (this->options->action == options_base::ACTION_VERSION) - { - action_version(std::cout); - return EXIT_SUCCESS; - } - - /* Initialise chroot configuration. */ - load_config(); - - if (this->options->load_chroots && - this->config->get_chroots("chroot").empty() && - this->options->quiet == false) - log_exception_warning(error(CHROOT_FILE2, SCHROOT_CONF, SCHROOT_CONF_CHROOT_D)); - - /* Get list of chroots to use */ - get_chroot_options(); - if (this->chroots.empty()) - { - if (!(this->options->all_chroots == true || - this->options->all_sessions == true || - this->options->all_source_chroots == true)) - throw error(SCHROOT_CONF, CHROOT_NOTDEFINED); - else - { - // If one of the --all options was used, then don't treat - // the lack of chroots as an error. TODO: Also check if any - // additional chroots were specified with -c; this needs - // changes in get_chroot_options. - if (this->options->verbose) - log_exception_warning(error((this->options->all_chroots == true) ? - SCHROOT_CONF : SCHROOT_SESSION_DIR, - CHROOT_NOTDEFINED)); - return EXIT_SUCCESS; - } - } - this->chroot_objects.clear(); - for(const auto& chroot_name : this->chroot_names) - { - // This should never fail, so no error handling here--we already - // validated everything when we got the chroot map. - const auto chroot = this->chroots.find(chroot_name); - assert(chroot->second); - sbuild::session::chroot_list::value_type e; - e.alias = chroot->first; - e.chroot = chroot->second; - chroot_objects.push_back(e); - } - - /* Print chroot list. */ - if (this->options->action == options_base::ACTION_LIST) - { - action_list(); - return EXIT_SUCCESS; - } - - if (this->config->find_alias("session", this->options->session_name)) - throw error(this->options->session_name, SESSION_INVALID); - - /* Print chroot information for specified chroots. */ - if (this->options->action == options_base::ACTION_INFO) - { - action_info(); - return EXIT_SUCCESS; - } - if (this->options->action == options_base::ACTION_LOCATION) - { - action_location(); - return EXIT_SUCCESS; - } - if (this->options->action == options_base::ACTION_CONFIG) - { - action_config(); - return EXIT_SUCCESS; - } - - /* Create a session. */ - sbuild::session::operation sess_op(sbuild::session::OPERATION_AUTOMATIC); - if (this->options->action == options_base::ACTION_SESSION_BEGIN) - sess_op = sbuild::session::OPERATION_BEGIN; - else if (this->options->action == options_base::ACTION_SESSION_RECOVER) - sess_op = sbuild::session::OPERATION_RECOVER; - else if (this->options->action == options_base::ACTION_SESSION_RUN) - sess_op = sbuild::session::OPERATION_RUN; - else if (this->options->action == options_base::ACTION_SESSION_END) - sess_op = sbuild::session::OPERATION_END; - - try - { - create_session(sess_op); - add_session_auth(); - - if (!this->options->command.empty()) - this->session->get_auth()->set_command(this->options->command); - if (!this->options->directory.empty()) - this->session->get_auth()->set_wd(this->options->directory); - if (!this->options->shell.empty()) - this->session->set_shell_override(this->options->shell); - this->session->set_preserve_environment(this->options->preserve); - this->session->set_session_id(this->options->session_name); - this->session->set_force(this->options->session_force); - if (this->options->quiet) - this->session->set_verbosity("quiet"); - else if (this->options->verbose) - this->session->set_verbosity("verbose"); - this->session->set_user_options(this->options->useroptions_map); - - /* Run session. */ - this->session->run(); - } - catch (std::runtime_error const& e) - { - if (!this->options->quiet) - sbuild::log_exception_error(e); - } - - if (this->session) - return this->session->get_child_status(); - else - return EXIT_FAILURE; -} - -void -main_base::add_session_auth () -{ - // Add PAM authentication handler. If PAM isn't available, just - // continue to use the default handler - -#ifdef SBUILD_FEATURE_PAM - sbuild::auth::ptr auth = sbuild::auth_pam::create("schroot"); - - sbuild::auth_pam_conv::auth_ptr pam_auth = - std::dynamic_pointer_cast<sbuild::auth_pam>(auth); - - sbuild::auth_pam_conv::ptr conv = sbuild::auth_pam_conv_tty::create(pam_auth); - - - /* Set up authentication timeouts. */ - time_t curtime = 0; - time(&curtime); - conv->set_warning_timeout(curtime + 15); - conv->set_fatal_timeout(curtime + 20); - pam_auth->set_conv(conv); - - this->session->set_auth(auth); -#endif // SBUILD_FEATURE_PAM -} diff --git a/bin/schroot/main-base.h b/bin/schroot/main-base.h deleted file mode 100644 index 4e9f32eb..00000000 --- a/bin/schroot/main-base.h +++ /dev/null @@ -1,159 +0,0 @@ -/* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org> - * - * schroot is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * schroot is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - * - *********************************************************************/ - -#ifndef SCHROOT_MAIN_BASE_H -#define SCHROOT_MAIN_BASE_H - -#include <bin-common/main.h> -#include <schroot/options-base.h> - -#include <sbuild/chroot-config.h> -#include <sbuild/custom-error.h> - -namespace schroot -{ - - /** - * Frontend base for schroot programs. This class is used to "run" - * schroot programs. This class contains functionality common to - * all schroot programs (schroot, dchroot, dchroot-dsa). - */ - class main_base : public bin_common::main - { - public: - /// Error codes. - enum error_code - { - CHROOT_FILE, ///< No chroots are defined in .... - CHROOT_FILE2, ///< No chroots are defined in ... or .... - CHROOT_NOTDEFINED, ///< The specified chroots are not defined. - SESSION_INVALID ///< Invalid session name. - }; - - /// Exception type. - typedef sbuild::custom_error<error_code> error; - - typedef sbuild::chroot_config::chroot_map chroot_map; - - /** - * The constructor. - * - * @param program_name the program name. - * @param program_usage the program usage message. - * @param options the command-line options to use. - * @param use_syslog whether to open a connection to the system - * logger. - */ - main_base (std::string const& program_name, - std::string const& program_usage, - options_base::ptr& options, - bool use_syslog); - - /// The destructor. - virtual ~main_base (); - - virtual void - action_version (std::ostream& stream); - - /** - * List chroots. - */ - virtual void - action_list () = 0; - - /** - * Print detailed information about chroots. - */ - virtual void - action_info (); - - /** - * Print location of chroots. - */ - virtual void - action_location (); - - /** - * Dump configuration file for chroots. - */ - virtual void - action_config (); - - protected: - /** - * Run the program. This is the program-specific run method which - * must be implemented in a derived class. - * - * @returns 0 on success, 1 on failure or the exit status of the - * chroot command. - */ - virtual int - run_impl (); - - /** - * Get a list of chroots based on the specified options (--all, --chroot). - * - * @returns a list of chroots. - */ - void - get_chroot_options (); - - /** - * Load configuration. - */ - virtual void - load_config (); - - /** - * Create a session. This sets the session member. - * - * @param sess_op the session operation to perform. - */ - virtual void - create_session (sbuild::session::operation sess_op) = 0; - - /** - * Add PAM authentication handler to the session. - */ - virtual void - add_session_auth (); - - protected: - /// The program options. - options_base::ptr options; - /// The chroot configuration. - sbuild::chroot_config::ptr config; - /// The chroots to use (original names or aliases). - sbuild::string_list chroot_names; - /// The chroots to use (alias to chroot mapping). - chroot_map chroots; - /// The chroots to use (for session). - sbuild::session::chroot_list chroot_objects; - /// The session. - sbuild::session::ptr session; - }; - -} - -#endif /* SCHROOT_MAIN_BASE_H */ - -/* - * Local Variables: - * mode:C++ - * End: - */ diff --git a/bin/schroot/main.cc b/bin/schroot/main.cc deleted file mode 100644 index c0502eeb..00000000 --- a/bin/schroot/main.cc +++ /dev/null @@ -1,77 +0,0 @@ -/* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org> - * - * schroot is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * schroot is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - * - *********************************************************************/ - -#include <config.h> - -#include <sbuild/config.h> - -#include "main.h" - -#include <cstdlib> -#include <ctime> -#include <iostream> -#include <locale> - -#include <termios.h> -#include <unistd.h> - -#include <boost/format.hpp> - -using std::endl; -using boost::format; -using sbuild::_; -using namespace schroot; - -main::main (options_base::ptr& options): - main_base("schroot", - _("[OPTION…] [COMMAND] — run command or shell in a chroot"), - options, - true) -{ -} - -main::~main () -{ -} - -void -main::action_list () -{ - // This list is pre-validated. - for (const auto& chroot : this->chroot_names) - std::cout << chroot << '\n'; - std::cout << std::flush; -} - -void -main::create_session(sbuild::session::operation sess_op) -{ - sbuild::log_debug(sbuild::DEBUG_INFO) << "Creating schroot session" << endl; - - this->session = sbuild::session::ptr - (new sbuild::session("schroot", sess_op, this->chroot_objects)); -} - -void -main::add_session_auth () -{ - main_base::add_session_auth(); - - if (!this->options->user.empty()) - this->session->get_auth()->set_user(this->options->user); -} diff --git a/bin/schroot/main.h b/bin/schroot/main.h deleted file mode 100644 index 65ca65f4..00000000 --- a/bin/schroot/main.h +++ /dev/null @@ -1,69 +0,0 @@ -/* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org> - * - * schroot is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * schroot is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - * - *********************************************************************/ - -#ifndef SCHROOT_MAIN_H -#define SCHROOT_MAIN_H - -#include <schroot/main-base.h> -#include <schroot/options-base.h> - -/** - * schroot program components. - */ -namespace schroot -{ - - /** - * Frontend for schroot. This class is used to "run" schroot. - */ - class main : public main_base - { - public: - /** - * The constructor. - * - * @param options the command-line options to use. - */ - main (options_base::ptr& options); - - /// The destructor. - virtual ~main (); - - /** - * List chroots. - */ - virtual void - action_list (); - - protected: - virtual void - create_session(sbuild::session::operation sess_op); - - virtual void - add_session_auth (); - }; - -} - -#endif /* SCHROOT_MAIN_H */ - -/* - * Local Variables: - * mode:C++ - * End: - */ diff --git a/bin/schroot/options-base.cc b/bin/schroot/options-base.cc deleted file mode 100644 index 44951f5a..00000000 --- a/bin/schroot/options-base.cc +++ /dev/null @@ -1,271 +0,0 @@ -/* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org> - * - * schroot is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * schroot is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - * - *********************************************************************/ - -#include <config.h> - -#include <sbuild/util.h> - -#include "options.h" - -#include <cstdlib> -#include <iostream> -#include <stdexcept> - -#include <boost/format.hpp> -#include <boost/program_options.hpp> - -using std::endl; -using boost::format; -using sbuild::_; -namespace opt = boost::program_options; -using namespace schroot; - -const options_base::action_type options_base::ACTION_SESSION_AUTO ("session_auto"); -const options_base::action_type options_base::ACTION_SESSION_BEGIN ("session_begin"); -const options_base::action_type options_base::ACTION_SESSION_RECOVER ("session_recover"); -const options_base::action_type options_base::ACTION_SESSION_RUN ("session_run"); -const options_base::action_type options_base::ACTION_SESSION_END ("session_end"); -const options_base::action_type options_base::ACTION_LIST ("list"); -const options_base::action_type options_base::ACTION_INFO ("info"); -const options_base::action_type options_base::ACTION_LOCATION ("location"); -const options_base::action_type options_base::ACTION_CONFIG ("config"); - -options_base::options_base (): - bin_common::options (), - chroots(), - command(), - directory(), - shell(), - user(), - preserve(false), - all(false), - all_chroots(false), - all_sessions(false), - all_source_chroots(false), - exclude_aliases(false), - session_name(), - session_force(false), - useroptions(), - useroptions_map(), - chroot(_("Chroot selection")), - chrootenv(_("Chroot environment")), - session_actions(_("Session actions")), - session_options(_("Session options")) -{ -} - -options_base::~options_base () -{ -} - -void -options_base::add_options () -{ - // Chain up to add basic options. - bin_common::options::add_options(); - - action.add(ACTION_SESSION_AUTO); - action.set_default(ACTION_SESSION_AUTO); - action.add(ACTION_SESSION_BEGIN); - action.add(ACTION_SESSION_RECOVER); - action.add(ACTION_SESSION_RUN); - action.add(ACTION_SESSION_END); - action.add(ACTION_LIST); - action.add(ACTION_INFO); - action.add(ACTION_LOCATION); - action.add(ACTION_CONFIG); - - actions.add_options() - ("list,l", - _("List available chroots")) - ("info,i", - _("Show information about selected chroots")) - ("config", - _("Dump configuration of selected chroots")); - - chroot.add_options() - ("chroot,c", opt::value<sbuild::string_list>(&this->chroots), - _("Use specified chroot")); - - hidden.add_options() - ("command", opt::value<sbuild::string_list>(&this->command), - _("Command to run")); - - positional.add("command", -1); -} - -void -options_base::add_option_groups () -{ - // Chain up to add basic option groups. - bin_common::options::add_option_groups(); - -#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD - if (!chroot.options().empty()) -#else - if (!chroot.primary_keys().empty()) -#endif - { - visible.add(chroot); - global.add(chroot); - } -#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD - if (!chrootenv.options().empty()) -#else - if (!chrootenv.primary_keys().empty()) -#endif - { - visible.add(chrootenv); - global.add(chrootenv); - } -#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD - if (!session_actions.options().empty()) -#else - if (!session_actions.primary_keys().empty()) -#endif - { - visible.add(session_actions); - global.add(session_actions); - } - -#ifndef BOOST_PROGRAM_OPTIONS_DESCRIPTION_OLD - if (!session_options.options().empty()) -#else - if (!session_options.primary_keys().empty()) -#endif - { - visible.add(session_options); - global.add(session_options); - } -} - -void -options_base::check_options () -{ - // Chain up to check basic options. - bin_common::options::check_options(); - - if (vm.count("list")) - this->action = ACTION_LIST; - if (vm.count("info")) - this->action = ACTION_INFO; - if (vm.count("config")) - this->action = ACTION_CONFIG; -} - -void -options_base::check_actions () -{ - // Chain up to check basic actions. - bin_common::options::check_actions(); - - if (this->quiet && this->verbose) - { - sbuild::log_warning() - << _("--quiet and --verbose may not be used at the same time") - << endl; - sbuild::log_info() << _("Using verbose output") << endl; - } - - if (!this->chroots.empty() && all_used()) - { - sbuild::log_warning() - << _("--chroot and --all may not be used at the same time") - << endl; - sbuild::log_info() << _("Using --chroots only") << endl; - this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false; - } - - /* Determine which chroots to load and use. */ - if (this->action == ACTION_SESSION_AUTO) - { - // Only allow normal chroots - this->load_chroots = true; - this->load_sessions = false; - this->all = this->all_sessions = false; - - // If no chroot was specified, fall back to the "default" chroot. - if (this->chroots.empty() && all_used() == false) - this->chroots.push_back("default"); - } - else if (this->action == ACTION_SESSION_BEGIN) - { - // Only allow one session chroot - this->load_chroots = true; - this->load_sessions = false; - if (this->chroots.size() != 1 || all_used()) - throw error - (_("Exactly one chroot must be specified when beginning a session")); - - this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false; - } - else if (this->action == ACTION_SESSION_RECOVER || - this->action == ACTION_SESSION_RUN || - this->action == ACTION_SESSION_END) - { - // Session operations work on all chroots. - this->load_chroots = this->load_sessions = true; - - if (!this->session_name.empty()) - throw error - (_("--session-name is not permitted for the specified action; did you mean to use --chroot?")); - } - else if (this->action == ACTION_HELP || - this->action == ACTION_VERSION) - { - // Chroots don't make sense here. - this->load_chroots = this->load_sessions = false; - this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false; - } - else if (this->action == ACTION_LIST || - this->action == ACTION_INFO || - this->action == ACTION_LOCATION || - this->action == ACTION_CONFIG) - { - // If not specified otherwise, load normal chroots, but allow - // --all options. - if (!this->chroots.empty()) // chroot specified - this->load_chroots = this->load_sessions = true; - else if (!all_used()) // no chroots specified - { - this->all_chroots = true; - if (this->action == ACTION_LIST || this->action == ACTION_INFO) - this->all_source_chroots = true; - this->load_chroots = true; - } - if (this->all_chroots || this->all_source_chroots) - this->load_chroots = true; - if (this->all_sessions) - this->load_chroots = this->load_sessions = true; - } - else - { - // Something went wrong - this->load_chroots = this->load_sessions = false; - this->all = this->all_chroots = this->all_source_chroots = this->all_sessions = false; - throw error(_("Unknown action specified")); - } - - if (!this->session_name.empty() && this->action != ACTION_SESSION_BEGIN) - throw error - (_("--session-name is not permitted for the specified action")); - - if (!this->session_name.empty() && - !sbuild::is_valid_sessionname(this->session_name)) - throw error(_("Invalid session name")); -} diff --git a/bin/schroot/options-base.h b/bin/schroot/options-base.h deleted file mode 100644 index a6a485d8..00000000 --- a/bin/schroot/options-base.h +++ /dev/null @@ -1,154 +0,0 @@ -/* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org> - * - * schroot is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * schroot is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see - * <http://www.gnu.org/licenses/>. - * - *********************************************************************/ - -#ifndef SCHROOT_OPTIONS_BASE_H -#define SCHROOT_OPTIONS_BASE_H - -#include <sbuild/session.h> -#include <sbuild/types.h> - -#include <bin-common/options.h> - -#include <memory> -#include <string> -#include <vector> - -#include <boost/program_options.hpp> - -namespace schroot -{ - - /** - * Basic schroot command-line options. This is specialised by the - * frontends to suit their particular command-line options and - * behaviour. This class contains functionality and options common - * to all schroot programs (schroot, dchroot, dchroot-dsa). - */ - class options_base : public bin_common::options - { - public: - /// Begin, run and end a session. - static const action_type ACTION_SESSION_AUTO; - /// Begin a session. - static const action_type ACTION_SESSION_BEGIN; - /// Recover an existing session. - static const action_type ACTION_SESSION_RECOVER; - /// Run an existing session. - static const action_type ACTION_SESSION_RUN; - /// End an existing session. - static const action_type ACTION_SESSION_END; - /// Display a list of chroots. - static const action_type ACTION_LIST; - /// Display chroot information. - static const action_type ACTION_INFO; - /// Display chroot location information. - static const action_type ACTION_LOCATION; - /// Display chroot configuration. - static const action_type ACTION_CONFIG; - - /// A shared_ptr to an options_base object. - typedef std::shared_ptr<options_base> ptr; - - /// The constructor. - options_base (); - - /// The destructor. - virtual ~options_base (); - - /// Chroots to use. - sbuild::string_list chroots; - /// Chroot to print path. - std::string chroot_path; - /// Command to run. - sbuild::string_list command; - /// Directory to use. - std::string directory; - /// Shell to use. - std::string shell; - /// User to run as. - std::string user; - /// Preserve environment. - bool preserve; - /// Use all chroots and sessions. - bool all; - /// Use all chroots. - bool all_chroots; - /// Use all sessions. - bool all_sessions; - /// Use all source_chroots. - bool all_source_chroots; - /// Exclude aliases in output. - bool exclude_aliases; - /// Load chroots. - bool load_chroots; - /// Load sessions. - bool load_sessions; - /// Session name. - std::string session_name; - /// Force session operations. - bool session_force; - /// Options as a key=value list. - sbuild::string_list useroptions; - /// Options in a string-string map. - sbuild::string_map useroptions_map; - - protected: - /** - * Check if any of the --all options have been used. - * - * @returns true if any of the options have been used, otherwise - * false. - */ - bool - all_used () const - { - return (this->all || this->all_chroots || this->all_source_chroots || this->all_sessions); - } - - virtual void - add_options (); - - virtual void - add_option_groups (); - - virtual void - check_options (); - - virtual void - check_actions (); - - /// Chroot options group. - boost::program_options::options_description chroot; - /// Chroot environment options group. - boost::program_options::options_description chrootenv; - /// Session actions group. - boost::program_options::options_description session_actions; - /// Session options group. - boost::program_options::options_description session_options; - }; - -} - -#endif /* SCHROOT_OPTIONS_BASE_H */ - -/* - * Local Variables: - * mode:C++ - * End: - */ - |