Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modda-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ path-absolutize = "3.1.1"
percent-encoding = "2.3.2"
regex = "1.12.3"
reqwest = { version = "0.13.2", features = ["stream", "json"] }
same-file = "1.0.6"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
serde_path_to_error = "0.1.20"
Expand Down
1 change: 1 addition & 0 deletions modda-lib/resources/test/file_lookup/conflicts/MOD1
1 change: 1 addition & 0 deletions modda-lib/resources/test/file_lookup/conflicts/MOD2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is NOT a symlink to mod2
1 change: 1 addition & 0 deletions modda-lib/resources/test/file_lookup/conflicts/mod1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MOD1 is a symlink to mod1
1 change: 1 addition & 0 deletions modda-lib/resources/test/file_lookup/conflicts/mod2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MOD2 is NOT a symlink to mod2
2 changes: 1 addition & 1 deletion modda-lib/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Downloader {


// Indicatif setup
let pb = match (total_size) {
let pb = match total_size {
Some(total_size) => Self::progress_with_size(total_size)?,
None => match &download_opts.size_hint{
None =>{
Expand Down
54 changes: 49 additions & 5 deletions modda-lib/src/utils/insensitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::io::ErrorKind;
use std::path::{Path, PathBuf};

use anyhow::{Result, bail};
use log::debug;
use itertools::Itertools;
use log::{debug, info};
use same_file::Handle;

/// Looks for a file matching the argument, allowing case-insensitive matches.
///
Expand All @@ -20,10 +22,15 @@ pub fn find_insensitive<P>(base: P, searched: &str) -> Result<Option<PathBuf>>
},
[ref name] => Ok(Some(PathBuf::from(name))),
_ => {
let msg = format!("More than one candidate ({count}) for lookup of {searched:?} in {base:?}",
count = candidates.len());
debug!("{msg}");
bail!(msg)
match handle_multiple_matches(&candidates, searched) {
Ok(result) => Ok(result),
Err(_) => {
let msg = format!("More than one candidate ({count}) for lookup of {searched:?} in {base:?}",
count = candidates.len());
debug!("{msg}");
bail!(msg)
}
}
},
}
}
Expand Down Expand Up @@ -110,6 +117,24 @@ pub fn find_all_insensitive<P>(base: P, searched: &str) -> Result<Vec<PathBuf>>
Ok(result)
}

fn handle_multiple_matches(candidates: &[PathBuf], searched: &str) -> Result<Option<PathBuf>> {
let handles: Result<Vec<Handle>, std::io::Error> = candidates.iter()
.map(|path| Handle::from_path(path))
.collect();
let handles = match handles {
Err(err) => bail!("Couldn't open all matching candidates\n {:?}", err),
Ok(handles) => handles,
};
if handles.iter().all_equal() {
let chosen = itertools::sorted(candidates.iter()).next().cloned();
info!("multiple ({count}) candidates for {searched} but all are the same file - choose {chosen:?}",
count = candidates.len());
Ok(chosen)
} else {
bail!("Multiple matches for {searched}, not the same file")
}
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;
Expand Down Expand Up @@ -275,4 +300,23 @@ mod tests {
].iter().collect::<HashSet<_>>(),
);
}

#[cfg(target_family = "unix")]
#[test]
fn multiple_candidates_symlink_same_file() {
let _ = env_logger::builder().is_test(true).filter_level(log::LevelFilter::Debug).try_init();
let base = Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test/file_lookup/conflicts");
assert_eq!(
find_insensitive(&base, "mod1").unwrap(),
Some(PathBuf::from(&base).join("MOD1")),
);
}

#[cfg(target_family = "unix")]
#[test]
fn multiple_candidates_symlink_different_file() {
let _ = env_logger::builder().is_test(true).filter_level(log::LevelFilter::Debug).try_init();
let base = Path::new(env!("CARGO_MANIFEST_DIR")).join("resources/test/file_lookup/conflicts");
assert!(find_insensitive(&base, "mod2").is_err());
}
}
Loading