1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![warn(clippy::cognitive_complexity)]
15#![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![deny(clippy::print_stderr)]
28#![deny(clippy::print_stdout)]
29#![warn(clippy::rc_buffer)]
30#![deny(clippy::ref_option_ref)]
31#![warn(clippy::semicolon_if_nothing_returned)]
32#![warn(clippy::trait_duplication_in_bounds)]
33#![deny(clippy::unchecked_time_subtraction)]
34#![deny(clippy::unnecessary_wraps)]
35#![warn(clippy::unseparated_literal_suffix)]
36#![deny(clippy::unwrap_used)]
37#![deny(clippy::mod_module_files)]
38#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] use std::collections::HashMap;
48use std::path::{Path, PathBuf};
49
50use serde::{Deserialize, Serialize};
51use std::borrow::Cow;
52#[cfg(feature = "expand-paths")]
53use {directories::BaseDirs, std::sync::LazyLock};
54
55use tor_error::{ErrorKind, HasKind};
56
57#[cfg(all(test, feature = "expand-paths"))]
58use std::ffi::OsStr;
59
60#[cfg(feature = "address")]
61pub mod addr;
62
63#[cfg(feature = "arti-client")]
64mod arti_client_paths;
65
66#[cfg(feature = "arti-client")]
67pub use arti_client_paths::arti_client_base_resolver;
68
69#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
78#[serde(transparent)]
79pub struct CfgPath(PathInner);
80
81#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
85#[serde(untagged)]
86enum PathInner {
87 Literal(LiteralPath),
89 Shell(String),
91}
92
93#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
94struct LiteralPath {
99 literal: PathBuf,
101}
102
103#[derive(thiserror::Error, Debug, Clone)]
105#[non_exhaustive]
106#[cfg_attr(test, derive(PartialEq))]
107pub enum CfgPathError {
108 #[error("Unrecognized variable {0} in path")]
110 UnknownVar(String),
111 #[error(
113 "Couldn't determine XDG Project Directories, needed to resolve a path; probably, unable to determine HOME directory"
114 )]
115 NoProjectDirs,
116 #[error("Can't construct base directories to resolve a path element")]
118 NoBaseDirs,
119 #[error("Can't find the path to the current binary")]
121 NoProgramPath,
122 #[error("Can't find the directory of the current binary")]
124 NoProgramDir,
125 #[error("Invalid path string: {0:?}")]
129 InvalidString(String),
130 #[error(
132 "Variable interpolation $ is not supported (tor-config/expand-paths feature disabled)); $ must still be doubled"
133 )]
134 VariableInterpolationNotSupported(String),
135 #[error("Home dir ~/ is not supported (tor-config/expand-paths feature disabled)")]
137 HomeDirInterpolationNotSupported(String),
138}
139
140impl HasKind for CfgPathError {
141 fn kind(&self) -> ErrorKind {
142 use CfgPathError as E;
143 use ErrorKind as EK;
144 match self {
145 E::UnknownVar(_) | E::InvalidString(_) => EK::InvalidConfig,
146 E::NoProjectDirs | E::NoBaseDirs => EK::NoHomeDirectory,
147 E::NoProgramPath | E::NoProgramDir => EK::InvalidConfig,
148 E::VariableInterpolationNotSupported(_) | E::HomeDirInterpolationNotSupported(_) => {
149 EK::FeatureDisabled
150 }
151 }
152 }
153}
154
155#[derive(Clone, Debug, Default)]
166pub struct CfgPathResolver {
167 vars: HashMap<String, Result<Cow<'static, Path>, CfgPathError>>,
170}
171
172impl CfgPathResolver {
173 #[cfg(feature = "expand-paths")]
175 fn get_var(&self, var: &str) -> Result<Cow<'static, Path>, CfgPathError> {
176 match self.vars.get(var) {
177 Some(val) => val.clone(),
178 None => Err(CfgPathError::UnknownVar(var.to_owned())),
179 }
180 }
181
182 pub fn set_var(
203 &mut self,
204 var: impl Into<String>,
205 val: Result<Cow<'static, Path>, CfgPathError>,
206 ) {
207 self.vars.insert(var.into(), val);
208 }
209
210 #[cfg(all(test, feature = "expand-paths"))]
212 fn from_pairs<K, V>(vars: impl IntoIterator<Item = (K, V)>) -> CfgPathResolver
213 where
214 K: Into<String>,
215 V: AsRef<OsStr>,
216 {
217 let mut path_resolver = CfgPathResolver::default();
218 for (name, val) in vars.into_iter() {
219 let val = Path::new(val.as_ref()).to_owned();
220 path_resolver.set_var(name, Ok(val.into()));
221 }
222 path_resolver
223 }
224}
225
226impl CfgPath {
227 pub fn new(s: String) -> Self {
229 CfgPath(PathInner::Shell(s))
230 }
231
232 pub fn new_literal<P: Into<PathBuf>>(path: P) -> Self {
234 CfgPath(PathInner::Literal(LiteralPath {
235 literal: path.into(),
236 }))
237 }
238
239 pub fn path(&self, path_resolver: &CfgPathResolver) -> Result<PathBuf, CfgPathError> {
244 match &self.0 {
245 PathInner::Shell(s) => expand(s, path_resolver),
246 PathInner::Literal(LiteralPath { literal }) => Ok(literal.clone()),
247 }
248 }
249
250 pub fn as_unexpanded_str(&self) -> Option<&str> {
257 match &self.0 {
258 PathInner::Shell(s) => Some(s),
259 PathInner::Literal(_) => None,
260 }
261 }
262
263 pub fn as_literal_path(&self) -> Option<&Path> {
268 match &self.0 {
269 PathInner::Shell(_) => None,
270 PathInner::Literal(LiteralPath { literal }) => Some(literal),
271 }
272 }
273}
274
275impl std::fmt::Display for CfgPath {
276 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277 match &self.0 {
278 PathInner::Literal(LiteralPath { literal }) => write!(fmt, "{:?} [exactly]", literal),
279 PathInner::Shell(s) => s.fmt(fmt),
280 }
281 }
282}
283
284#[cfg(feature = "expand-paths")]
288pub fn home() -> Result<&'static Path, CfgPathError> {
289 static HOME_DIR: LazyLock<Option<PathBuf>> =
291 LazyLock::new(|| Some(BaseDirs::new()?.home_dir().to_owned()));
292 HOME_DIR
293 .as_ref()
294 .map(PathBuf::as_path)
295 .ok_or(CfgPathError::NoBaseDirs)
296}
297
298#[cfg(feature = "expand-paths")]
300fn expand(s: &str, path_resolver: &CfgPathResolver) -> Result<PathBuf, CfgPathError> {
301 let path = shellexpand::path::full_with_context(
302 s,
303 || home().ok(),
304 |x| path_resolver.get_var(x).map(Some),
305 );
306 Ok(path.map_err(|e| e.cause)?.into_owned())
307}
308
309#[cfg(not(feature = "expand-paths"))]
311fn expand(input: &str, _: &CfgPathResolver) -> Result<PathBuf, CfgPathError> {
312 if input.starts_with('~') {
314 return Err(CfgPathError::HomeDirInterpolationNotSupported(input.into()));
315 }
316
317 let mut out = String::with_capacity(input.len());
318 let mut s = input;
319 while let Some((lhs, rhs)) = s.split_once('$') {
320 if let Some(rhs) = rhs.strip_prefix('$') {
321 out += lhs;
323 out += "$";
324 s = rhs;
325 } else {
326 return Err(CfgPathError::VariableInterpolationNotSupported(
327 input.into(),
328 ));
329 }
330 }
331 out += s;
332 Ok(out.into())
333}
334
335#[cfg(all(test, feature = "expand-paths"))]
336mod test {
337 #![allow(clippy::unwrap_used)]
338 use super::*;
339
340 #[test]
341 fn expand_no_op() {
342 let r = CfgPathResolver::from_pairs([("FOO", "foo")]);
343
344 let p = CfgPath::new("Hello/world".to_string());
345 assert_eq!(p.to_string(), "Hello/world".to_string());
346 assert_eq!(p.path(&r).unwrap().to_str(), Some("Hello/world"));
347
348 let p = CfgPath::new("/usr/local/foo".to_string());
349 assert_eq!(p.to_string(), "/usr/local/foo".to_string());
350 assert_eq!(p.path(&r).unwrap().to_str(), Some("/usr/local/foo"));
351 }
352
353 #[cfg(not(target_family = "windows"))]
354 #[test]
355 fn expand_home() {
356 let r = CfgPathResolver::from_pairs([("USER_HOME", home().unwrap())]);
357
358 let p = CfgPath::new("~/.arti/config".to_string());
359 assert_eq!(p.to_string(), "~/.arti/config".to_string());
360
361 let expected = dirs::home_dir().unwrap().join(".arti/config");
362 assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
363
364 let p = CfgPath::new("${USER_HOME}/.arti/config".to_string());
365 assert_eq!(p.to_string(), "${USER_HOME}/.arti/config".to_string());
366 assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
367 }
368
369 #[cfg(target_family = "windows")]
370 #[test]
371 fn expand_home() {
372 let r = CfgPathResolver::from_pairs([("USER_HOME", home().unwrap())]);
373
374 let p = CfgPath::new("~\\.arti\\config".to_string());
375 assert_eq!(p.to_string(), "~\\.arti\\config".to_string());
376
377 let expected = dirs::home_dir().unwrap().join(".arti\\config");
378 assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
379
380 let p = CfgPath::new("${USER_HOME}\\.arti\\config".to_string());
381 assert_eq!(p.to_string(), "${USER_HOME}\\.arti\\config".to_string());
382 assert_eq!(p.path(&r).unwrap().to_str(), expected.to_str());
383 }
384
385 #[test]
386 fn expand_bogus() {
387 let r = CfgPathResolver::from_pairs([("FOO", "foo")]);
388
389 let p = CfgPath::new("${ARTI_WOMBAT}/example".to_string());
390 assert_eq!(p.to_string(), "${ARTI_WOMBAT}/example".to_string());
391
392 assert!(matches!(p.path(&r), Err(CfgPathError::UnknownVar(_))));
393 assert_eq!(
394 &p.path(&r).unwrap_err().to_string(),
395 "Unrecognized variable ARTI_WOMBAT in path"
396 );
397 }
398
399 #[test]
400 fn literal() {
401 let r = CfgPathResolver::from_pairs([("ARTI_CACHE", "foo")]);
402
403 let p = CfgPath::new_literal(PathBuf::from("${ARTI_CACHE}/literally"));
404 assert_eq!(
406 p.path(&r).unwrap().to_str().unwrap(),
407 "${ARTI_CACHE}/literally"
408 );
409 assert_eq!(p.to_string(), "\"${ARTI_CACHE}/literally\" [exactly]");
410 }
411
412 #[test]
413 #[cfg(feature = "expand-paths")]
414 fn program_dir() {
415 let current_exe = std::env::current_exe().unwrap();
416 let r = CfgPathResolver::from_pairs([("PROGRAM_DIR", current_exe.parent().unwrap())]);
417
418 let p = CfgPath::new("${PROGRAM_DIR}/foo".to_string());
419
420 let mut this_binary = current_exe;
421 this_binary.pop();
422 this_binary.push("foo");
423 let expanded = p.path(&r).unwrap();
424 assert_eq!(expanded, this_binary);
425 }
426
427 #[test]
428 #[cfg(not(feature = "expand-paths"))]
429 fn rejections() {
430 let r = CfgPathResolver::from_pairs([("PROGRAM_DIR", std::env::current_exe().unwrap())]);
431
432 let chk_err = |s: &str, mke: &dyn Fn(String) -> CfgPathError| {
433 let p = CfgPath::new(s.to_string());
434 assert_eq!(p.path(&r).unwrap_err(), mke(s.to_string()));
435 };
436
437 let chk_ok = |s: &str, exp| {
438 let p = CfgPath::new(s.to_string());
439 assert_eq!(p.path(&r), Ok(PathBuf::from(exp)));
440 };
441
442 chk_err(
443 "some/${PROGRAM_DIR}/foo",
444 &CfgPathError::VariableInterpolationNotSupported,
445 );
446 chk_err("~some", &CfgPathError::HomeDirInterpolationNotSupported);
447
448 chk_ok("some$$foo$$bar", "some$foo$bar");
449 chk_ok("no dollars", "no dollars");
450 }
451}
452
453#[cfg(test)]
454mod test_serde {
455 #![allow(clippy::bool_assert_comparison)]
457 #![allow(clippy::clone_on_copy)]
458 #![allow(clippy::dbg_macro)]
459 #![allow(clippy::mixed_attributes_style)]
460 #![allow(clippy::print_stderr)]
461 #![allow(clippy::print_stdout)]
462 #![allow(clippy::single_char_pattern)]
463 #![allow(clippy::unwrap_used)]
464 #![allow(clippy::unchecked_time_subtraction)]
465 #![allow(clippy::useless_vec)]
466 #![allow(clippy::needless_pass_by_value)]
467 use super::*;
470
471 use std::ffi::OsString;
472 use std::fmt::Debug;
473
474 use derive_builder::Builder;
475 use tor_config::load::TopLevel;
476 use tor_config::{ConfigBuildError, impl_standard_builder};
477
478 #[derive(Serialize, Deserialize, Builder, Eq, PartialEq, Debug)]
479 #[builder(derive(Serialize, Deserialize, Debug))]
480 #[builder(build_fn(error = "ConfigBuildError"))]
481 struct TestConfigFile {
482 p: CfgPath,
483 }
484
485 impl_standard_builder! { TestConfigFile: !Default }
486
487 impl TopLevel for TestConfigFile {
488 type Builder = TestConfigFileBuilder;
489 }
490
491 fn deser_json(json: &str) -> CfgPath {
492 dbg!(json);
493 let TestConfigFile { p } = serde_json::from_str(json).expect("deser json failed");
494 p
495 }
496 fn deser_toml(toml: &str) -> CfgPath {
497 dbg!(toml);
498 let TestConfigFile { p } = toml::from_str(toml).expect("deser toml failed");
499 p
500 }
501 fn deser_toml_cfg(toml: &str) -> CfgPath {
502 dbg!(toml);
503 let mut sources = tor_config::ConfigurationSources::new_empty();
504 sources.push_source(
505 tor_config::ConfigurationSource::from_verbatim(toml.to_string()),
506 tor_config::sources::MustRead::MustRead,
507 );
508 let cfg = sources.load().unwrap();
509
510 dbg!(&cfg);
511 let TestConfigFile { p } = tor_config::load::resolve(cfg).expect("cfg resolution failed");
512 p
513 }
514
515 #[test]
516 fn test_parse() {
517 fn desers(toml: &str, json: &str) -> Vec<CfgPath> {
518 vec![deser_toml(toml), deser_toml_cfg(toml), deser_json(json)]
519 }
520
521 for cp in desers(r#"p = "string""#, r#"{ "p": "string" }"#) {
522 assert_eq!(cp.as_unexpanded_str(), Some("string"));
523 assert_eq!(cp.as_literal_path(), None);
524 }
525
526 for cp in desers(
527 r#"p = { literal = "lit" }"#,
528 r#"{ "p": {"literal": "lit"} }"#,
529 ) {
530 assert_eq!(cp.as_unexpanded_str(), None);
531 assert_eq!(cp.as_literal_path(), Some(&*PathBuf::from("lit")));
532 }
533 }
534
535 fn non_string_path() -> PathBuf {
536 #[cfg(target_family = "unix")]
537 {
538 use std::os::unix::ffi::OsStringExt;
539 return PathBuf::from(OsString::from_vec(vec![0x80_u8]));
540 }
541
542 #[cfg(target_family = "windows")]
543 {
544 use std::os::windows::ffi::OsStringExt;
545 return PathBuf::from(OsString::from_wide(&[0xD800_u16]));
546 }
547
548 #[allow(unreachable_code)]
549 PathBuf::default()
551 }
552
553 fn test_roundtrip_cases<SER, S, DESER, E, F>(ser: SER, deser: DESER)
554 where
555 SER: Fn(&TestConfigFile) -> Result<S, E>,
556 DESER: Fn(&S) -> Result<TestConfigFile, F>,
557 S: Debug,
558 E: Debug,
559 F: Debug,
560 {
561 let case = |easy, p| {
562 let input = TestConfigFile { p };
563 let s = match ser(&input) {
564 Ok(s) => s,
565 Err(e) if easy => panic!("ser failed {:?} e={:?}", &input, &e),
566 Err(_) => return,
567 };
568 dbg!(&input, &s);
569 let output = deser(&s).expect("deser failed");
570 assert_eq!(&input, &output, "s={:?}", &s);
571 };
572
573 case(true, CfgPath::new("string".into()));
574 case(true, CfgPath::new_literal(PathBuf::from("nice path")));
575 case(true, CfgPath::new_literal(PathBuf::from("path with ✓")));
576
577 case(false, CfgPath::new_literal(non_string_path()));
582 }
583
584 #[test]
585 fn roundtrip_json() {
586 test_roundtrip_cases(
587 |input| serde_json::to_string(&input),
588 |json| serde_json::from_str(json),
589 );
590 }
591
592 #[test]
593 fn roundtrip_toml() {
594 test_roundtrip_cases(|input| toml::to_string(&input), |toml| toml::from_str(toml));
595 }
596
597 #[test]
598 fn roundtrip_mpack() {
599 test_roundtrip_cases(
600 |input| rmp_serde::to_vec(&input),
601 |mpack| rmp_serde::from_slice(mpack),
602 );
603 }
604}