use super::*;
pub(super) fn resolve(config: &BootstrapConfig) -> Option<PathBuf> {
if config.connection_file_source != ConnectionFileSource::TempDirFallback {
return None;
}
#[cfg(test)]
let base = Ok::<_, io::Error>(config.profile_override.clone()?);
#[cfg(not(test))]
let base = subc_os::profile_dir();
match base {
Ok(base) => Some(
base.join("AppData")
.join("Local")
.join("cortexkit")
.join("run")
.join(CONNECTION_FILE_NAME),
),
Err(error) => {
warn_unavailable(&config.connection_file_path, None, &error);
None
}
}
}
pub(super) fn warn_unavailable(primary: &Path, copy: Option<&Path>, error: &dyn fmt::Display) {
warn!(
primary = %primary.display(),
copy = ?copy,
%error,
"Windows fixed connection-file copy unavailable; using the primary only"
);
}
async fn claim(path: &Path) -> Result<StartLock, BootstrapError> {
let parent = path.parent().expect("fixed connection path has a parent");
if !parent.exists() {
subc_os::windows_acl::create_private_dir(parent).map_err(|source| {
BootstrapError::StartLockCreate {
path: parent.to_path_buf(),
source,
}
})?;
}
StartLock::acquire(path).await
}
pub(super) async fn reclaim(primary: &Path, copy: Option<PathBuf>) -> Option<PathBuf> {
let path = copy?;
let result = async {
let _lock = claim(&path).await?;
if stable_peer(&path).await? {
return Err(io::Error::other("fixed copy belongs to a live daemon").into());
}
remove_stale_connection_file_if_present(&path)?;
Ok::<_, CopyError>(())
}
.await;
match result {
Ok(()) => Some(path),
Err(error) => {
warn_unavailable(primary, Some(&path), &error);
None
}
}
}
pub(super) async fn claim_for_publication(
primary: &Path,
copy: Option<&Path>,
info: &ConnectionInfo,
) -> Option<(PathBuf, StartLock)> {
let path = copy?;
let result = async {
let lock = claim(path).await?;
let ours = match connection_file::read(path) {
Ok(existing) => existing.daemon_id == info.daemon_id,
Err(error) if is_absent_or_stale_connection_file(&error) => false,
Err(error) => return Err(CopyError::Read(error)),
};
if !ours && stable_peer(path).await? {
return Err(io::Error::other("fixed copy belongs to a live foreign daemon").into());
}
Ok(lock)
}
.await;
match result {
Ok(lock) => Some((path.to_path_buf(), lock)),
Err(error) => {
warn_unavailable(primary, Some(path), &error);
None
}
}
}
#[derive(Debug)]
enum CopyError {
Bootstrap(BootstrapError),
Read(ConnectionFileError),
Io(io::Error),
}
impl From<BootstrapError> for CopyError {
fn from(error: BootstrapError) -> Self {
Self::Bootstrap(error)
}
}
impl From<io::Error> for CopyError {
fn from(error: io::Error) -> Self {
Self::Io(error)
}
}
impl fmt::Display for CopyError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bootstrap(error) => error.fmt(formatter),
Self::Read(error) => error.fmt(formatter),
Self::Io(error) => error.fmt(formatter),
}
}
}
#[cfg(test)]
mod tests;