1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5/conf/config_parser.hpp"
#include "utils/iniparser.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
#include "libdnf5/utils/fs/file.hpp"
#include <map>
#include <utility>
namespace libdnf5 {
static void read(ConfigParser & cfg_parser, IniParser & parser) {
IniParser::ItemType readed_type;
while ((readed_type = parser.next()) != IniParser::ItemType::END_OF_INPUT) {
auto section = parser.get_section();
if (readed_type == IniParser::ItemType::SECTION) {
cfg_parser.add_section(std::move(section), std::move(parser.get_raw_item()));
} else if (readed_type == IniParser::ItemType::KEY_VAL) {
cfg_parser.set_value(
section, std::move(parser.get_key()), std::move(parser.get_value()), std::move(parser.get_raw_item()));
} else if (readed_type == IniParser::ItemType::COMMENT_LINE || readed_type == IniParser::ItemType::EMPTY_LINE) {
if (section.empty()) {
cfg_parser.get_header() += parser.get_raw_item();
} else {
cfg_parser.add_comment_line(section, std::move(parser.get_raw_item()));
}
}
}
}
ConfigParserSectionNotFoundError::ConfigParserSectionNotFoundError(const std::string & section)
: ConfigParserError(M_("Section \"{}\" not found"), section) {}
ConfigParserOptionNotFoundError::ConfigParserOptionNotFoundError(
const std::string & section, const std::string & option)
: ConfigParserError(M_("Section \"{}\" does not contain option \"{}\""), section, option) {}
class ConfigParser::Impl {
private:
friend ConfigParser;
Container data;
int item_number{0};
std::string header;
std::map<std::string, std::string> raw_items;
};
ConfigParser::ConfigParser() : p_impl(std::make_unique<Impl>()) {}
ConfigParser::~ConfigParser() = default;
ConfigParser::ConfigParser(const ConfigParser & src) : p_impl(new Impl(*src.p_impl)) {}
ConfigParser::ConfigParser(ConfigParser && src) noexcept = default;
ConfigParser & ConfigParser::operator=(const ConfigParser & src) {
if (this != &src) {
if (p_impl) {
*p_impl = *src.p_impl;
} else {
p_impl = std::make_unique<Impl>(*src.p_impl);
}
}
return *this;
}
ConfigParser & ConfigParser::operator=(ConfigParser && src) noexcept = default;
void ConfigParser::read(const std::string & file_path) try {
IniParser parser(file_path);
::libdnf5::read(*this, parser);
} catch (const FileSystemError & e) {
if (e.get_error_code() == ENOENT) {
libdnf5::throw_with_nested(MissingConfigError(M_("Configuration file \"{}\" not found"), file_path));
} else {
libdnf5::throw_with_nested(
InaccessibleConfigError(M_("Unable to access configuration file \"{}\""), file_path));
}
} catch (const Error & e) {
libdnf5::throw_with_nested(InvalidConfigError(M_("Error in configuration file \"{}\""), file_path));
}
bool ConfigParser::add_section(const std::string & section, const std::string & raw_line) {
if (p_impl->data.find(section) != p_impl->data.end()) {
return false;
}
if (!raw_line.empty()) {
p_impl->raw_items[section] = raw_line;
}
p_impl->data[section] = {};
return true;
}
bool ConfigParser::add_section(const std::string & section) {
return add_section(section, "");
}
bool ConfigParser::add_section(std::string && section, std::string && raw_line) {
if (p_impl->data.find(section) != p_impl->data.end()) {
return false;
}
if (!raw_line.empty()) {
p_impl->raw_items[section] = std::move(raw_line);
}
p_impl->data[std::move(section)] = {};
return true;
}
bool ConfigParser::add_section(std::string && section) {
return add_section(std::move(section), "");
}
bool ConfigParser::has_section(const std::string & section) const noexcept {
return p_impl->data.find(section) != p_impl->data.end();
}
bool ConfigParser::has_option(const std::string & section, const std::string & key) const noexcept {
auto section_iter = p_impl->data.find(section);
return section_iter != p_impl->data.end() && section_iter->second.find(key) != section_iter->second.end();
}
static std::string create_raw_item(const std::string & value, const std::string & old_raw_item) {
auto eql_pos = old_raw_item.find('=');
if (eql_pos == std::string::npos) {
return "";
}
auto value_pos = old_raw_item.find_first_not_of(" \t", eql_pos + 1);
auto key_and_delim_length = value_pos != std::string::npos ? value_pos : old_raw_item.length();
return old_raw_item.substr(0, key_and_delim_length) + value + '\n';
}
void ConfigParser::set_value(
const std::string & section, const std::string & key, const std::string & value, const std::string & raw_item) {
auto section_iter = p_impl->data.find(section);
if (section_iter == p_impl->data.end()) {
throw ConfigParserSectionNotFoundError(section);
}
if (raw_item.empty()) {
p_impl->raw_items.erase(section + ']' + key);
} else {
p_impl->raw_items[section + ']' + key] = raw_item;
}
section_iter->second[key] = value;
}
void ConfigParser::set_value(const std::string & section, const std::string & key, const std::string & value) {
auto raw_iter = p_impl->raw_items.find(section + ']' + key);
auto raw = create_raw_item(value, raw_iter != p_impl->raw_items.end() ? raw_iter->second : "");
set_value(section, key, value, raw);
}
void ConfigParser::set_value(
const std::string & section, std::string && key, std::string && value, std::string && raw_item) {
auto section_iter = p_impl->data.find(section);
if (section_iter == p_impl->data.end()) {
throw ConfigParserSectionNotFoundError(section);
}
if (raw_item.empty()) {
p_impl->raw_items.erase(section + ']' + key);
} else {
p_impl->raw_items[section + ']' + key] = std::move(raw_item);
}
section_iter->second[std::move(key)] = std::move(value);
}
void ConfigParser::set_value(const std::string & section, std::string && key, std::string && value) {
auto raw_iter = p_impl->raw_items.find(section + ']' + key);
auto raw = create_raw_item(value, raw_iter != p_impl->raw_items.end() ? raw_iter->second : "");
set_value(section, std::move(key), std::move(value), std::move(raw));
}
bool ConfigParser::remove_section(const std::string & section) {
auto removed = p_impl->data.erase(section) > 0;
if (removed) {
p_impl->raw_items.erase(section);
}
return removed;
}
bool ConfigParser::remove_option(const std::string & section, const std::string & key) {
auto section_iter = p_impl->data.find(section);
if (section_iter == p_impl->data.end()) {
return false;
}
auto removed = section_iter->second.erase(key) > 0;
if (removed) {
p_impl->raw_items.erase(section + ']' + key);
}
return removed;
}
void ConfigParser::add_comment_line(const std::string & section, const std::string & comment) {
auto section_iter = p_impl->data.find(section);
if (section_iter == p_impl->data.end()) {
throw ConfigParserSectionNotFoundError(section);
}
section_iter->second["#" + std::to_string(++p_impl->item_number)] = comment;
}
void ConfigParser::add_comment_line(const std::string & section, std::string && comment) {
auto section_iter = p_impl->data.find(section);
if (section_iter == p_impl->data.end()) {
throw ConfigParserSectionNotFoundError(section);
}
section_iter->second["#" + std::to_string(++p_impl->item_number)] = std::move(comment);
}
const std::string & ConfigParser::get_value(const std::string & section, const std::string & key) const {
auto sect = p_impl->data.find(section);
if (sect == p_impl->data.end()) {
throw ConfigParserSectionNotFoundError(section);
}
auto key_val = sect->second.find(key);
if (key_val == sect->second.end()) {
throw ConfigParserOptionNotFoundError(section, key);
}
return key_val->second;
}
const std::string & ConfigParser::get_header() const noexcept {
return p_impl->header;
}
std::string & ConfigParser::get_header() noexcept {
return p_impl->header;
}
const ConfigParser::Container & ConfigParser::get_data() const noexcept {
return p_impl->data;
}
ConfigParser::Container & ConfigParser::get_data() noexcept {
return p_impl->data;
}
static void write_key_vals(
utils::fs::File & file,
const std::string & section,
const ConfigParser::Container::mapped_type & key_val_map,
const std::map<std::string, std::string> & raw_items,
bool & prepend_new_line) {
for (const auto & key_val : key_val_map) {
if (prepend_new_line) {
file.putc('\n');
}
auto first = key_val.first[0];
if (first == '#' || first == ';') {
file.write(key_val.second);
prepend_new_line = key_val.second.empty() || key_val.second.back() != '\n';
} else {
auto raw_item = raw_items.find(section + ']' + key_val.first);
if (raw_item != raw_items.end()) {
file.write(raw_item->second);
prepend_new_line = raw_item->second.empty() || raw_item->second.back() != '\n';
} else {
file.write(key_val.first);
file.putc('=');
for (const auto chr : key_val.second) {
file.putc(chr);
if (chr == '\n') {
file.putc(' ');
}
}
file.putc('\n');
prepend_new_line = false;
}
}
}
}
static void write_section(
utils::fs::File & file,
const std::string & section,
const ConfigParser::Container::mapped_type & key_val_map,
const std::map<std::string, std::string> & raw_items,
bool & prepend_new_line) {
if (prepend_new_line) {
file.putc('\n');
}
auto raw_item = raw_items.find(section);
if (raw_item != raw_items.end()) {
file.write(raw_item->second);
prepend_new_line = raw_item->second.empty() || raw_item->second.back() != '\n';
} else {
file.write(fmt::format("[{}]\n", section));
prepend_new_line = false;
}
write_key_vals(file, section, key_val_map, raw_items, prepend_new_line);
}
void ConfigParser::write(const std::string & file_path, bool append) const {
utils::fs::File file(file_path, append ? "a" : "w");
bool prepend_new_line = append;
if (!p_impl->header.empty()) {
if (prepend_new_line) {
file.putc('\n');
}
file.write(p_impl->header);
prepend_new_line = p_impl->header.back() != '\n';
}
for (const auto & section : p_impl->data) {
write_section(file, section.first, section.second, p_impl->raw_items, prepend_new_line);
}
// Make sure file ends with newline character '\n'.
if (prepend_new_line) {
file.putc('\n');
}
}
void ConfigParser::write(const std::string & file_path, bool append, const std::string & section) const {
auto sit = p_impl->data.find(section);
if (sit == p_impl->data.end()) {
throw ConfigParserSectionNotFoundError(section);
}
utils::fs::File file(file_path, append ? "a" : "w");
bool prepend_new_line = append;
write_section(file, sit->first, sit->second, p_impl->raw_items, prepend_new_line);
// Make sure file ends with newline character '\n'.
if (prepend_new_line) {
file.putc('\n');
}
}
} // namespace libdnf5
|