fix: 完善虚拟U盘创建错误提示 #275

This commit is contained in:
mofeng-git
2026-07-18 11:50:26 +08:00
parent eaa114f40c
commit 701b141347
6 changed files with 85 additions and 8 deletions

View File

@@ -11,8 +11,6 @@ clap = { version = "4", features = ["derive"] }
# Error handling
thiserror = "2"
[dev-dependencies]
tempfile = "3"
[profile.release]

View File

@@ -28,8 +28,15 @@ impl VentoyImage {
path.display()
);
// Create sparse file
let mut file = File::create(path)?;
// Build beside the destination so a failed create cannot leave a partial image.
let parent = path
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let mut temp = tempfile::Builder::new()
.prefix(".ventoy-")
.tempfile_in(parent)?;
let mut file = temp.as_file_mut();
file.set_len(size)?;
// Write boot code
@@ -64,6 +71,8 @@ impl VentoyImage {
format_exfat(&mut file, layout.data_offset(), layout.data_size(), label)?;
file.flush()?;
temp.persist(path)
.map_err(|error| VentoyError::Io(error.error))?;
println!("[INFO] Ventoy IMG created successfully!");
@@ -274,3 +283,20 @@ impl VentoyImage {
&self.path
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn failed_create_does_not_publish_partial_image() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("ventoy.img");
let error = VentoyImage::create(&path, "64M", "TEST").err().unwrap();
assert!(matches!(error, VentoyError::ResourceNotFound(_)));
assert!(!path.exists());
assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 0);
}
}