Release RuneStone 0.1.7 with plugin documents and shared menus
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use std::fs;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
@@ -511,14 +511,43 @@ fn list_entries(root: &Path, rel: &str) -> WsResult<Vec<TreeEntry>> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn workspace_read(window: tauri::Window, state: State<WorkspaceState>, rel: String) -> WsResult<String> {
|
||||
pub fn workspace_read(window: tauri::Window, state: State<WorkspaceState>, rel: String, missing_ok: Option<bool>) -> WsResult<Option<String>> {
|
||||
let root = current_root(&state, window.label())?;
|
||||
let path = resolve_existing(&root, &rel)?;
|
||||
let path = if missing_ok.unwrap_or(false) { resolve_for_write(&root, &rel)? } else { resolve_existing(&root, &rel)? };
|
||||
if path.is_dir() {
|
||||
return Err(err("path is a folder"));
|
||||
}
|
||||
let bytes = fs::read(&path)?;
|
||||
String::from_utf8(bytes).map_err(|_| err("file is not valid UTF-8"))
|
||||
read_optional_text(&path, missing_ok.unwrap_or(false))
|
||||
}
|
||||
|
||||
fn read_optional_text(path: &Path, missing_ok: bool) -> WsResult<Option<String>> {
|
||||
match fs::read(path) {
|
||||
Ok(bytes) => String::from_utf8(bytes).map(Some).map_err(|_| err("file is not valid UTF-8")),
|
||||
Err(error) if missing_ok && error.kind() == std::io::ErrorKind::NotFound => Ok(None),
|
||||
Err(error) => Err(error.into()),
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_BINARY_BYTES: u64 = 64 * 1024 * 1024;
|
||||
|
||||
fn read_workspace_bytes(root: &Path, rel: &str) -> WsResult<Vec<u8>> {
|
||||
let path = resolve_existing(root, rel)?;
|
||||
let file = fs::File::open(path)?;
|
||||
let metadata = file.metadata()?;
|
||||
if !metadata.is_file() { return Err(err("The path must be a file.")); }
|
||||
if metadata.len() > MAX_BINARY_BYTES { return Err(err("The file exceeds the 64 MiB limit.")); }
|
||||
let mut bytes = Vec::new();
|
||||
file.take(MAX_BINARY_BYTES + 1).read_to_end(&mut bytes)?;
|
||||
if bytes.len() as u64 > MAX_BINARY_BYTES { return Err(err("The file exceeds the 64 MiB limit.")); }
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn workspace_read_bytes(window: tauri::Window, state: State<'_, WorkspaceState>, rel: String) -> WsResult<tauri::ipc::Response> {
|
||||
let root = current_root(&state, window.label())?;
|
||||
let bytes = tauri::async_runtime::spawn_blocking(move || read_workspace_bytes(&root, &rel))
|
||||
.await.map_err(|e| err(e.to_string()))??;
|
||||
Ok(tauri::ipc::Response::new(bytes))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
@@ -590,7 +619,7 @@ fn is_internal_name(name: &str) -> bool {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn workspace_create(window: tauri::Window, state: State<WorkspaceState>, rel: String, kind: String) -> WsResult<()> {
|
||||
pub fn workspace_create(window: tauri::Window, state: State<WorkspaceState>, rel: String, kind: String, contents: Option<String>) -> WsResult<()> {
|
||||
let root = current_root(&state, window.label())?;
|
||||
let path = resolve_for_write(&root, &rel)?;
|
||||
if path.exists() {
|
||||
@@ -598,6 +627,8 @@ pub fn workspace_create(window: tauri::Window, state: State<WorkspaceState>, rel
|
||||
}
|
||||
if kind == "folder" {
|
||||
fs::create_dir_all(&path)?;
|
||||
} else if let Some(contents) = contents {
|
||||
create_text_if_absent(&path, &contents)?;
|
||||
} else {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
@@ -610,6 +641,24 @@ pub fn workspace_create(window: tauri::Window, state: State<WorkspaceState>, rel
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Publish a complete file without replacing a destination created by another writer.
|
||||
fn create_text_if_absent(path: &Path, contents: &str) -> WsResult<()> {
|
||||
static NEXT_CREATE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
let parent = path.parent().ok_or_else(|| err("file has no parent"))?;
|
||||
fs::create_dir_all(parent)?;
|
||||
let temp = parent.join(format!(".runestone-save-create-{}-{}", std::process::id(), NEXT_CREATE.fetch_add(1, std::sync::atomic::Ordering::Relaxed)));
|
||||
let mut file = fs::OpenOptions::new().write(true).create_new(true).open(&temp)?;
|
||||
let result = (|| -> WsResult<()> {
|
||||
file.write_all(contents.as_bytes())?;
|
||||
file.sync_all()?;
|
||||
drop(file);
|
||||
fs::hard_link(&temp, path)?;
|
||||
Ok(())
|
||||
})();
|
||||
let _ = fs::remove_file(&temp);
|
||||
result
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn workspace_rename(window: tauri::Window, state: State<WorkspaceState>, from: String, to: String) -> WsResult<()> {
|
||||
let root = current_root(&state, window.label())?;
|
||||
@@ -887,6 +936,42 @@ mod tests {
|
||||
use super::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[test]
|
||||
fn binary_reads_preserve_bytes_and_enforce_workspace_and_size_limits() {
|
||||
let fixture = std::env::temp_dir().join(format!("rs-binary-{}", std::process::id()));
|
||||
let root = fixture.join("workspace");
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
let root = dunce::canonicalize(root).unwrap();
|
||||
let bytes = b"%PDF-1.7\n\xff\x00\xfe";
|
||||
fs::write(root.join("test.pdf"), bytes).unwrap();
|
||||
fs::write(fixture.join("outside.pdf"), bytes).unwrap();
|
||||
assert_eq!(read_workspace_bytes(&root, "test.pdf").unwrap(), bytes);
|
||||
assert!(read_workspace_bytes(&root, "../outside.pdf").is_err());
|
||||
assert!(read_workspace_bytes(&root, &fixture.join("outside.pdf").to_string_lossy()).is_err());
|
||||
assert!(read_workspace_bytes(&root, "").is_err());
|
||||
assert!(read_workspace_bytes(&root, "missing.pdf").is_err());
|
||||
fs::File::create(root.join("large.pdf")).unwrap().set_len(MAX_BINARY_BYTES + 1).unwrap();
|
||||
assert!(read_workspace_bytes(&root, "large.pdf").unwrap_err().to_string().contains("64 MiB"));
|
||||
fs::remove_dir_all(fixture).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plugin_files_publish_complete_text_without_overwriting() {
|
||||
let root = std::env::temp_dir().join(format!("rs-plugin-create-{}", std::process::id()));
|
||||
fs::create_dir_all(&root).unwrap();
|
||||
let path = root.join("note.md");
|
||||
assert_eq!(read_optional_text(&path, true).unwrap(), None);
|
||||
assert!(read_optional_text(&path, false).is_err());
|
||||
create_text_if_absent(&path, "complete note\n").unwrap();
|
||||
assert_eq!(read_optional_text(&path, true).unwrap().as_deref(), Some("complete note\n"));
|
||||
assert!(create_text_if_absent(&path, "replacement").is_err());
|
||||
assert_eq!(fs::read_to_string(&path).unwrap(), "complete note\n");
|
||||
assert_eq!(fs::read_dir(&root).unwrap().count(), 1);
|
||||
assert!(read_optional_text(&root, true).is_err());
|
||||
fs::remove_file(path).unwrap();
|
||||
fs::remove_dir(root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore_files_do_not_hide_workspace_entries_or_search_results() {
|
||||
let fixture = std::env::temp_dir().join(format!("rs-ignore-rules-{}", std::process::id()));
|
||||
|
||||
Reference in New Issue
Block a user