fix(jdbc): keep local plugin installation fully offline

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-08-20 21:39:00 +08:00
1 parent be9f8e72e4
commit c030835acf
11 files changed
+89 -9

No files matched your search

+63 -5
View File
@@ -374,7 +374,9 @@ pub async fn install_jdbc_plugin_from_file(plugins_root: &Path, file_path: &str)
})
.await
.map_err(|err| err.to_string())??;
jdbc_plugin_status_from_dir(&status_dir).await
// Local install must stay fully offline: build status from the local
// manifest only, do not check for a newer release over the network.
jdbc_plugin_status_from_dir_local(&status_dir)
}
pub fn uninstall_jdbc_plugin(plugins_root: &Path) -> Result<JdbcPluginStatus, String> {
@@ -390,14 +392,20 @@ pub fn uninstall_jdbc_plugin(plugins_root: &Path) -> Result<JdbcPluginStatus, St
std::fs::remove_file(path).map_err(|err| err.to_string())?;
}
}
// synchronous version: check local manifest only, no network call
jdbc_plugin_status_from_dir_local(&plugin_dir)
}
/// Builds plugin status from the local manifest only, no network call.
/// Used by install/uninstall paths that must stay fully offline (uninstall,
/// and installing a plugin package the user already has on disk).
fn jdbc_plugin_status_from_dir_local(plugin_dir: &Path) -> Result<JdbcPluginStatus, String> {
let manifest_path = plugin_dir.join("manifest.json");
let manifest = match std::fs::read_to_string(&manifest_path) {
Ok(raw) => Some(serde_json::from_str::<PluginManifest>(&raw).map_err(|err| err.to_string())?),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
Err(err) => return Err(err.to_string()),
};
Ok(build_plugin_status(&manifest, None, &plugin_dir))
Ok(build_plugin_status(&manifest, None, plugin_dir))
}
// ---- System Fonts ----
@@ -451,13 +459,25 @@ fn jdbc_maven_resolver_executable(plugin_dir: &Path) -> PathBuf {
}
async fn jdbc_plugin_status_from_dir(plugin_dir: &Path) -> Result<JdbcPluginStatus, String> {
jdbc_plugin_status_from_dir_after(plugin_dir, latest_jdbc_plugin()).await
}
/// Waits on `latest` before reading the manifest, so a slow/offline network
/// check can never race ahead of a concurrent local install and report a
/// stale pre-install snapshot back to the caller. `latest` is a parameter
/// (rather than calling `latest_jdbc_plugin()` directly) so tests can
/// simulate a delayed network response without a real network call.
async fn jdbc_plugin_status_from_dir_after(
plugin_dir: &Path,
latest: impl std::future::Future<Output = Option<JdbcPluginLatest>>,
) -> Result<JdbcPluginStatus, String> {
let latest = latest.await;
let manifest_path = plugin_dir.join("manifest.json");
let manifest = match std::fs::read_to_string(&manifest_path) {
Ok(raw) => Some(serde_json::from_str::<PluginManifest>(&raw).map_err(|err| err.to_string())?),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
Err(err) => return Err(err.to_string()),
};
let latest = latest_jdbc_plugin().await;
Ok(build_plugin_status(&manifest, latest.as_ref(), plugin_dir))
}
@@ -553,7 +573,10 @@ fn install_jdbc_plugin_zip(bytes: &[u8], plugin_dir: &Path) -> Result<(), String
if !temp_dir.join("manifest.json").exists() {
let _ = std::fs::remove_dir_all(&temp_dir);
return Err("Downloaded JDBC plugin package is missing manifest.json".to_string());
return Err(format!(
"This ZIP is not a valid DBX JDBC plugin package (missing manifest.json). \
The correct package is available at: {JDBC_PLUGIN_DOWNLOAD_URL}"
));
}
let manifest_path = temp_dir.join("manifest.json");
let manifest = std::fs::read_to_string(&manifest_path)
@@ -1447,4 +1470,39 @@ mod tests {
let _ = std::fs::remove_dir_all(root);
}
#[tokio::test]
async fn delayed_network_status_check_does_not_revert_a_concurrent_local_install() {
let root = std::env::temp_dir().join(format!("dbx-jdbc-status-race-test-{}", uuid::Uuid::new_v4()));
let plugin_dir = root.join("jdbc");
std::fs::create_dir_all(&plugin_dir).unwrap();
// Mirrors DriverStoreDialog's mount-time status check: it starts
// before the local install and only finishes its (simulated)
// network wait after the local install has already written the
// new manifest to disk.
let status_plugin_dir = plugin_dir.clone();
let status_task = tokio::spawn(async move {
jdbc_plugin_status_from_dir_after(&status_plugin_dir, async {
tokio::time::sleep(std::time::Duration::from_millis(80)).await;
None
})
.await
});
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
// Local install completes fully offline while the status check
// above is still waiting on its simulated network round-trip.
std::fs::write(
plugin_dir.join("manifest.json"),
r#"{"id":"jdbc","name":"DBX JDBC Plugin","version":"0.1.30","protocol_version":1,"executable":"bin/dbx-jdbc-plugin","drivers":[]}"#,
)
.unwrap();
let status = status_task.await.unwrap().unwrap();
assert!(status.installed, "stale pre-install snapshot must not overwrite the completed local install");
assert_eq!(status.version.as_deref(), Some("0.1.30"));
let _ = std::fs::remove_dir_all(root);
}
}