Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
18 changes: 14 additions & 4 deletions apps/rustgis-mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ mod typed;
pub use ios::main as ios_main;

/// The files of `picked` to open, each by its path: all but the parts another of them reads from
/// beside it (a Shapefile's `.dbf`, `.shx`, `.prj` and `.cpg`, a MapInfo file's `.mid`), matched
/// by name whatever the case. A part picked without its main file stays, and is refused by name.
/// beside it (a Shapefile's `.dbf`, `.shx`, `.prj`, `.cpg` and unused `.cst`, a DBF's unused
/// `.cst`, a MapInfo file's `.mid`), matched by name whatever the case. A part picked without
/// its main file stays, and is refused by name.
#[cfg_attr(not(any(target_os = "android", target_os = "ios", test)), expect(dead_code, reason = "only a phone picks files"))]
fn without_sidecars(picked: Vec<std::path::PathBuf>) -> Vec<std::path::PathBuf> {
let lower = |part: Option<&std::ffi::OsStr>| part.map(|p| p.to_string_lossy().to_lowercase()).unwrap_or_default();
Expand All @@ -39,6 +40,13 @@ fn without_sidecars(picked: Vec<std::path::PathBuf>) -> Vec<std::path::PathBuf>
for (path, (stem, extension)) in picked.iter().zip(&named) {
let read_by = match extension.as_str() {
"dbf" | "shx" | "prj" | "cpg" => "shp",
"cst" => {
if has(stem, "shp") {
"shp"
} else {
"dbf"
}
}
"mid" => "mif",
_ => "",
};
Expand Down Expand Up @@ -241,8 +249,10 @@ mod tests {

#[test]
fn parts_picked_with_their_file_are_left_to_it() {
assert_eq!(kept(&["roads.shp", "roads.dbf", "roads.shx", "roads.prj", "roads.cpg"]), ["roads.shp"]);
assert_eq!(kept(&["ROADS.SHP", "Roads.DBF", "parcels.mif", "parcels.MID"]), ["ROADS.SHP", "parcels.mif"]);
assert_eq!(kept(&["roads.shp", "roads.dbf", "roads.shx", "roads.prj", "roads.cpg", "roads.cst"]), ["roads.shp"]);
assert_eq!(kept(&["ROADS.SHP", "Roads.DBF", "roads.CsT", "parcels.mif", "parcels.MID"]), ["ROADS.SHP", "parcels.mif"]);
assert_eq!(kept(&["table.DBF", "table.CST", "notes.cst"]), ["table.DBF", "notes.cst"]);
assert_eq!(kept(&["roads.cst"]), ["roads.cst"]);
}

#[test]
Expand Down
68 changes: 67 additions & 1 deletion apps/rustgis-mobile/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,20 @@ fn read_batch(purpose: &str, mut files: Vec<(String, PathBuf)>, inbox: &Inbox, p
let max = PICK_MAX_BYTES;
let mut read = Vec::new();
let mut too_large = Vec::new();
// Only a Shapefile dialog's matched CST is a presence marker. Other picks keep their bytes.
let dbf_names: std::collections::HashSet<_> = if mark == rustgis_ui_egui::SHAPEFILE_PICK {
files.iter().map(|(name, _)| name.to_ascii_lowercase()).filter(|name| name.ends_with(".dbf")).collect()
} else {
std::collections::HashSet::new()
};
for (name, path) in files {
let shortcut = mark == rustgis_ui_egui::SHORTCUT_PICK || (mark.is_empty() && name.to_ascii_lowercase().ends_with(".rgisshortcuts"));
let lower = name.to_ascii_lowercase();
if lower.strip_suffix(".cst").is_some_and(|stem| dbf_names.contains(&format!("{stem}.dbf"))) {
read.push((name, Vec::new()));
remove_copy(&path);
continue;
}
let shortcut = mark == rustgis_ui_egui::SHORTCUT_PICK || (mark.is_empty() && lower.ends_with(".rgisshortcuts"));
let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0);
if shortcut && size > rustgis_ui_egui::SHORTCUT_FILE_MAX_BYTES as u64 {
say(Problem::ShortcutTooLarge { name });
Expand All @@ -492,6 +504,14 @@ fn read_batch(purpose: &str, mut files: Vec<(String, PathBuf)>, inbox: &Inbox, p
}
remove_copy(&path);
}
let read_dbfs: std::collections::HashSet<_> =
read.iter().map(|(name, _)| name.to_ascii_lowercase()).filter(|name| name.ends_with(".dbf")).collect();
read.retain(|(name, _)| {
name.to_ascii_lowercase().strip_suffix(".cst").is_none_or(|stem| {
let dbf = format!("{stem}.dbf");
!dbf_names.contains(&dbf) || read_dbfs.contains(&dbf)
})
});
if !too_large.is_empty() {
say(Problem::PicturesTooLarge { names: too_large });
}
Expand Down Expand Up @@ -1229,6 +1249,52 @@ mod tests {
assert_eq!(*inbox.lock().unwrap(), vec![(crate::mark("dxf:7"), Vec::<u8>::new())]);
}

/// The dialog needs only a matched CST's name; unrelated payloads still count and arrive whole.
#[test]
fn a_shapefile_pick_keeps_cst_presence_without_reading_its_body() {
let dir = std::env::temp_dir().join(format!("rustgis-mobile-cst-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
// The picker packages these bytes; the dialog reads the DBF from that package later.
let table = b"DBF payload";
let unrelated = b"keep me";
let files = [("table.DBF", &table[..]), ("TABLE.CsT", &b"label"[..]), ("notes.cst", &unrelated[..])]
.into_iter()
.map(|(name, bytes)| {
let path = dir.join(name);
std::fs::write(&path, bytes).unwrap();
(name.to_string(), path)
})
.collect();
let (inbox, problems) = (Inbox::default(), Problems::default());
let mut held = 0;
read_batch("shapefile", files, &inbox, &problems, &mut held);
assert!(problems.lock().unwrap().is_empty());
assert_eq!(held, (table.len() + unrelated.len()) as u64);
let read = inbox.lock().unwrap();
assert_eq!(read.len(), 2);
assert!(
read.iter()
.any(|(name, bytes)| name == &format!("{}notes.cst", rustgis_ui_egui::SHAPEFILE_PICK) && bytes.as_slice() == unrelated)
);
assert!(read.iter().any(|(name, bytes)| name == &format!("{}table.zip", rustgis_ui_egui::SHAPEFILE_PICK) && !bytes.is_empty()));
assert!(!dir.exists());
}

/// The failed DBF's own error is enough: its CST marker is never delivered as another file.
#[test]
fn a_failed_dbf_read_retires_its_phone_cst_marker() {
let dir = std::env::temp_dir().join(format!("rustgis-mobile-cst-failed-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let cst = dir.join("roads.cst");
std::fs::write(&cst, b"label").unwrap();
let (inbox, problems) = (Inbox::default(), Problems::default());
let files = vec![("roads.dbf".into(), dir.join("roads.dbf")), ("roads.cst".into(), cst)];
read_batch("shapefile", files, &inbox, &problems, &mut 0);
assert!(inbox.lock().unwrap().is_empty());
assert!(matches!(problems.lock().unwrap().as_slice(), [Problem::Unread { name, .. }] if name == "roads.dbf"));
assert!(!dir.exists());
}

/// A script picked for Load… over the pane's limit is refused before it is read, naming it; one
/// within it reaches the inbox marked for the pane.
#[test]
Expand Down
35 changes: 35 additions & 0 deletions apps/rustgis-web/src/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@

use rustgis_ui_egui::{GEOLIBRE_EXTS, ITEM_EXTS, PROJECT_EXTS};

/// Names whose CST contents can be replaced by a presence marker during a data pick.
pub(crate) fn cst_markers(names: impl Iterator<Item = String>) -> std::collections::HashSet<String> {
let names: std::collections::HashSet<_> = names.map(|name| name.to_ascii_lowercase()).collect();
names.iter().filter(|name| name.strip_suffix(".cst").is_some_and(|stem| names.contains(&format!("{stem}.dbf")))).cloned().collect()
}

/// Discards synthetic markers whose DBF failed to read; original picker payloads stay intact.
pub(crate) fn discard_failed_cst_markers(read: &mut Vec<(String, Vec<u8>)>, markers: &std::collections::HashSet<String>) {
let usable = cst_markers(read.iter().map(|(name, _)| name.clone()));
read.retain(|(name, _)| {
let lower = name.to_ascii_lowercase();
!markers.contains(&lower) || usable.contains(&lower)
});
}

/// Returns the static extension filter list for a named picker `purpose`, when it uses one.
pub(crate) fn filter_extensions(purpose: &str) -> Option<&'static [&'static str]> {
match purpose {
Expand All @@ -18,6 +33,26 @@ pub(crate) fn filter_extensions(purpose: &str) -> Option<&'static [&'static str]
mod tests {
use super::*;

#[test]
fn failed_dbf_reads_retire_only_their_synthetic_cst_markers() {
let markers = ["roads.cst".to_string()].into_iter().collect();
let mut read = vec![("roads.CST".into(), vec![]), ("empty.cst".into(), vec![]), ("notes.cst".into(), b"keep".to_vec())];
discard_failed_cst_markers(&mut read, &markers);
assert_eq!(read, vec![("empty.cst".into(), vec![]), ("notes.cst".into(), b"keep".to_vec())]);
let mut read = vec![("roads.DBF".into(), b"table".to_vec()), ("roads.CST".into(), vec![])];
discard_failed_cst_markers(&mut read, &markers);
assert_eq!(read.len(), 2);
}

/// Unrelated CST files keep their payload; matched DBF companions need only their names.
#[test]
fn cst_markers_require_a_matching_table() {
let names = ["roads.SHP", "ROADS.dbf", "Roads.CsT", "notes.cst", "lone.shp", "lone.cst", "table.DBF", "table.cst"];
let markers = cst_markers(names.into_iter().map(str::to_string));
assert_eq!(markers, ["roads.cst".to_string(), "table.cst".to_string()].into_iter().collect());
assert!(cst_markers(["notes.cst".to_string()].into_iter()).is_empty());
}

/// The browser picker re-exports `ui-egui`'s canonical project and item extension slices.
#[test]
fn browser_picker_extensions_match_ui_egui_lists() {
Expand Down
14 changes: 13 additions & 1 deletion apps/rustgis-web/src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const DATA_EXTS: &[&str] = &[
"shx",
"prj",
"cpg",
"cst",
"kml",
"kmz",
"gpx",
Expand Down Expand Up @@ -540,9 +541,19 @@ async fn read_together(mut files: Vec<web_sys::File>, mark: &str, inbox: &Inbox,
);
notes.lock().unwrap_or_else(|e| e.into_inner()).push((text, true));
}
let markers = if mark.is_empty() || mark == rustgis_ui_egui::SHAPEFILE_PICK {
crate::picker::cst_markers(files.iter().map(|file| file.name()))
} else {
Default::default()
};
let mut too_large = Vec::new();
for file in files {
let name = file.name();
// Data sidecars travel as presence markers; script and tool pickers keep their own reads.
if markers.contains(&name.to_ascii_lowercase()) {
read.push((name, Vec::new()));
continue;
}
let shortcut = mark == rustgis_ui_egui::SHORTCUT_PICK || (mark.is_empty() && name.to_ascii_lowercase().ends_with(".rgisshortcuts"));
if shortcut && file.size() > rustgis_ui_egui::SHORTCUT_FILE_MAX_BYTES as f64 {
notes
Expand All @@ -568,6 +579,7 @@ async fn read_together(mut files: Vec<web_sys::File>, mark: &str, inbox: &Inbox,
read.push((name, bytes));
}
}
crate::picker::discard_failed_cst_markers(&mut read, &markers);
if let Some(first) = too_large.first() {
let why = rustgis_ui_egui::rustgis_assistant::attach::too_large(first.clone());
let text = if too_large.len() == 1 {
Expand Down Expand Up @@ -1401,7 +1413,7 @@ fn services(inbox: Inbox, font_inbox: Inbox, notes: Notes, ctx: egui::Context, e
} else if purpose == "script" {
rfd::AsyncFileDialog::new().add_filter(rustgis_ui_egui::tl!("Script"), &["txt"])
} else if purpose == "shapefile" {
rfd::AsyncFileDialog::new().add_filter("Shapefile", &["shp", "dbf", "shx", "prj", "cpg", "zip"])
rfd::AsyncFileDialog::new().add_filter("Shapefile", &["shp", "dbf", "shx", "prj", "cpg", "cst", "zip"])
} else if let Some(exts) = crate::picker::filter_extensions(purpose) {
let label = if purpose == "project" {
rustgis_ui_egui::tl!("Projects and JSON data (RustGIS, QGIS, GeoLibre)")
Expand Down
Loading
Loading