[24167] | 1 | /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org> |
---|
| 2 | * |
---|
| 3 | * schroot is free software: you can redistribute it and/or modify it |
---|
| 4 | * under the terms of the GNU General Public License as published by |
---|
| 5 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 6 | * (at your option) any later version. |
---|
| 7 | * |
---|
| 8 | * schroot is distributed in the hope that it will be useful, but |
---|
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 11 | * General Public License for more details. |
---|
| 12 | * |
---|
| 13 | * You should have received a copy of the GNU General Public License |
---|
| 14 | * along with this program. If not, see |
---|
| 15 | * <http://www.gnu.org/licenses/>. |
---|
| 16 | * |
---|
| 17 | *********************************************************************/ |
---|
| 18 | |
---|
| 19 | #ifndef SBUILD_CHROOT_CONFIG_H |
---|
| 20 | #define SBUILD_CHROOT_CONFIG_H |
---|
| 21 | |
---|
| 22 | #include <sbuild/sbuild-chroot.h> |
---|
| 23 | #include <sbuild/sbuild-custom-error.h> |
---|
| 24 | |
---|
| 25 | #include <map> |
---|
| 26 | #include <ostream> |
---|
| 27 | #include <vector> |
---|
| 28 | #include <string> |
---|
| 29 | |
---|
| 30 | namespace sbuild |
---|
| 31 | { |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * Chroot configuration. |
---|
| 35 | * |
---|
| 36 | * This class holds the configuration details from the configuration |
---|
| 37 | * file. Conceptually, it's an opaque container of chroot objects. |
---|
| 38 | * |
---|
| 39 | * Methods are provided to query the available chroots and find |
---|
| 40 | * specific chroots. |
---|
| 41 | */ |
---|
| 42 | class chroot_config |
---|
| 43 | { |
---|
| 44 | public: |
---|
| 45 | /// A list of chroots. |
---|
| 46 | typedef std::vector<chroot::ptr> chroot_list; |
---|
| 47 | /// A map between key-value string pairs. |
---|
| 48 | typedef std::map<std::string, std::string> string_map; |
---|
| 49 | /// A map between a chroot name and a chroot object. |
---|
| 50 | typedef std::map<std::string, chroot::ptr> chroot_map; |
---|
| 51 | |
---|
| 52 | /// Error codes. |
---|
| 53 | enum error_code |
---|
| 54 | { |
---|
| 55 | ALIAS_EXIST, ///< Alias already associated with chroot. |
---|
| 56 | CHROOT_NOTFOUND, ///< No such chroot. |
---|
| 57 | CHROOT_EXIST, ///< A chroot or alias already exists with this name. |
---|
| 58 | FILE_NOTREG, ///< File is not a regular file. |
---|
| 59 | FILE_OPEN, ///< Failed to open file. |
---|
| 60 | FILE_OWNER, ///< File is not owned by user root. |
---|
| 61 | FILE_PERMS ///< File has write permissions for others. |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | /// Exception type. |
---|
| 65 | typedef custom_error<error_code> error; |
---|
| 66 | |
---|
| 67 | /// A shared_ptr to a chroot_config object. |
---|
| 68 | typedef std::tr1::shared_ptr<chroot_config> ptr; |
---|
| 69 | |
---|
| 70 | /// The constructor. |
---|
| 71 | chroot_config (); |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * The constructor. |
---|
| 75 | * |
---|
| 76 | * @param file initialise using a configuration file or a whole |
---|
| 77 | * directory containing configuration files. |
---|
| 78 | * @param active true if the chroots in the configuration file are |
---|
| 79 | * active sessions, otherwise false. |
---|
| 80 | */ |
---|
| 81 | chroot_config (std::string const& file, |
---|
| 82 | bool active); |
---|
| 83 | |
---|
| 84 | /// The destructor. |
---|
| 85 | virtual ~chroot_config (); |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Add a configuration file or directory. The configuration file |
---|
| 89 | * or directory specified will be loaded. |
---|
| 90 | * |
---|
| 91 | * @param location initialise using a configuration file or a |
---|
| 92 | * whole directory containing configuration files. |
---|
| 93 | * @param active true if the chroots in the configuration file are |
---|
| 94 | * active sessions, otherwise false. |
---|
| 95 | */ |
---|
| 96 | void |
---|
| 97 | add (std::string const& location, |
---|
| 98 | bool active); |
---|
| 99 | |
---|
| 100 | private: |
---|
| 101 | /** |
---|
| 102 | * Add a configuration file. The configuration file specified |
---|
| 103 | * will be loaded. |
---|
| 104 | * |
---|
| 105 | * @param file the file to load. |
---|
| 106 | * @param active true if the chroots in the configuration file are |
---|
| 107 | * active sessions, otherwise false. |
---|
| 108 | */ |
---|
| 109 | void |
---|
| 110 | add_config_file (std::string const& file, |
---|
| 111 | bool active); |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Add a configuration directory. The configuration files in the |
---|
| 115 | * directory specified will all be loaded. |
---|
| 116 | * |
---|
| 117 | * @param dir the directory containing the files to load. |
---|
| 118 | * @param active true if the chroots in the configuration file are |
---|
| 119 | * active sessions, otherwise false. |
---|
| 120 | */ |
---|
| 121 | void |
---|
| 122 | add_config_directory (std::string const& dir, |
---|
| 123 | bool active); |
---|
| 124 | |
---|
| 125 | protected: |
---|
| 126 | /** |
---|
| 127 | * Add a chroot. The lists of chroots and aliases will be |
---|
| 128 | * updated. If a chroot or alias by the same name exists, the |
---|
| 129 | * chroot will not be added, and a warning will be logged. Af any |
---|
| 130 | * of the aliases already exist, a warning will be logged, and the |
---|
| 131 | * alias will not be added. |
---|
| 132 | * |
---|
| 133 | * @param chroot the chroot to add. |
---|
| 134 | * @param kconfig the chroot configuration. |
---|
| 135 | */ |
---|
| 136 | void |
---|
| 137 | add (chroot::ptr& chroot, |
---|
| 138 | keyfile const& kconfig); |
---|
| 139 | |
---|
| 140 | public: |
---|
| 141 | /** |
---|
| 142 | * Get a list of available chroots. |
---|
| 143 | * |
---|
| 144 | * @returns a list of available chroots. The list will be empty |
---|
| 145 | * if no chroots are available. |
---|
| 146 | */ |
---|
| 147 | chroot_list |
---|
| 148 | get_chroots () const; |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * Find a chroot by its name. |
---|
| 152 | * |
---|
| 153 | * @param name the chroot name |
---|
| 154 | * @returns the chroot if found, otherwise 0. |
---|
| 155 | */ |
---|
| 156 | const chroot::ptr |
---|
| 157 | find_chroot (std::string const& name) const; |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Find a chroot by its name or an alias. |
---|
| 161 | * |
---|
| 162 | * @param name the chroot name or alias. |
---|
| 163 | * @returns the chroot if found, otherwise 0. |
---|
| 164 | */ |
---|
| 165 | const chroot::ptr |
---|
| 166 | find_alias (std::string const& name) const; |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * Get the names (including aliases) of all the available chroots, |
---|
| 170 | * sorted in alphabetical order. |
---|
| 171 | * |
---|
| 172 | * @returns the list. The list will be empty if no chroots are |
---|
| 173 | * available. |
---|
| 174 | */ |
---|
| 175 | string_list |
---|
| 176 | get_chroot_list () const; |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * Print all the available chroots to the specified stream. |
---|
| 180 | * |
---|
| 181 | * @param stream the stream to output to. |
---|
| 182 | */ |
---|
| 183 | void |
---|
| 184 | print_chroot_list (std::ostream& stream) const; |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * Print a single line of all the available chroots to the |
---|
| 188 | * specified stream. |
---|
| 189 | * |
---|
| 190 | * @param stream the stream to output to. |
---|
| 191 | */ |
---|
| 192 | void |
---|
| 193 | print_chroot_list_simple (std::ostream& stream) const; |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * Print information about the specified chroots to the specified |
---|
| 197 | * stream. |
---|
| 198 | * |
---|
| 199 | * @param chroots a list of chroots to print. |
---|
| 200 | * @param stream the stream to output to. |
---|
| 201 | */ |
---|
| 202 | void |
---|
| 203 | print_chroot_info (string_list const& chroots, |
---|
| 204 | std::ostream& stream) const; |
---|
| 205 | |
---|
| 206 | /** |
---|
| 207 | * Print location information about the specified chroots to the |
---|
| 208 | * specified stream. |
---|
| 209 | * |
---|
| 210 | * @param chroots a list of chroots to print. |
---|
| 211 | * @param stream the stream to output to. |
---|
| 212 | */ |
---|
| 213 | void |
---|
| 214 | print_chroot_location (string_list const& chroots, |
---|
| 215 | std::ostream& stream) const; |
---|
| 216 | |
---|
| 217 | /** |
---|
| 218 | * Print configuration of the specified chroots to the specified |
---|
| 219 | * stream. |
---|
| 220 | * |
---|
| 221 | * @param chroots a list of chroots to print. |
---|
| 222 | * @param stream the stream to output to. |
---|
| 223 | */ |
---|
| 224 | void |
---|
| 225 | print_chroot_config (string_list const& chroots, |
---|
| 226 | std::ostream& stream) const; |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * Check that all the chroots specified exist. |
---|
| 230 | * |
---|
| 231 | * @param chroots a list of chroots to validate. |
---|
| 232 | * @returns a list of invalid chroots. The list will be empty if |
---|
| 233 | * all chroots are valid. |
---|
| 234 | */ |
---|
| 235 | string_list |
---|
| 236 | validate_chroots (string_list const& chroots) const; |
---|
| 237 | |
---|
| 238 | private: |
---|
| 239 | /** |
---|
| 240 | * Load a configuration file. If there are problems with the |
---|
| 241 | * configuration file, an error will be thrown. The file must be |
---|
| 242 | * owned by root, not writable by other, and be a regular file. |
---|
| 243 | * |
---|
| 244 | * @param file the file to load. |
---|
| 245 | * @param active true if the chroots in the configuration file are |
---|
| 246 | * active sessions, otherwise false. |
---|
| 247 | */ |
---|
| 248 | void |
---|
| 249 | load_data (std::string const& file, |
---|
| 250 | bool active); |
---|
| 251 | |
---|
| 252 | protected: |
---|
| 253 | /** |
---|
| 254 | * Parse a loaded configuration file. If there are problems with |
---|
| 255 | * the configuration file, an error will be thrown. |
---|
| 256 | * |
---|
| 257 | * @param stream the data stream to parse. |
---|
| 258 | * @param active true if the chroots in the configuration file are |
---|
| 259 | * active sessions, otherwise false. |
---|
| 260 | */ |
---|
| 261 | virtual void |
---|
| 262 | parse_data (std::istream& stream, |
---|
| 263 | bool active); |
---|
| 264 | |
---|
| 265 | /** |
---|
| 266 | * Load a keyfile. If there are problems with the configuration |
---|
| 267 | * file, an error will be thrown. |
---|
| 268 | * |
---|
| 269 | * @param kconfig the chroot configuration. |
---|
| 270 | * @param active true if the chroots in the configuration file are |
---|
| 271 | * active sessions, otherwise false. |
---|
| 272 | */ |
---|
| 273 | virtual void |
---|
| 274 | load_keyfile (keyfile& kconfig, |
---|
| 275 | bool active); |
---|
| 276 | |
---|
| 277 | /// A list of chroots (name->chroot mapping). |
---|
| 278 | chroot_map chroots; |
---|
| 279 | /// A list of aliases (alias->name mapping). |
---|
| 280 | string_map aliases; |
---|
| 281 | }; |
---|
| 282 | |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | #endif /* SBUILD_CHROOT_CONFIG_H */ |
---|
| 286 | |
---|
| 287 | /* |
---|
| 288 | * Local Variables: |
---|
| 289 | * mode:C++ |
---|
| 290 | * End: |
---|
| 291 | */ |
---|