mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-01 19:03:56 +01:00
Stack t2: #2794: classify the commands main added since; hold block library paths to the roots
every_command_is_classified fails on current main: BLOCKLIBRARY, BLOCKLIBIMPORT, BLOCKLIBRARIES, BLOCKSPALETTE, -DIMSTYLE, DIMSTYLE.OVERRIDES, DIMSTYLE.SAVEAS, DIMOVERRIDE.CLEAR and PROPERTIES.DESCRIBE landed after the branch. The two that read library drawings check the path before listing a folder, looking at it, or answering from the drawing cache; BLOCKLIBRARIES keeps its list in the settings folder and is refused under the roots, as DRAWINGRECOVERY is.
This commit is contained in:
1 parent
fc992081a7
commit
776e871003
2 files changed
+38
-1
No files matched your search
@@ -70,6 +70,8 @@ fn stamp(path: &str) -> Option<(u64, Option<SystemTime>)> {
|
||||
/// A library drawing, read once and kept while the file is as it was: placing a block must not
|
||||
/// parse the whole file again.
|
||||
fn cached_drawing(path: &str) -> Result<Arc<Drawing>> {
|
||||
// Before the cache too: a drawing read earlier must not be handed out past the roots.
|
||||
crate::file_access::check_read(path).map_err(|e| bad("blocklibrary", e))?;
|
||||
// The reader sniffs the bytes, so a DXF named `part.bin` would load: the contract is DWG/DXF by name.
|
||||
if !is_drawing(Path::new(path)) {
|
||||
return Err(bad("blocklibrary", format!("{path}: expected a DWG or DXF file")));
|
||||
@@ -186,6 +188,9 @@ pub fn read_library_file(path: &str) -> Result<Arc<Drawing>> {
|
||||
/// What BLOCKLIBRARY answers for `path`. It needs no session, so the palette reads big drawings
|
||||
/// and slow folders on a worker thread.
|
||||
pub fn list_library(path: &str) -> Result<Value> {
|
||||
// Before anything looks at the path: a folder's listing, or whether it is one, says what is
|
||||
// outside the automation roots.
|
||||
crate::file_access::check_read(path).map_err(|e| bad("blocklibrary", e))?;
|
||||
if is_folder(path) {
|
||||
let (files, truncated) = folder_files(Path::new(path))?;
|
||||
let list: Vec<Value> = files.iter().map(|(name, file)| json!({ "name": name, "file": file })).collect();
|
||||
@@ -356,6 +361,26 @@ fn run_libraries(_s: &mut Session, p: &Value) -> Result<Value> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Under the automation roots a library folder or file outside them is refused before it is
|
||||
/// listed or read.
|
||||
#[test]
|
||||
fn the_automation_roots_hold_library_paths() {
|
||||
let base = std::env::temp_dir().join(format!("cadcraft-library-roots-{}", std::process::id()));
|
||||
let (inside, outside) = (base.join("in"), base.join("outside"));
|
||||
std::fs::create_dir_all(&inside).unwrap();
|
||||
std::fs::create_dir_all(&outside).unwrap();
|
||||
std::fs::write(outside.join("parts.dxf"), b"0\nEOF\n").unwrap();
|
||||
let roots = crate::file_access::AutomationRoots::new(Some(&inside), None).unwrap();
|
||||
crate::file_access::confine(roots.as_ref(), || {
|
||||
let folder = list_library(&outside.to_string_lossy()).map(|v| v.to_string());
|
||||
assert!(folder.as_ref().is_err_and(|e| e.to_string().contains("automation")), "{folder:?}");
|
||||
let file = list_library(&outside.join("parts.dxf").to_string_lossy()).map(|v| v.to_string());
|
||||
assert!(file.as_ref().is_err_and(|e| e.to_string().contains("automation")), "{file:?}");
|
||||
assert!(list_library(&inside.to_string_lossy()).is_ok());
|
||||
});
|
||||
let _ = std::fs::remove_dir_all(&base);
|
||||
}
|
||||
|
||||
/// A list that could not be written stays the list of this session, whatever the file says.
|
||||
#[test]
|
||||
fn a_failed_write_keeps_the_list_in_memory() {
|
||||
|
||||
@@ -14,6 +14,8 @@ pub const FILES: &[&str] = &[
|
||||
"aliasedit",
|
||||
"appload",
|
||||
"attach",
|
||||
"blocklibimport",
|
||||
"blocklibrary",
|
||||
"audit",
|
||||
"compare",
|
||||
"compareexport",
|
||||
@@ -61,10 +63,19 @@ pub const FILES: &[&str] = &[
|
||||
];
|
||||
|
||||
/// Commands that can't be held to the roots, and why.
|
||||
pub const REFUSED: &[(&str, &str)] = &[("drawingrecovery", "it works on the autosave folder (SAVEFILEPATH), which is the server's own")];
|
||||
pub const REFUSED: &[(&str, &str)] = &[
|
||||
("drawingrecovery", "it works on the autosave folder (SAVEFILEPATH), which is the server's own"),
|
||||
("blocklibraries", "it keeps its list of libraries in the settings folder, which is the server's own"),
|
||||
];
|
||||
|
||||
/// Commands that touch no file.
|
||||
pub const NO_FILES: &[&str] = &[
|
||||
"blockspalette",
|
||||
"dimoverride.clear",
|
||||
"-dimstyle",
|
||||
"dimstyle.overrides",
|
||||
"dimstyle.saveas",
|
||||
"properties.describe",
|
||||
"close",
|
||||
"closeall",
|
||||
"document.switch",
|
||||
@@ -532,6 +543,7 @@ mod tests {
|
||||
confine(roots.as_ref(), || {
|
||||
assert!(refusal("made.up").unwrap().contains("not classified"));
|
||||
assert!(refusal("drawingrecovery").unwrap().contains("autosave"));
|
||||
assert!(refusal("blocklibraries").unwrap().contains("settings folder"));
|
||||
assert_eq!(refusal("open"), None);
|
||||
assert_eq!(refusal("line"), None);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user