Share one exporter, and flush the file before saying it is written (#686)

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Gregory Schier
2026-09-15 14:56:23 -07:00
committed by GitHub
co-authored by Claude Opus 5
parent caeebebc75
commit f827aae46d
8 changed files with 73 additions and 24 deletions
@@ -3,7 +3,7 @@ use crate::context::CliContext;
use crate::utils::workspace::resolve_workspace_id;
use std::fs;
use std::io::ErrorKind;
use yaak::export::{self, ExportDataParams};
use yaak_models::export::{self, ExportDataParams};
use yaak::import;
use yaak_models::util::{
BatchUpsertResult, ImportDestination, ImportOrigin, ImportPlanAction, ImportPlanItem,
@@ -127,6 +127,46 @@ fn format_skipped(items: &[ImportPlanItem]) -> Option<String> {
if parts.is_empty() { None } else { Some(parts.join(", ")) }
}
/// Write a file so that afterwards it is either the old one or the whole new one.
///
/// Writing in place would truncate the previous export before the new one existed, so an
/// interrupted run loses both. Instead the bytes go to a neighbouring file, are flushed, and
/// take the target's name in one step. The directory is flushed too: `sync_all` on a file
/// promises its contents, not that the name it just gained will survive a power cut.
fn write_durably(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> {
use std::io::Write;
let dir = match path.parent() {
Some(p) if !p.as_os_str().is_empty() => p,
_ => std::path::Path::new("."),
};
// Alongside the target, because a rename is only atomic within one filesystem.
let mut name = path.file_name().unwrap_or_default().to_os_string();
name.push(format!(".tmp{}", std::process::id()));
let tmp = dir.join(name);
let write = |tmp: &std::path::Path| -> std::io::Result<()> {
let mut file = std::fs::File::create(tmp)?;
file.write_all(bytes)?;
file.sync_all()
};
if let Err(e) = write(&tmp) {
let _ = std::fs::remove_file(&tmp);
return Err(e);
}
if let Err(e) = std::fs::rename(&tmp, path) {
let _ = std::fs::remove_file(&tmp);
return Err(e);
}
// Only Unix lets a directory be opened for this. Elsewhere the rename is as much as
// the platform offers, and the file itself is already flushed.
#[cfg(unix)]
std::fs::File::open(dir)?.sync_all()?;
Ok(())
}
fn export(ctx: &CliContext, args: ExportArgs) -> CommandResult<usize> {
let workspace_ids = resolve_export_workspace_ids(ctx, args.workspace_ids, args.all)?;
let workspace_id_refs: Vec<&str> = workspace_ids.iter().map(String::as_str).collect();
@@ -137,7 +177,10 @@ fn export(ctx: &CliContext, args: ExportArgs) -> CommandResult<usize> {
include_private_environments: args.include_private_environments,
})
.map_err(|e| format!("Failed to export data: {e}"))?;
std::fs::write(&args.file, document)
// Flushed before reporting success: an export is a backup, and a backup that the
// command called done while it was still only in the page cache is the one case
// where saying nothing went wrong is worst.
write_durably(&args.file, document.as_bytes())
.map_err(|e| format!("Failed to write {}: {e}", args.file.display()))?;
Ok(workspace_ids.len())