Review follow-ups to M5. - Chat channel names and topics (app/src/channel_sync.rs): a channel with syncDiscord (default on) gets its name and description as its Discord name and topic when saved; registering fills them from Discord; Discord's form of the name is written back; a topic over 1,024 characters is refused before saving; a rate-limited edit is pending and retried once by itself with the latest text; a missing permission fails with the reason and the app keeps the text. Commands discord_channel_info and channel_sync_status. Checked on real Discord with a temporary channel (fill, edit, rate limit, automatic retry). - voice.tts.speed (0.5-2.0) on the model screen, passed to the worker as --melo-speed. MeloTTS's Korean model has one voice, so no voice choice. - Closing the window runs AdminApi::shutdown first: an install in progress is cancelled and waited for, the services stop, a failed run's leftovers (Strata) are cleared (Controller::shutdown). If that fails the window stays open with the reason; closing again quits. - WebView2 data goes in the data folder (cache/webview), or a temp scratch folder before one is chosen, removed at the next start: %APPDATA%\JarvisBot holds only the location file. - Command output that is not UTF-8 (CP949 on a PC without the UTF-8 option) is decoded in the OEM code page instead of ending the reading. Verified on the win-ci VM: closing during the install leaves no process, no --location keeps only location.json under %APPDATA%, the unit tests on Windows 168/168. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
223 lines
9.1 KiB
Rust
223 lines
9.1 KiB
Rust
//! The data folder and the file that remembers where it is (`app/src/admin.spec.md`).
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use serde_json::json;
|
|
|
|
/// The wizard warns below this much free space (desktop-app.md: 200 GB recommended).
|
|
pub const RECOMMENDED_FREE_BYTES: u64 = 200 * 1_000_000_000;
|
|
|
|
/// Folders inside the data folder (desktop-app.md, "데이터 폴더 구조").
|
|
pub const LAYOUT: &[&str] = &[
|
|
"config", "runtime", "models/llm", "models/stt", "models/tts", "models/vad", "memory", "browser",
|
|
"collect", "cache", "logs", "models/hf", "models/nltk",
|
|
];
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct DataDir(PathBuf);
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct DataDirError(pub String);
|
|
|
|
impl std::fmt::Display for DataDirError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.write_str(&self.0)
|
|
}
|
|
}
|
|
impl std::error::Error for DataDirError {}
|
|
|
|
fn io(what: &'static str, p: &Path) -> impl FnOnce(std::io::Error) -> DataDirError {
|
|
let p = p.display().to_string();
|
|
move |e| DataDirError(format!("{what} {p}: {e}"))
|
|
}
|
|
|
|
impl DataDir {
|
|
/// Make the layout inside `path` (new or existing); nothing is ever deleted.
|
|
pub fn create(path: &Path) -> Result<Self, DataDirError> {
|
|
if !path.is_absolute() {
|
|
return Err(DataDirError(format!("the data folder must be an absolute path: {}", path.display())));
|
|
}
|
|
if path.is_file() {
|
|
return Err(DataDirError(format!("the data folder is a file: {}", path.display())));
|
|
}
|
|
for sub in LAYOUT {
|
|
let d = path.join(sub);
|
|
std::fs::create_dir_all(&d).map_err(io("could not create", &d))?;
|
|
}
|
|
Ok(Self(path.to_path_buf()))
|
|
}
|
|
|
|
/// Create the folder and remember it in the location file.
|
|
pub fn choose(path: &Path, location_file: &Path) -> Result<Self, DataDirError> {
|
|
let dir = Self::create(path)?;
|
|
let body = json!({"dataDir": path.to_string_lossy()}).to_string();
|
|
write_atomic(location_file, body.as_bytes()).map_err(io("could not write", location_file))?;
|
|
Ok(dir)
|
|
}
|
|
|
|
/// The folder in use: `JARVIS_DATA_DIR`, else the location file; None before the wizard finished.
|
|
pub fn locate(location_file: &Path) -> Option<Self> {
|
|
Self::locate_with(std::env::var("JARVIS_DATA_DIR").ok(), location_file)
|
|
}
|
|
|
|
/// `locate` with the environment variable's value given.
|
|
pub fn locate_with(env: Option<String>, location_file: &Path) -> Option<Self> {
|
|
if let Some(p) = env.filter(|p| !p.is_empty()) {
|
|
return Some(Self(PathBuf::from(p)));
|
|
}
|
|
let raw = std::fs::read_to_string(location_file).ok()?;
|
|
let v: serde_json::Value = serde_json::from_str(raw.trim_start_matches('\u{feff}')).ok()?;
|
|
let p = PathBuf::from(v.get("dataDir")?.as_str()?);
|
|
p.is_absolute().then_some(Self(p))
|
|
}
|
|
|
|
pub fn path(&self) -> &Path {
|
|
&self.0
|
|
}
|
|
pub fn config(&self) -> PathBuf {
|
|
self.0.join("config")
|
|
}
|
|
pub fn settings_file(&self) -> PathBuf {
|
|
self.config().join("settings.json")
|
|
}
|
|
pub fn secrets_file(&self) -> PathBuf {
|
|
self.config().join("secrets.dat")
|
|
}
|
|
pub fn logs(&self) -> PathBuf {
|
|
self.0.join("logs")
|
|
}
|
|
}
|
|
|
|
/// Where the location file lives on this OS (`%APPDATA%\JarvisBot`, else the XDG config folder).
|
|
pub fn default_location_file() -> Option<PathBuf> {
|
|
if cfg!(windows) {
|
|
let appdata = std::env::var_os("APPDATA")?;
|
|
return Some(PathBuf::from(appdata).join("JarvisBot").join("location.json"));
|
|
}
|
|
let base = std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from)
|
|
.or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))?;
|
|
Some(base.join("jarvis-bot").join("location.json"))
|
|
}
|
|
|
|
/// Where the window's own browser data (WebView2) goes: `cache/webview` in the data folder once
|
|
/// there is one. Before the first-run wizard chose a folder it is a scratch folder in the system
|
|
/// temp folder (`JarvisBot-webview-<process id>`, `true`); WebView2 holds it until the app's process
|
|
/// has ended, so the app removes it on its next start (`remove_scratch_webview`).
|
|
pub fn webview_data_dir(location_file: &Path) -> (PathBuf, bool) {
|
|
match DataDir::locate(location_file) {
|
|
Some(d) => (d.path().join("cache").join("webview"), false),
|
|
None => (std::env::temp_dir().join(format!("{WEBVIEW_SCRATCH}{}", std::process::id())), true),
|
|
}
|
|
}
|
|
|
|
const WEBVIEW_SCRATCH: &str = "JarvisBot-webview-";
|
|
|
|
/// Remove the scratch browser data folders in `temp` (all of them, or only `only`); a folder still in
|
|
/// use stays and goes next time.
|
|
pub fn remove_scratch_webview(temp: &Path, only: Option<&Path>) {
|
|
let Ok(rd) = std::fs::read_dir(temp) else { return };
|
|
for e in rd.flatten() {
|
|
let p = e.path();
|
|
let ours = p.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with(WEBVIEW_SCRATCH));
|
|
if ours && only.is_none_or(|o| o == p) {
|
|
let _ = std::fs::remove_dir_all(&p);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Free bytes on the disk that holds `path` (or its nearest existing parent).
|
|
pub fn free_bytes(path: &Path) -> Result<u64, DataDirError> {
|
|
let mut p = path;
|
|
while !p.exists() {
|
|
p = p.parent().ok_or_else(|| DataDirError(format!("no existing parent for {}", path.display())))?;
|
|
}
|
|
fs4::available_space(p).map_err(io("could not read the free space of", p))
|
|
}
|
|
|
|
/// Write `bytes` to `path` through a temporary file in the same folder and a rename: a crash leaves
|
|
/// the old file or the new one, never half of one.
|
|
pub fn write_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
|
if let Some(parent) = path.parent() {
|
|
std::fs::create_dir_all(parent)?;
|
|
}
|
|
let tmp = path.with_extension(format!("tmp{}", std::process::id()));
|
|
std::fs::write(&tmp, bytes)?;
|
|
std::fs::rename(&tmp, path).inspect_err(|_| {
|
|
let _ = std::fs::remove_file(&tmp);
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub(crate) fn temp_dir(tag: &str) -> PathBuf {
|
|
let d = std::env::temp_dir().join(format!("jarvis-{tag}-{}-{}", std::process::id(), rand::random::<u32>()));
|
|
std::fs::create_dir_all(&d).unwrap();
|
|
d
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn create_makes_the_layout_and_keeps_what_is_there() {
|
|
let root = temp_dir("dd").join("data");
|
|
std::fs::create_dir_all(root.join("memory")).unwrap();
|
|
std::fs::write(root.join("memory").join("keep.db"), b"x").unwrap();
|
|
let d = DataDir::create(&root).unwrap();
|
|
for sub in LAYOUT {
|
|
assert!(root.join(sub).is_dir(), "{sub}");
|
|
}
|
|
assert_eq!(std::fs::read(root.join("memory").join("keep.db")).unwrap(), b"x");
|
|
assert_eq!(d.settings_file(), root.join("config").join("settings.json"));
|
|
}
|
|
|
|
#[test]
|
|
fn the_window_data_goes_in_the_data_folder_or_a_scratch_folder_that_is_removed() {
|
|
let root = temp_dir("wv");
|
|
let loc = root.join("cfg").join("location.json");
|
|
let (dir, scratch) = webview_data_dir(&loc);
|
|
assert!(scratch && dir.starts_with(std::env::temp_dir()), "no folder chosen yet: scratch in temp");
|
|
DataDir::choose(&root.join("data"), &loc).unwrap();
|
|
if std::env::var("JARVIS_DATA_DIR").is_err() {
|
|
assert_eq!(webview_data_dir(&loc), (root.join("data").join("cache").join("webview"), false));
|
|
}
|
|
// Scratch folders of earlier runs are removed; other folders stay.
|
|
let temp = root.join("tmp");
|
|
for n in ["JarvisBot-webview-1", "JarvisBot-webview-2", "other"] {
|
|
std::fs::create_dir_all(temp.join(n).join("EBWebView")).unwrap();
|
|
}
|
|
remove_scratch_webview(&temp, Some(&temp.join("JarvisBot-webview-2")));
|
|
assert!(temp.join("JarvisBot-webview-1").exists() && !temp.join("JarvisBot-webview-2").exists());
|
|
remove_scratch_webview(&temp, None);
|
|
assert!(!temp.join("JarvisBot-webview-1").exists() && temp.join("other").exists());
|
|
}
|
|
|
|
#[test]
|
|
fn a_relative_path_or_a_file_is_refused() {
|
|
assert!(DataDir::create(Path::new("relative/data")).unwrap_err().0.contains("absolute"));
|
|
let f = temp_dir("ddf").join("a-file");
|
|
std::fs::write(&f, b"x").unwrap();
|
|
assert!(DataDir::create(&f).unwrap_err().0.contains("file"));
|
|
}
|
|
|
|
#[test]
|
|
fn choose_remembers_the_folder_and_locate_finds_it() {
|
|
let base = temp_dir("ddl");
|
|
let loc = base.join("cfg").join("location.json");
|
|
assert_eq!(DataDir::locate_with(None, &loc), None, "no wizard yet");
|
|
let data = base.join("데이터");
|
|
DataDir::choose(&data, &loc).unwrap();
|
|
assert_eq!(DataDir::locate_with(None, &loc), Some(DataDir(data)));
|
|
// The environment variable wins (tests, development); an empty one does not count.
|
|
assert_eq!(DataDir::locate_with(Some("/elsewhere".into()), &loc).unwrap().path(), Path::new("/elsewhere"));
|
|
assert!(DataDir::locate_with(Some(String::new()), &loc).is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn free_space_is_read_from_the_nearest_existing_parent() {
|
|
let base = temp_dir("ddfree");
|
|
let n = free_bytes(&base.join("not").join("yet")).unwrap();
|
|
assert!(n > 0);
|
|
}
|
|
}
|