From e5f83cd9a7d8b79ea068ed84c74bb95482a7e647 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Wed, 28 Feb 2024 13:21:24 -0800 Subject: [PATCH] Fix logic for relocatable directory trees The existing logic did not work because: - Path::new("/foo/bar").ends_with("/bar") does not return true. - PathBuf::shrink_to() only (potentially) reallocates the backing storage, and won't have an effect on the stored value. --- src/bin/fish.rs | 62 ++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/src/bin/fish.rs b/src/bin/fish.rs index a1ebbaa5f..bfaf72e7d 100644 --- a/src/bin/fish.rs +++ b/src/bin/fish.rs @@ -174,51 +174,35 @@ fn determine_config_directory_paths(argv0: impl AsRef) -> ConfigPaths { if !done { // The next check is that we are in a reloctable directory tree - let installed_suffix = Path::new("/bin/fish"); - let just_a_fish = Path::new("/fish"); - let suffix = if exec_path.ends_with(installed_suffix) { - Some(installed_suffix) - } else if exec_path.ends_with(just_a_fish) { + if exec_path.ends_with("bin/fish") { + let base_path = exec_path.parent().unwrap().parent().unwrap(); + paths = ConfigPaths { + data: base_path.join("share/fish"), + sysconf: base_path.join("etc/fish"), + doc: base_path.join("share/doc/fish"), + bin: base_path.join("bin"), + } + } else if exec_path.ends_with("fish") { FLOG!( config, "'fish' not in a 'bin/', trying paths relative to source tree" ); - Some(just_a_fish) - } else { - None - }; - - if let Some(suffix) = suffix { - let seems_installed = suffix == installed_suffix; - - let mut base_path = exec_path; - base_path.shrink_to(base_path.as_os_str().len() - suffix.as_os_str().len()); - let base_path = base_path; - - paths = if seems_installed { - ConfigPaths { - data: base_path.join("share/fish"), - sysconf: base_path.join("etc/fish"), - doc: base_path.join("share/doc/fish"), - bin: base_path.join("bin"), - } - } else { - ConfigPaths { - data: base_path.join("share"), - sysconf: base_path.join("etc"), - doc: base_path.join("user_doc/html"), - bin: base_path, - } - }; - - if paths.data.exists() && paths.sysconf.exists() { - // The docs dir may not exist; in that case fall back to the compiled in path. - if !paths.doc.exists() { - paths.doc = PathBuf::from(DOC_DIR); - } - done = true; + let base_path = exec_path.parent().unwrap(); + paths = ConfigPaths { + data: base_path.join("share"), + sysconf: base_path.join("etc"), + doc: base_path.join("user_doc/html"), + bin: base_path.to_path_buf(), } } + + if paths.data.exists() && paths.sysconf.exists() { + // The docs dir may not exist; in that case fall back to the compiled in path. + if !paths.doc.exists() { + paths.doc = PathBuf::from(DOC_DIR); + } + done = true; + } } }