Add DirMgr::load_once.

This function loads a current bootstrapped directory from disk, if
it can.
This commit is contained in:
Nick Mathewson 2021-03-29 12:23:47 -04:00
parent e226a57869
commit 83ebd56f64
2 changed files with 29 additions and 0 deletions

View File

@ -30,4 +30,8 @@ pub enum Error {
/// We couldn't configure the network.
#[error("bad network configuration")]
BadNetworkConfig(&'static str),
/// User requested an operation that required a usable
/// bootstrapped directory, but we didn't have one.
#[error("directory not present or not up-to-date")]
DirectoryNotPresent,
}

View File

@ -55,6 +55,10 @@ pub struct DirMgr {
/// Handle to our sqlite cache.
// XXXX I'd like to use an rwlock, but that's not feasible, since
// rusqlite::Connection isn't Sync.
// TODO: Does this have to be a futures::Mutex? I would rather have
// a rule that we never hold the guard for this mutex across an async
// suspend point. But that will be hard to enforce until the
// `must_not_suspend` lint is in stable.
store: Mutex<SqliteStore>,
/// Our latest sufficiently bootstrapped directory, if we have one.
///
@ -67,6 +71,27 @@ pub struct DirMgr {
}
impl DirMgr {
/// Try to load the directory from disk, without launching any
/// kind of update process.
///
/// This function will give an error if the result is not
/// up-to-date, or not fully downloaded.
///
/// In general, you shouldn't use this function in a long-running
/// program; it's only suitable for command-line or batch tools.
// TODO: I wish this function didn't have to be async.
pub async fn load_once(config: NetDirConfig) -> Result<Arc<NetDir>> {
let dirmgr = DirMgr::from_config(config, None)?;
// TODO: add some way to return a directory that isn't up-to-date
let _success = dirmgr.load_directory().await?;
dirmgr
.opt_netdir()
.await
.ok_or_else(|| Error::DirectoryNotPresent.into())
}
/// Return a new directory manager from a given configuration,
/// bootstrapping from the network as necessary.
///