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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/util/settings.h]
/**
| Settings value type
| (string/integer/boolean/null variant).
|
| @note UniValue is used here for convenience
| and because it can be easily serialized
| in a readable format. But any other
| variant type that can be assigned
| strings, int64_t, and bool values and
| has get_str(), get_int64(), get_bool(),
| isNum(), isBool(), isFalse(), isTrue()
| and
| isNull() methods can be substituted if
| there's a need to move away from
| UniValue. (An implementation with
| boost::variant was posted at
| https://github.com/bitcoin/bitcoin/pull/15934/files#r337691812)
*/
pub struct SettingsValue(pub UniValue);
impl PartialEq<SettingsValue> for SettingsValue {
fn eq(&self, other: &SettingsValue) -> bool {
todo!();
/*
return a.write() == b.write();
*/
}
}
impl Eq for SettingsValue {}
impl std::fmt::Display for SettingsValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!();
/*
os << value.write();
return os;
*/
}
}
pub struct SettingsTuple((String,SettingsValue));
impl std::fmt::Display for SettingsTuple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!();
/*
SettingsValue out(SettingsValue::VOBJ);
out.__pushKV(kv.first, kv.second);
os << out.write();
return os;
*/
}
}
/**
| Stored settings. This struct combines settings
| from the command line, a read-only
| configuration file, and a read-write runtime
| settings file.
*/
#[derive(Default)]
pub struct Settings {
/**
| Map of setting name to forced setting
| value.
|
*/
pub forced_settings: HashMap<String,SettingsValue>,
/**
| Map of setting name to list of command
| line values.
|
*/
pub command_line_options: HashMap<String,Vec<SettingsValue>>,
/**
| Map of setting name to read-write file
| setting value.
|
*/
pub rw_settings: HashMap<String,SettingsValue>,
/**
| Map of config section name and setting
| name to list of config file values.
|
*/
pub ro_config: HashMap<String,HashMap<String,Vec<SettingsValue>>>,
}
/**
| Accessor for list of settings that skips
| negated values when iterated over.
|
| The last boolean `false` value in the list and
| all earlier values are considered negated.
*/
pub struct SettingsSpan {
pub data: *const SettingsValue,
pub size: usize,
}
impl Default for SettingsSpan {
fn default() -> Self {
Self {
data: null(),
size: 0,
}
}
}
impl From<&SettingsValue> for SettingsSpan {
fn from(value: &SettingsValue) -> Self {
todo!();
/*
: settings_span(&value, 1),
*/
}
}
impl From<&Vec<SettingsValue>> for SettingsSpan {
fn from(vec: &Vec<SettingsValue>) -> Self {
todo!();
/*
: settings_span(vec.data(), vec.size()),
*/
}
}
impl SettingsSpan {
pub fn new(
data: *const SettingsValue,
size: usize) -> Self {
todo!();
/*
: data(data),
: size(size),
*/
}
/**
| Pointer to first non-negated value.
|
*/
pub fn begin(&self) -> *const SettingsValue {
todo!();
/*
return data + negated();
*/
}
/**
| Pointer to end of values.
|
*/
pub fn end(&self) -> *const SettingsValue {
todo!();
/*
return data + size;
*/
}
/**
| True if there are any non-negated values.
|
*/
pub fn empty(&self) -> bool {
todo!();
/*
return size == 0 || last_negated();
*/
}
/**
| True if the last value is negated.
|
*/
pub fn last_negated(&self) -> bool {
todo!();
/*
return size > 0 && data[size - 1].isFalse();
*/
}
/**
| Number of negated values.
|
*/
pub fn negated(&self) -> usize {
todo!();
/*
for (size_t i = size; i > 0; --i) {
if (data[i - 1].isFalse()) return i; // Return number of negated values (position of last false value)
}
return 0;
*/
}
}
//-------------------------------------------[.cpp/bitcoin/src/util/settings.cpp]
pub enum Source {
FORCED,
COMMAND_LINE,
RW_SETTINGS,
CONFIG_FILE_NETWORK_SECTION,
CONFIG_FILE_DEFAULT_SECTION
}
/**
| Merge settings from multiple sources in
| precedence order:
|
| Forced config > command line > read-write
| settings file > config file network-specific
| section > config file default section
|
| This function is provided with a callback
| function fn that contains specific logic for
| how to merge the sources.
*/
pub fn merge_settings<Fn>(
settings: &Settings,
section: &String,
name: &String,
fn_: Fn) {
todo!();
/*
// Merge in the forced settings
if (auto* value = FindKey(settings.forced_settings, name)) {
fn(SettingsSpan(*value), Source::FORCED);
}
// Merge in the command-line options
if (auto* values = FindKey(settings.command_line_options, name)) {
fn(SettingsSpan(*values), Source::COMMAND_LINE);
}
// Merge in the read-write settings
if (const SettingsValue* value = FindKey(settings.rw_settings, name)) {
fn(SettingsSpan(*value), Source::RW_SETTINGS);
}
// Merge in the network-specific section of the config file
if (!section.empty()) {
if (auto* map = FindKey(settings.ro_config, section)) {
if (auto* values = FindKey(*map, name)) {
fn(SettingsSpan(*values), Source::CONFIG_FILE_NETWORK_SECTION);
}
}
}
// Merge in the default section of the config file
if (auto* map = FindKey(settings.ro_config, "")) {
if (auto* values = FindKey(*map, name)) {
fn(SettingsSpan(*values), Source::CONFIG_FILE_DEFAULT_SECTION);
}
}
*/
}
/**
| Read settings file.
|
*/
pub fn read_settings(
path: &Path,
values: &mut HashMap<String,SettingsValue>,
errors: &mut Vec<String>) -> bool {
todo!();
/*
values.clear();
errors.clear();
// Ok for file to not exist
if (!fs::exists(path)) return true;
fsbridge::ifstream file;
file.open(path);
if (!file.is_open()) {
errors.emplace_back(strprintf("%s. Please check permissions.", fs::PathToString(path)));
return false;
}
SettingsValue in;
if (!in.read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
errors.emplace_back(strprintf("Unable to parse settings file %s", fs::PathToString(path)));
return false;
}
if (file.fail()) {
errors.emplace_back(strprintf("Failed reading settings file %s", fs::PathToString(path)));
return false;
}
file.close(); // Done with file descriptor. Release while copying data.
if (!in.isObject()) {
errors.emplace_back(strprintf("Found non-object value %s in settings file %s", in.write(), fs::PathToString(path)));
return false;
}
const std::vector<std::string>& in_keys = in.getKeys();
const std::vector<SettingsValue>& in_values = in.getValues();
for (size_t i = 0; i < in_keys.size(); ++i) {
auto inserted = values.emplace(in_keys[i], in_values[i]);
if (!inserted.second) {
errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path)));
}
}
return errors.empty();
*/
}
/**
| Write settings file.
|
*/
pub fn write_settings(
path: &Path,
values: &HashMap<String,SettingsValue>,
errors: &mut Vec<String>) -> bool {
todo!();
/*
SettingsValue out(SettingsValue::VOBJ);
for (const auto& value : values) {
out.__pushKV(value.first, value.second);
}
fsbridge::ofstream file;
file.open(path);
if (file.fail()) {
errors.emplace_back(strprintf("Error: Unable to open settings file %s for writing", fs::PathToString(path)));
return false;
}
file << out.write(/* prettyIndent= */ 1, /* indentLevel= */ 4) << std::endl;
file.close();
return true;
*/
}
/**
| Get settings value from combined sources:
| forced settings, command line arguments,
| runtime read-write settings, and the
| read-only config file.
|
| -----------
| @param ignore_default_section_config
|
| - ignore values in the default section
| of the config file (part before any [section]
| keywords)
| ----------
| @param get_chain_name
|
| - enable special backwards compatible
| behavior for GetChainName
|
*/
pub fn get_setting(
settings: &Settings,
section: &str,
name: &str,
ignore_default_section_config: bool,
get_chain_name: bool) -> SettingsValue {
todo!();
/*
SettingsValue result;
bool done = false; // Done merging any more settings sources.
MergeSettings(settings, section, name, [&](SettingsSpan span, Source source) {
// Weird behavior preserved for backwards compatibility: Apply negated
// setting even if non-negated setting would be ignored. A negated
// value in the default section is applied to network specific options,
// even though normal non-negated values there would be ignored.
const bool never_ignore_negated_setting = span.last_negated();
// Weird behavior preserved for backwards compatibility: Take first
// assigned value instead of last. In general, later settings take
// precedence over early settings, but for backwards compatibility in
// the config file the precedence is reversed for all settings except
// chain name settings.
const bool reverse_precedence =
(source == Source::CONFIG_FILE_NETWORK_SECTION || source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
!get_chain_name;
// Weird behavior preserved for backwards compatibility: Negated
// -regtest and -testnet arguments which you would expect to override
// values set in the configuration file are currently accepted but
// silently ignored. It would be better to apply these just like other
// negated values, or at least warn they are ignored.
const bool skip_negated_command_line = get_chain_name;
if (done) return;
// Ignore settings in default config section if requested.
if (ignore_default_section_config && source == Source::CONFIG_FILE_DEFAULT_SECTION &&
!never_ignore_negated_setting) {
return;
}
// Skip negated command line settings.
if (skip_negated_command_line && span.last_negated()) return;
if (!span.empty()) {
result = reverse_precedence ? span.begin()[0] : span.end()[-1];
done = true;
} else if (span.last_negated()) {
result = false;
done = true;
}
});
return result;
*/
}
/**
| Get combined setting value similar to
| GetSetting(), except if setting was specified
| multiple times, return a list of all the
| values specified.
*/
pub fn get_settings_list(
settings: &Settings,
section: &String,
name: &String,
ignore_default_section_config: bool) -> Vec<SettingsValue> {
todo!();
/*
std::vector<SettingsValue> result;
bool done = false; // Done merging any more settings sources.
bool prev_negated_empty = false;
MergeSettings(settings, section, name, [&](SettingsSpan span, Source source) {
// Weird behavior preserved for backwards compatibility: Apply config
// file settings even if negated on command line. Negating a setting on
// command line will ignore earlier settings on the command line and
// ignore settings in the config file, unless the negated command line
// value is followed by non-negated value, in which case config file
// settings will be brought back from the dead (but earlier command
// line settings will still be ignored).
const bool add_zombie_config_values =
(source == Source::CONFIG_FILE_NETWORK_SECTION || source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
!prev_negated_empty;
// Ignore settings in default config section if requested.
if (ignore_default_section_config && source == Source::CONFIG_FILE_DEFAULT_SECTION) return;
// Add new settings to the result if isn't already complete, or if the
// values are zombies.
if (!done || add_zombie_config_values) {
for (const auto& value : span) {
if (value.isArray()) {
result.insert(result.end(), value.getValues().begin(), value.getValues().end());
} else {
result.push_back(value);
}
}
}
// If a setting was negated, or if a setting was forced, set
// done to true to ignore any later lower priority settings.
done |= span.negated() > 0 || source == Source::FORCED;
// Update the negated and empty state used for the zombie values check.
prev_negated_empty |= span.last_negated() && result.empty();
});
return result;
*/
}
/**
| Return true if a setting is set in the default
| config file section, and not overridden by
| a higher priority command-line or network
| section value.
|
| This is used to provide user warnings about
| values that might be getting ignored
| unintentionally.
*/
pub fn only_has_default_section_setting(
settings: &Settings,
section: &str,
name: &str) -> bool {
todo!();
/*
bool has_default_section_setting = false;
bool has_other_setting = false;
MergeSettings(settings, section, name, [&](SettingsSpan span, Source source) {
if (span.empty()) return;
else if (source == Source::CONFIG_FILE_DEFAULT_SECTION) has_default_section_setting = true;
else has_other_setting = true;
});
// If a value is set in the default section and not explicitly overwritten by the
// user on the command line or in a different section, then we want to enable
// warnings about the value being ignored.
return has_default_section_setting && !has_other_setting;
*/
}