From 29a6a6a836e5966ab2a57a5ea97e455a27a11acf Mon Sep 17 00:00:00 2001 From: diogo464 Date: Fri, 9 Jul 2021 02:43:59 -0400 Subject: Made links unique by origin. Inserting a new link with a new link with an with an origin that already exists will replace the old link with that same origin. --- dotup/src/depot.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/dotup/src/depot.rs b/dotup/src/depot.rs index 6f18738..9fb156f 100644 --- a/dotup/src/depot.rs +++ b/dotup/src/depot.rs @@ -1,5 +1,6 @@ use slotmap::SlotMap; use std::{ + collections::HashMap, fs::Metadata, path::{Path, PathBuf}, sync::Arc, @@ -121,6 +122,7 @@ pub struct Depot { shared: Arc, // Maps the origin to the link links: SlotMap, + links_by_origin: HashMap, } impl Depot { @@ -195,6 +197,7 @@ fn depot_create(config: DepotConfig) -> Result { let mut depot = Depot { shared: Arc::new(depot_shared), links: Default::default(), + links_by_origin: Default::default(), }; for archive_link in config.archive.links { @@ -346,10 +349,16 @@ fn depot_uninstall_link(_depot: &Depot, link: &Link, install_base: &Path) -> Res } fn depot_insert_link(depot: &mut Depot, mut link: Link) -> LinkID { - depot.links.insert_with_key(move |k| { + let origin = link.origin().to_path_buf(); + if let Some(link_id) = depot.links_by_origin.remove(&origin) { + depot.links.remove(link_id); + } + let link_id = depot.links.insert_with_key(move |k| { link.id = k; link - }) + }); + depot.links_by_origin.insert(origin, link_id); + link_id } fn depot_links(depot: &Depot) -> impl Iterator { -- cgit