Loading crates/tor-dirmgr/Cargo.toml +1 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ dirfilter = [] experimental-api = [] [dependencies] fs-mistrust = { path = "../fs-mistrust", version = "0.1.0" } tor-basic-utils = { path = "../tor-basic-utils", version = "0.2.0" } retry-error = { path = "../retry-error", version = "0.1.0" } tor-checkable = { path = "../tor-checkable", version = "0.2.0" } Loading crates/tor-dirmgr/src/config.rs +11 −4 Original line number Diff line number Diff line Loading @@ -189,6 +189,9 @@ pub struct DirMgrConfig { /// Cannot be changed on a running Arti client. pub cache_path: PathBuf, /// Rules for whether to trust the the permissions on the cache_path. pub cache_trust: fs_mistrust::Mistrust, /// Configuration information about the network. pub network: NetworkConfig, Loading Loading @@ -228,10 +231,13 @@ impl DirMgrConfig { /// Note that each time this is called, a new store object will be /// created: you probably only want to call this once. pub(crate) fn open_store(&self, readonly: bool) -> Result<DynStore> { Ok(Box::new(crate::storage::SqliteStore::from_path( Ok(Box::new( crate::storage::SqliteStore::from_path_and_mistrust( &self.cache_path, &self.cache_trust, readonly, )?)) )?, )) } /// Return a slice of the configured authorities Loading @@ -251,6 +257,7 @@ impl DirMgrConfig { pub(crate) fn update_from_config(&self, new_config: &DirMgrConfig) -> DirMgrConfig { DirMgrConfig { cache_path: self.cache_path.clone(), cache_trust: self.cache_trust.clone(), network: NetworkConfig { fallback_caches: new_config.network.fallback_caches.clone(), authorities: self.network.authorities.clone(), Loading crates/tor-dirmgr/src/err.rs +11 −0 Original line number Diff line number Diff line Loading @@ -83,6 +83,9 @@ pub enum Error { /// An attempt was made to bootstrap a `DirMgr` created in offline mode. #[error("cannot bootstrap offline DirMgr")] OfflineMode, /// A problem with file permissions on our cache directory. #[error("Bad permissions in cache directory")] CachePermissions(#[from] fs_mistrust::Error), /// Unable to spawn task #[error("unable to spawn {spawning}")] Loading Loading @@ -154,6 +157,7 @@ impl Error { // These errors cannot come from a directory cache. Error::NoDownloadSupport | Error::CacheCorruption(_) | Error::CachePermissions(_) | Error::SqliteError(_) | Error::UnrecognizedSchema | Error::BadNetworkConfig(_) Loading Loading @@ -209,6 +213,13 @@ impl HasKind for Error { E::Unwanted(_) => EK::TorProtocolViolation, E::NoDownloadSupport => EK::NotImplemented, E::CacheCorruption(_) => EK::CacheCorrupted, E::CachePermissions(e) => { if e.is_bad_permission() { EK::FsPermissions } else { EK::CacheAccessFailed } } E::SqliteError(e) => sqlite_error_kind(e), E::UnrecognizedSchema => EK::CacheCorrupted, E::BadNetworkConfig(_) => EK::InvalidConfig, Loading crates/tor-dirmgr/src/state.rs +6 −1 Original line number Diff line number Diff line Loading @@ -995,7 +995,12 @@ mod test { fn temp_store() -> (TempDir, Mutex<DynStore>) { let tempdir = TempDir::new().unwrap(); let store = crate::storage::SqliteStore::from_path(tempdir.path(), false).unwrap(); let store = crate::storage::SqliteStore::from_path_and_mistrust( tempdir.path(), fs_mistrust::Mistrust::new().dangerously_trust_everyone(), false, ) .unwrap(); (tempdir, Mutex::new(Box::new(store))) } Loading crates/tor-dirmgr/src/storage.rs +8 −14 Original line number Diff line number Diff line Loading @@ -17,8 +17,9 @@ use crate::docmeta::{AuthCertMeta, ConsensusMeta}; use crate::{Error, Result}; use std::cell::RefCell; use std::collections::HashMap; use std::fs::File; use std::str::Utf8Error; use std::time::SystemTime; use std::{path::Path, str::Utf8Error}; use time::Duration; pub(crate) mod sqlite; Loading Loading @@ -124,18 +125,15 @@ impl InputString { } } } /// Construct a new InputString from a file on disk, trying to /// memory-map the file if possible. pub(crate) fn load<P: AsRef<Path>>(path: P) -> Result<Self> { let f = std::fs::File::open(path)?; /// DOCDOC pub(crate) fn load(file: File) -> Result<Self> { #[cfg(feature = "mmap")] { let mapping = unsafe { // I'd rather have a safe option, but that's not possible // with mmap, since other processes could in theory replace // the contents of the file while we're using it. memmap2::Mmap::map(&f) memmap2::Mmap::map(&file) }; if let Ok(bytes) = mapping { return Ok(InputString::MappedBytes { Loading @@ -145,7 +143,7 @@ impl InputString { } } use std::io::{BufReader, Read}; let mut f = BufReader::new(f); let mut f = BufReader::new(file); let mut result = String::new(); f.read_to_string(&mut result)?; Ok(InputString::Utf8(result)) Loading Loading @@ -308,13 +306,9 @@ mod test { fn files() { let td = tempdir().unwrap(); let absent = td.path().join("absent"); let s = InputString::load(&absent); assert!(s.is_err()); let goodstr = td.path().join("goodstr"); std::fs::write(&goodstr, "This is a reasonable file.\n").unwrap(); let s = InputString::load(&goodstr); let s = InputString::load(File::open(goodstr).unwrap()); let s = s.unwrap(); assert_eq!(s.as_str().unwrap(), "This is a reasonable file.\n"); assert_eq!(s.as_str().unwrap(), "This is a reasonable file.\n"); Loading @@ -322,7 +316,7 @@ mod test { let badutf8 = td.path().join("badutf8"); std::fs::write(&badutf8, b"Not good \xff UTF-8.\n").unwrap(); let s = InputString::load(&badutf8); let s = InputString::load(File::open(badutf8).unwrap()); assert!(s.is_err() || s.unwrap().as_str().is_err()); } Loading Loading
crates/tor-dirmgr/Cargo.toml +1 −0 Original line number Diff line number Diff line Loading @@ -26,6 +26,7 @@ dirfilter = [] experimental-api = [] [dependencies] fs-mistrust = { path = "../fs-mistrust", version = "0.1.0" } tor-basic-utils = { path = "../tor-basic-utils", version = "0.2.0" } retry-error = { path = "../retry-error", version = "0.1.0" } tor-checkable = { path = "../tor-checkable", version = "0.2.0" } Loading
crates/tor-dirmgr/src/config.rs +11 −4 Original line number Diff line number Diff line Loading @@ -189,6 +189,9 @@ pub struct DirMgrConfig { /// Cannot be changed on a running Arti client. pub cache_path: PathBuf, /// Rules for whether to trust the the permissions on the cache_path. pub cache_trust: fs_mistrust::Mistrust, /// Configuration information about the network. pub network: NetworkConfig, Loading Loading @@ -228,10 +231,13 @@ impl DirMgrConfig { /// Note that each time this is called, a new store object will be /// created: you probably only want to call this once. pub(crate) fn open_store(&self, readonly: bool) -> Result<DynStore> { Ok(Box::new(crate::storage::SqliteStore::from_path( Ok(Box::new( crate::storage::SqliteStore::from_path_and_mistrust( &self.cache_path, &self.cache_trust, readonly, )?)) )?, )) } /// Return a slice of the configured authorities Loading @@ -251,6 +257,7 @@ impl DirMgrConfig { pub(crate) fn update_from_config(&self, new_config: &DirMgrConfig) -> DirMgrConfig { DirMgrConfig { cache_path: self.cache_path.clone(), cache_trust: self.cache_trust.clone(), network: NetworkConfig { fallback_caches: new_config.network.fallback_caches.clone(), authorities: self.network.authorities.clone(), Loading
crates/tor-dirmgr/src/err.rs +11 −0 Original line number Diff line number Diff line Loading @@ -83,6 +83,9 @@ pub enum Error { /// An attempt was made to bootstrap a `DirMgr` created in offline mode. #[error("cannot bootstrap offline DirMgr")] OfflineMode, /// A problem with file permissions on our cache directory. #[error("Bad permissions in cache directory")] CachePermissions(#[from] fs_mistrust::Error), /// Unable to spawn task #[error("unable to spawn {spawning}")] Loading Loading @@ -154,6 +157,7 @@ impl Error { // These errors cannot come from a directory cache. Error::NoDownloadSupport | Error::CacheCorruption(_) | Error::CachePermissions(_) | Error::SqliteError(_) | Error::UnrecognizedSchema | Error::BadNetworkConfig(_) Loading Loading @@ -209,6 +213,13 @@ impl HasKind for Error { E::Unwanted(_) => EK::TorProtocolViolation, E::NoDownloadSupport => EK::NotImplemented, E::CacheCorruption(_) => EK::CacheCorrupted, E::CachePermissions(e) => { if e.is_bad_permission() { EK::FsPermissions } else { EK::CacheAccessFailed } } E::SqliteError(e) => sqlite_error_kind(e), E::UnrecognizedSchema => EK::CacheCorrupted, E::BadNetworkConfig(_) => EK::InvalidConfig, Loading
crates/tor-dirmgr/src/state.rs +6 −1 Original line number Diff line number Diff line Loading @@ -995,7 +995,12 @@ mod test { fn temp_store() -> (TempDir, Mutex<DynStore>) { let tempdir = TempDir::new().unwrap(); let store = crate::storage::SqliteStore::from_path(tempdir.path(), false).unwrap(); let store = crate::storage::SqliteStore::from_path_and_mistrust( tempdir.path(), fs_mistrust::Mistrust::new().dangerously_trust_everyone(), false, ) .unwrap(); (tempdir, Mutex::new(Box::new(store))) } Loading
crates/tor-dirmgr/src/storage.rs +8 −14 Original line number Diff line number Diff line Loading @@ -17,8 +17,9 @@ use crate::docmeta::{AuthCertMeta, ConsensusMeta}; use crate::{Error, Result}; use std::cell::RefCell; use std::collections::HashMap; use std::fs::File; use std::str::Utf8Error; use std::time::SystemTime; use std::{path::Path, str::Utf8Error}; use time::Duration; pub(crate) mod sqlite; Loading Loading @@ -124,18 +125,15 @@ impl InputString { } } } /// Construct a new InputString from a file on disk, trying to /// memory-map the file if possible. pub(crate) fn load<P: AsRef<Path>>(path: P) -> Result<Self> { let f = std::fs::File::open(path)?; /// DOCDOC pub(crate) fn load(file: File) -> Result<Self> { #[cfg(feature = "mmap")] { let mapping = unsafe { // I'd rather have a safe option, but that's not possible // with mmap, since other processes could in theory replace // the contents of the file while we're using it. memmap2::Mmap::map(&f) memmap2::Mmap::map(&file) }; if let Ok(bytes) = mapping { return Ok(InputString::MappedBytes { Loading @@ -145,7 +143,7 @@ impl InputString { } } use std::io::{BufReader, Read}; let mut f = BufReader::new(f); let mut f = BufReader::new(file); let mut result = String::new(); f.read_to_string(&mut result)?; Ok(InputString::Utf8(result)) Loading Loading @@ -308,13 +306,9 @@ mod test { fn files() { let td = tempdir().unwrap(); let absent = td.path().join("absent"); let s = InputString::load(&absent); assert!(s.is_err()); let goodstr = td.path().join("goodstr"); std::fs::write(&goodstr, "This is a reasonable file.\n").unwrap(); let s = InputString::load(&goodstr); let s = InputString::load(File::open(goodstr).unwrap()); let s = s.unwrap(); assert_eq!(s.as_str().unwrap(), "This is a reasonable file.\n"); assert_eq!(s.as_str().unwrap(), "This is a reasonable file.\n"); Loading @@ -322,7 +316,7 @@ mod test { let badutf8 = td.path().join("badutf8"); std::fs::write(&badutf8, b"Not good \xff UTF-8.\n").unwrap(); let s = InputString::load(&badutf8); let s = InputString::load(File::open(badutf8).unwrap()); assert!(s.is_err() || s.unwrap().as_str().is_err()); } Loading