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
crate::ix!();
pub fn abs_path_for_config_val(
path: &Path,
net_specific: Option<bool>) -> PathBuf {
let net_specific: bool = net_specific.unwrap_or(true);
if path.is_absolute() {
return path.to_path_buf();
}
let base = match net_specific {
true => G_ARGS.lock().cs_args.lock().get_data_dir_net(),
false => G_ARGS.lock().cs_args.lock().get_data_dir_base(),
};
let mut builder = PathBuf::new();
builder.push(canonicalize(base).unwrap());
builder.push(canonicalize(path).unwrap());
builder
}
pub fn get_config_file(conf_path: &str) -> PathBuf {
let path = Path::new(conf_path);
abs_path_for_config_val(&path,Some(false))
}
pub fn check_data_dir_option() -> bool {
let datadir: String = G_ARGS
.lock()
.cs_args
.lock()
.get_arg("-datadir", "");
let path = Path::new(&datadir);
datadir.is_empty() || std::fs::canonicalize(path).unwrap().is_dir()
}
pub fn check_valid(
key: &str,
val: &SettingsValue,
flags: u32,
error: &mut String) -> bool {
if val.0.is_bool()
&& (flags & ArgsManagerFlags::ALLOW_BOOL.bits()) == 0
{
*error = format!{"Negating of -{} is meaningless and therefore forbidden",key};
return false;
}
true
}
pub fn get_config_options<R: std::io::Read>(
stream: &mut std::io::BufReader<R>,
filepath: &str,
error: &mut String,
options: &mut Vec<(String,String)>,
sections: &mut LinkedList<SectionInfo>) -> bool {
let mut str_: String = String::default();
let mut prefix: String = String::default();
let mut pos: Option<usize> = None;
let mut linenr: i32 = 1;
while stream.read_line(&mut str_).is_ok() {
let mut used_hash: bool = false;
pos = str_.find('#');
if pos != None {
str_ = str_[0..pos.unwrap()].to_string();
used_hash = true;
}
lazy_static!{
static ref pattern: String = " \t\r\n".to_string();
}
str_ = trim_string(&str_,Some(pattern.as_str()));
if str_.len() != 0 {
if str_.chars().nth(0).unwrap() == '['
&& str_.chars().nth(str_.len() - 1).unwrap() == ']' {
let section: String = str_[1..str_.len() - 2].to_string();
let info = SectionInfo::new(
§ion,
filepath,
linenr
);
sections.push_back(info);
prefix = format!{"{}.", section};
} else {
if str_.chars().nth(0).unwrap() == '-' {
*error = format!{
"parse error on line {}: {}, options in configuration file must be specified without leading -",
linenr,
str_
};
return false;
} else {
pos = str_.find('=');
if pos != None {
let name: String = format!{
"{}{}",
prefix,
&trim_string(
&str_[0..pos.unwrap()].to_string(),
Some(&*pattern)
)
};
let value: String = trim_string(
&str_
.chars()
.nth(pos.unwrap() + 1)
.unwrap()
.to_string(),
Some(&*pattern)
);
if used_hash && name.find("rpcpassword") != None {
*error = format!{
"parse error on line {}, using # in rpcpassword can be ambiguous and should be avoided",
linenr
};
return false;
}
options.push((name.clone(), value));
pos = name.rfind('.');
if pos != None && prefix.len() <= pos.unwrap() {
let info = SectionInfo::new(&name[0..pos.unwrap()],filepath,linenr);
sections.push_back(info);
}
} else {
*error = format!{
"parse error on line {}: {}",
linenr,
str_
};
if str_.len() >= 2 && &str_[0..2] == "no" {
*error = format!{
"{}, if you intended to specify a negated option, use {}=1 instead",
error,
str_
};
}
return false;
}
}
}
}
linenr += 1;
}
true
}
impl ArgsManagerInner {
pub fn read_config_stream<R: std::io::Read>(&mut self,
stream: &mut std::io::BufReader<R>,
filepath: &str,
error: &mut String,
ignore_invalid_keys: Option<bool>) -> bool {
let ignore_invalid_keys: bool = ignore_invalid_keys.unwrap_or(false);
let mut options = Vec::<(String,String)>::default();
if !get_config_options(
stream,
filepath,
error,
&mut options,
&mut self.config_sections)
{
return false;
}
for option in options.iter() {
let mut section = String::default();
let mut key: String = option.0.to_string();
let value: SettingsValue = interpret_option(
&mut section,
&mut key,
&option.1
);
let arg = format!{"-{}",key};
let flags: Option::<u32> = self.get_arg_flags(&arg);
if flags.is_some() {
if !check_valid(&key,&value,flags.unwrap(),error) {
return false;
}
self.settings
.ro_config
.get_mut(§ion)
.unwrap()
.get_mut(&key)
.unwrap()
.push(value);
} else {
if ignore_invalid_keys {
log_printf!(
"Ignoring unknown configuration value %s\n",
option.0
);
} else {
*error = format!{
"Invalid configuration value {}",
option.0
};
return false;
}
}
}
true
}
pub fn read_config_files(&mut self,
error: &mut String,
ignore_invalid_keys: Option<bool>) -> bool {
let ignore_invalid_keys: bool = ignore_invalid_keys.unwrap_or(false);
self.settings.ro_config.clear();
self.config_sections.clear();
let conf_path: String = self.get_arg("-conf", BITCOIN_CONF_FILENAME);
let mut file: Result<File,_> = File::open(get_config_file(&conf_path));
if self.is_arg_set("-conf") && !file.is_ok() {
*error = format!{
"specified config file \"{}\" could not be opened.",
conf_path
};
return false;
}
if file.is_ok() {
let mut stream = BufReader::new(file.unwrap());
if !self.read_config_stream(
&mut stream,
&conf_path,
error,
Some(ignore_invalid_keys))
{
return false;
}
let mut use_conf_file: bool = true;;
let includes = self.settings.command_line_options.get("includeconf");
if includes.is_some() {
assert!(
SettingsSpan::from(includes.unwrap()).last_negated()
);
use_conf_file = false;
}
if use_conf_file {
let chain_id: String = self.get_chain_name().unwrap();
let mut conf_file_names = Vec::<String>::default();
let mut add_includes = |
network: &str,
skip: Option::<usize>,
conf_file_names: &mut Vec<String>,
settings: &Settings
| {
let skip = skip.unwrap_or(0);
let mut num_values: usize = 0;
let section = settings.ro_config.get(network);
if section.is_some() {
let values = section.unwrap().get("includeconf");
if values.is_some() {
let span = SettingsSpan::from(values.unwrap());
let vlen = values.as_ref().unwrap().len();
for i in max(skip,span.negated())..vlen {
conf_file_names.push(values.unwrap()[i].to_string());
}
num_values = values.unwrap().len();
}
}
return num_values;
};
let chain_includes: usize = add_includes(&chain_id, None, &mut conf_file_names, &self.settings);
let default_includes: usize = add_includes("", None, &mut conf_file_names, &self.settings);
for conf_file_name in conf_file_names.iter() {
let mut conf_file_stream: Result<File,_> = File::open(get_config_file(conf_file_name));
if conf_file_stream.is_ok() {
let mut reader = BufReader::new(conf_file_stream.unwrap());
if !self.read_config_stream(
&mut reader,
conf_file_name,
error,
Some(ignore_invalid_keys))
{
return false;
}
log_printf!(
"Included configuration file {}\n",
conf_file_name
);
} else {
*error = format!{
"Failed to include configuration file {}",
conf_file_name
};
return false;
}
}
conf_file_names.clear();
add_includes(&chain_id, Some(chain_includes), &mut conf_file_names, &self.settings);
add_includes("", Some(default_includes), &mut conf_file_names, &self.settings);
let chain_id_final: String = self.get_chain_name().unwrap();
if chain_id_final != chain_id {
add_includes(&chain_id_final, None, &mut conf_file_names, &self.settings);
}
for conf_file_name in conf_file_names.iter() {
eprintln!(
"warning: -includeconf cannot be used from included files; ignoring -includeconf={}\n",
conf_file_name
);
}
}
}
G_ARGS
.lock()
.cs_args
.lock()
.clear_path_cache();
if !check_data_dir_option() {
*error = format!{
"specified data directory \"{}\" does not exist.",
self.get_arg("-datadir","")
};
return false;
}
true
}
}