Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(plugin-installer): keep atomic writes and rollbacks lossless

Jinpy2851da891e15
diff --git a/crates/dbx-plugin-runtime/src/plugins/installer.rs b/crates/dbx-plugin-runtime/src/plugins/installer.rs
index abcce1af24..1cf98a53cc 100644
--- a/crates/dbx-plugin-runtime/src/plugins/installer.rs
+++ b/crates/dbx-plugin-runtime/src/plugins/installer.rs
@@ -1065,7 +1065,11 @@ fn migrate_legacy_container(container_dir: &Path) -> Result<(), String> {
let migration = (|| {
std::fs::create_dir_all(&versions_dir).map_err(|error| error.to_string())?;
std::fs::create_dir_all(&activations_dir).map_err(|error| error.to_string())?;
- std::fs::rename(&temporary, &version_dir).map_err(|error| error.to_string())?;
+ // Write the activation record before moving the payload into place:
+ // the rollback below wipes container_dir, so the container must only
+ // ever hold empty scaffolding while a step can still fail. The old
+ // order moved the payload first — a failed activation record then
+ // rolled back by deleting the only copy of the legacy plugin.
write_activation_record(
container_dir,
&PluginActivationRecord {
@@ -1076,7 +1080,8 @@ fn migrate_legacy_container(container_dir: &Path) -> Result<(), String> {
activated_at: Utc::now().to_rfc3339(),
provenance: None,
},
- )
+ )?;
+ rename_with_transient_lock_retry(&temporary, &version_dir).map_err(|error| error.to_string())
})();
if let Err(error) = migration {
let _ = std::fs::remove_dir_all(container_dir);
@@ -1111,10 +1116,11 @@ fn write_json_atomically<T: Serialize>(path: &Path, value: &T) -> Result<(), Str
file.write_all(&serde_json::to_vec_pretty(value).map_err(|error| error.to_string())?)
.map_err(|error| error.to_string())?;
file.sync_all().map_err(|error| error.to_string())?;
- if path.exists() {
- std::fs::remove_file(path).map_err(|error| error.to_string())?;
- }
- std::fs::rename(&temporary, path).map_err(|error| error.to_string())?;
+ // std::fs::rename atomically replaces an existing destination on POSIX
+ // and Windows alike. The previous remove-then-rename opened a window
+ // where a crash between the two calls lost the document (trust keys,
+ // repository metadata) entirely — exactly what "atomically" must not do.
+ rename_with_transient_lock_retry(&temporary, path).map_err(|error| error.to_string())?;
sync_directory(parent)
}
@@ -1135,7 +1141,7 @@ fn open_install_lock(root_dir: &Path) -> Result<File, String> {
const TRANSIENT_LOCK_RETRY_DELAYS: [Duration; 3] =
[Duration::from_millis(200), Duration::from_millis(400), Duration::from_millis(800)];
-fn rename_with_transient_lock_retry(src: &Path, dst: &Path) -> std::io::Result<()> {
+pub(crate) fn rename_with_transient_lock_retry(src: &Path, dst: &Path) -> std::io::Result<()> {
retry_transient_lock(&TRANSIENT_LOCK_RETRY_DELAYS, || std::fs::rename(src, dst))
}
@@ -1289,9 +1295,9 @@ mod tests {
use super::{
is_activation_record_file, read_install_identity, retry_transient_lock, sha256_hex,
- validate_package_expectation, PluginInstallPolicy, PluginPackageExpectation, PluginPackageInstaller,
- PluginSignatureStatus, PluginTrustStore, ACTIVATIONS_DIR, INSTALL_LOCK_FILE, PLUGIN_CHECKSUMS_FILE,
- PLUGIN_SIGNATURE_FILE, PLUGIN_TRASH_DIR, VERSIONS_DIR,
+ validate_package_expectation, write_json_atomically, PluginInstallPolicy, PluginPackageExpectation,
+ PluginPackageInstaller, PluginSignatureStatus, PluginTrustStore, ACTIVATIONS_DIR, INSTALL_LOCK_FILE,
+ PLUGIN_CHECKSUMS_FILE, PLUGIN_SIGNATURE_FILE, PLUGIN_TRASH_DIR, VERSIONS_DIR,
};
use crate::plugins::{PluginManifest, PluginRegistry};
@@ -1544,6 +1550,29 @@ mod tests {
assert!(container.join(VERSIONS_DIR).join(legacy_storage_version).join("manifest.json").is_file());
}
+ #[test]
+ fn atomic_json_writes_replace_in_place_without_temp_litter() {
+ let root = tempfile::tempdir().unwrap();
+ let path = root.path().join("nested").join("doc.json");
+
+ #[derive(serde::Serialize)]
+ struct Doc {
+ value: u32,
+ }
+
+ write_json_atomically(&path, &Doc { value: 1 }).unwrap();
+ write_json_atomically(&path, &Doc { value: 2 }).unwrap();
+
+ let saved: serde_json::Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
+ assert_eq!(saved["value"], 2);
+ let leftovers: Vec<String> = std::fs::read_dir(path.parent().unwrap())
+ .unwrap()
+ .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
+ .filter(|name| name.ends_with(".tmp"))
+ .collect();
+ assert!(leftovers.is_empty(), "temp files leaked: {leftovers:?}");
+ }
+
#[test]
fn strict_policy_requires_and_verifies_trusted_signature() {
let root = tempfile::tempdir().unwrap();
diff --git a/crates/dbx-plugin-runtime/src/plugins/marketplace.rs b/crates/dbx-plugin-runtime/src/plugins/marketplace.rs
index 60d497f19c..fdf8aabe59 100644
--- a/crates/dbx-plugin-runtime/src/plugins/marketplace.rs
+++ b/crates/dbx-plugin-runtime/src/plugins/marketplace.rs
@@ -821,10 +821,10 @@ fn write_json_atomically<T: Serialize>(path: &Path, value: &T) -> Result<(), Str
file.write_all(&serde_json::to_vec_pretty(value).map_err(|error| error.to_string())?)
.map_err(|error| error.to_string())?;
file.sync_all().map_err(|error| error.to_string())?;
- if path.exists() {
- std::fs::remove_file(path).map_err(|error| error.to_string())?;
- }
- std::fs::rename(&temporary, path).map_err(|error| error.to_string())?;
+ // std::fs::rename atomically replaces an existing destination on POSIX
+ // and Windows alike; remove-then-rename could lose the repository
+ // document to a crash between the two calls.
+ crate::plugins::installer::rename_with_transient_lock_retry(&temporary, path).map_err(|error| error.to_string())?;
Ok(())
}