pub mod error; use std::collections::HashSet; use std::str::FromStr; use localtime::LocalTime; use radicle::crypto::PublicKey; use radicle::prelude::RepoId; use radicle::storage::refs::RefsAt; use radicle::storage::{ReadStorage as _, RefUpdate, RemoteRepository, WriteRepository as _}; use radicle::{git, node, Storage}; use radicle_fetch::{Allowed, BlockList, FetchLimit}; use super::channels::ChannelsFlush; #[derive(Debug, Default)] pub struct FetchResult { /// The set of updates references. pub updated: Vec, /// The set of remote namespaces that were updated. pub namespaces: HashSet, } pub enum Handle { Clone { handle: radicle_fetch::Handle, tmp: tempfile::TempDir, }, Pull { handle: radicle_fetch::Handle, notifications: node::notifications::StoreWriter, }, } impl Handle { pub fn new( rid: RepoId, local: PublicKey, storage: &Storage, follow: Allowed, blocked: BlockList, channels: ChannelsFlush, notifications: node::notifications::StoreWriter, ) -> Result { let exists = storage.contains(&rid)?; if exists { let repo = storage.repository(rid)?; let handle = radicle_fetch::Handle::new(local, repo, follow, blocked, channels)?; Ok(Handle::Pull { handle, notifications, }) } else { let (repo, tmp) = storage.lock_repository(rid)?; let handle = radicle_fetch::Handle::new(local, repo, follow, blocked, channels)?; Ok(Handle::Clone { handle, tmp }) } } pub fn fetch( self, rid: RepoId, storage: &Storage, limit: FetchLimit, remote: PublicKey, refs_at: Option>, ) -> Result { let (result, notifs) = match self { Self::Clone { mut handle, tmp } => { log::debug!(target: "worker", "{} cloning from {remote}", handle.local()); let result = radicle_fetch::clone(&mut handle, limit, remote)?; mv(tmp, storage, &rid)?; (result, None) } Self::Pull { mut handle, notifications, } => { log::debug!(target: "worker", "{} pulling from {remote}", handle.local()); let result = radicle_fetch::pull(&mut handle, limit, remote, refs_at)?; (result, Some(notifications)) } }; for rejected in result.rejected() { log::warn!(target: "worker", "Rejected update for {}", rejected.refname()) } for warn in result.warnings() { log::warn!(target: "worker", "Validation error: {}", warn); } match result { radicle_fetch::FetchResult::Failed { failures, .. } => { for fail in failures.iter() { log::error!(target: "worker", "Validation error: {}", fail); } Err(error::Fetch::Validation) } radicle_fetch::FetchResult::Success { applied, remotes, .. } => { // N.b. We do not go through handle for this since the cloning handle // points to a repository that is temporary and gets moved by [`mv`]. let repo = storage.repository(rid)?; repo.set_identity_head()?; repo.set_head()?; // Notifications are only posted for pulls, not clones. if let Some(mut store) = notifs { // Only create notifications for repos that we have // contributed to in some way, otherwise our inbox will // be flooded by all the repos we are seeding. if repo.remote(&storage.info().key).is_ok() { notify(&rid, &applied, &mut store)?; } } Ok(FetchResult { updated: applied.updated, namespaces: remotes.into_iter().collect(), }) } } } } /// In the case of cloning, we have performed the fetch into a /// temporary directory -- ensuring that no concurrent operations /// see an empty repository. /// /// At the end of the clone, we perform a rename of the temporary /// directory to the storage repository. /// /// # Errors /// - Will fail if `storage` contains `rid` already. fn mv(tmp: tempfile::TempDir, storage: &Storage, rid: &RepoId) -> Result<(), error::Fetch> { use std::io::{Error, ErrorKind}; let from = tmp.path(); let to = storage.path_of(rid); if !to.exists() { std::fs::rename(from, to)?; } else { log::warn!(target: "worker", "Refusing to move cloned repository {rid} already exists"); return Err(Error::new( ErrorKind::AlreadyExists, format!("repository already exists {:?}", to), ) .into()); } Ok(()) } // Post notifications for the given refs. fn notify( rid: &RepoId, refs: &radicle_fetch::git::refs::Applied<'static>, store: &mut node::notifications::StoreWriter, ) -> Result<(), error::Fetch> { let now = LocalTime::now(); for update in refs.updated.iter() { if let Some(r) = update.name().to_namespaced() { let r = r.strip_namespace(); if r == *git::refs::storage::SIGREFS_BRANCH { // Don't notify about signed refs. continue; } if let Some(rest) = r.strip_prefix(git::refname!("refs/heads/patches")) { if radicle::cob::ObjectId::from_str(rest.as_str()).is_ok() { // Don't notify about patch branches, since we already get // notifications about patch updates. continue; } } } if let RefUpdate::Skipped { .. } = update { // Don't notify about skipped refs. } else if let Err(e) = store.insert(&rid, update, now) { log::error!( target: "worker", "Failed to update notification store for {rid}: {e}" ); } } Ok(()) }