Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(filesystem): keep a data folder on a network drive absolute

James Dam4f35e7df4365
diff --git a/src-tauri/src/core/filesystem/helpers.rs b/src-tauri/src/core/filesystem/helpers.rs
index 2409879e8..dc5e36c8e 100644
--- a/src-tauri/src/core/filesystem/helpers.rs
+++ b/src-tauri/src/core/filesystem/helpers.rs
@@ -1,4 +1,6 @@
use crate::core::app::commands::get_jan_data_folder_path;
+#[cfg(windows)]
+use jan_utils::strip_verbatim_prefix;
use jan_utils::{normalize_file_path, normalize_path};
use std::path::{Path, PathBuf};
use tauri::Runtime;
@@ -69,12 +71,15 @@ fn canonicalize_for_scope(path: &Path) -> PathBuf {
normalize_path(&canonical_parent.join(suffix))
}
+/// The form both sides of a containment check are compared in: `canonicalize`
+/// returns `\\?\C:\x` and `\\?\UNC\server\share\x` on Windows, which become
+/// `C:\x` and `\\server\share\x` (a data folder on a network drive stays
+/// absolute instead of turning into a relative `UNC\server\share\x`).
fn normalize_scope_path(path: PathBuf) -> PathBuf {
#[cfg(windows)]
{
- let path_str = path.to_string_lossy();
- if let Some(stripped) = path_str.strip_prefix(r"\\?\") {
- return normalize_path(Path::new(stripped));
+ if let Some(plain) = path.to_str().map(strip_verbatim_prefix) {
+ return normalize_path(Path::new(&plain));
}
}
@@ -275,4 +280,45 @@ mod tests {
assert!(resolved.starts_with(&root));
assert!(resolved.ends_with("file.txt"));
}
+
+ #[test]
+ fn verbatim_data_folder_paths_compare_in_plain_form() {
+ // Windows-style strings, checked on every host.
+ for (data, child, plain_data) in [
+ (
+ r"\\?\UNC\server\share\Jan",
+ r"\\?\UNC\server\share\Jan\threads\t.json",
+ r"\\server\share\Jan",
+ ),
+ (
+ r"\\?\C:\Users\u\Jan",
+ r"\\?\C:\Users\u\Jan\threads\t.json",
+ r"C:\Users\u\Jan",
+ ),
+ ] {
+ let data = jan_utils::strip_verbatim_prefix(data);
+ let child = jan_utils::strip_verbatim_prefix(child);
+ assert_eq!(data, plain_data);
+ assert!(child.starts_with(&format!("{data}\\")), "{child}");
+ }
+ // A plain path typed by the user lands in the same form.
+ assert_eq!(
+ jan_utils::strip_verbatim_prefix(r"\\server\share\Jan"),
+ r"\\server\share\Jan"
+ );
+ }
+
+ #[cfg(windows)]
+ #[test]
+ fn unc_data_folder_stays_absolute() {
+ let plain = normalize_scope_path(PathBuf::from(r"\\?\UNC\server\share\Jan"));
+ assert_eq!(plain, PathBuf::from(r"\\server\share\Jan"));
+ assert!(plain.is_absolute());
+ let child = normalize_scope_path(PathBuf::from(r"\\?\UNC\server\share\Jan\threads"));
+ assert!(child.starts_with(&plain));
+ assert_eq!(
+ normalize_path(Path::new(r"\\?\UNC\server\share\Jan\x")),
+ PathBuf::from(r"\\server\share\Jan\x")
+ );
+ }
}
diff --git a/src-tauri/utils/src/path.rs b/src-tauri/utils/src/path.rs
index 174b827d9..9d00f0a23 100644
--- a/src-tauri/utils/src/path.rs
+++ b/src-tauri/utils/src/path.rs
@@ -27,7 +27,17 @@ pub fn normalize_path(path: &Path) -> PathBuf {
pb.push(format!("{}:", disk as char));
pb
}
- Prefix::Verbatim(_) | Prefix::VerbatimUNC(_, _) => {
+ Prefix::VerbatimUNC(server, share) => {
+ components.next(); // skip this prefix
+ // Re-add it as a plain UNC prefix (\\server\share), not a
+ // relative `UNC\server\share`.
+ PathBuf::from(format!(
+ r"\\{}\{}",
+ server.to_string_lossy(),
+ share.to_string_lossy()
+ ))
+ }
+ Prefix::Verbatim(_) => {
components.next(); // skip this prefix
PathBuf::new()
}