fix(export): enforce batch-atomic OBJ/GLB/raster publication and parity tests (#722)

* fix(export): enforce batch-atomic OBJ/GLB/raster publication and parity tests

* style(mcmat): use as_chunks::<2> in test

* test(mesh_export): use valid non-black PNG in MTL slot link test

* fix(formats): reserve batch targets and roll back partial renames in write_batch_atomic

* fix(formats): preserve target during backup and report incomplete rollback

* fix(formats): address FredApps & CodeRabbit review on write_batch_atomic

* fix(formats): harden batch rollback, backup mode, and special-file guards

* fix(formats): validate regular file handle in rollback ownership check
This commit is contained in:
Dylan M. Taylor
2026-11-20 20:12:30 -05:00
committed by GitHub
parent b0bc66c7cf
commit 89252a0ebe
7 changed files with 586 additions and 36 deletions
+50 -8
View File
@@ -729,15 +729,16 @@ fn write_materialx(s: &Session, sets: &[usize], opts: &ExportOptions, dir: &std:
fn export_mesh(s: &mut Session, p: &Value) -> Result<Value> {
let path = native::path_param(p, "path", "file.exportMesh")?;
let out = s.mesh_export(path, strp(p, "format"))?;
let write = |path: &str, bytes: &[u8]| std::fs::write(path, bytes).map_err(|e| bad("file.exportMesh", format!("cannot write {path}: {e}")));
let dir = std::path::Path::new(&out.path).parent().unwrap_or(std::path::Path::new(""));
let mut textures = Vec::new();
for (name, bytes) in &out.textures {
let file = dir.join(name).display().to_string();
write(&file, bytes)?;
textures.push(file);
}
write(&out.path, &out.bytes)?;
let texture_paths: Vec<std::path::PathBuf> = out.textures.iter().map(|(name, _)| dir.join(name)).collect();
let main_path = std::path::PathBuf::from(&out.path);
let entries = texture_paths
.iter()
.zip(&out.textures)
.map(|(file, (_, bytes))| (file.as_path(), bytes.as_slice()))
.chain(std::iter::once((main_path.as_path(), out.bytes.as_slice())));
texturecraft_formats::write_batch_atomic(entries).map_err(|e| bad("file.exportMesh", format!("cannot write {}: {e}", out.path)))?;
let textures = texture_paths.into_iter().map(|file| file.display().to_string()).collect();
s.log.info("Exporter", format!("Exported mesh to {}", out.path));
Ok(out.reply(textures))
}
@@ -963,10 +964,51 @@ f 1/1 2/2 3/3
assert_eq!(back.mesh.materials, s.mesh.materials);
// Base color, ORM and normal per texture set.
assert_eq!(bytes.windows(9).filter(|w| *w == b"image/png").count(), 3 * s.mesh.materials.len());
let reopened = Session::from_mesh_bytes("ball.glb", &bytes, 128, "ball").unwrap();
assert_eq!(reopened.mesh.triangle_count(), s.mesh.triangle_count());
assert_eq!(reopened.mesh.materials, s.mesh.materials);
assert!(crate::execute(&mut s, "file.exportMesh", &json!({ "path": base, "format": "fbx" })).is_err());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn rejected_glb_export_preserves_existing_files_and_publishes_nothing() {
let mut s = Session::new_project("t", primitives::cube(1.0), "builtin:cube", None, 128).unwrap();
let dir = std::env::temp_dir().join(format!("tc_rejected_glb_export_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let path = dir.join("cube.glb");
crate::execute(&mut s, "file.exportMesh", &json!({ "path": path, "format": "glb" })).unwrap();
let before = std::fs::read(&path).unwrap();
std::sync::Arc::make_mut(&mut s.mesh).indices[0] = u32::MAX;
assert!(s.mesh_glb_bytes().unwrap_err().to_string().contains("corner index is out of range"));
for destination in [path.clone(), dir.join("new.glb")] {
let error = crate::execute(&mut s, "file.exportMesh", &json!({ "path": destination, "format": "glb" })).unwrap_err();
assert!(error.to_string().contains("corner index is out of range"), "{error}");
}
assert_eq!(std::fs::read(&path).unwrap(), before);
assert!(!dir.join("new.glb").exists());
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn export_mesh_publishes_nothing_when_final_target_is_a_directory() {
let mut s = Session::new_project("t", primitives::cube(1.0), "builtin:cube", None, 128).unwrap();
let dir = std::env::temp_dir().join(format!("tc_atomic_obj_export_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(dir.join("blocked.obj")).unwrap();
let existing_mtl = dir.join("blocked.mtl");
std::fs::write(&existing_mtl, b"keep-existing-mtl").unwrap();
let error = crate::execute(&mut s, "file.exportMesh", &json!({ "path": dir.join("blocked.obj") })).unwrap_err();
assert!(error.to_string().contains("cannot write"), "{error}");
assert_eq!(std::fs::read(&existing_mtl).unwrap(), b"keep-existing-mtl");
let entries: Vec<_> = std::fs::read_dir(&dir).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect();
assert_eq!(entries.len(), 2, "no companion textures or temp files should be published: {entries:?}");
let _ = std::fs::remove_dir_all(&dir);
}
/// A mesh without materials exports its one implicit material, maps included.
#[test]
fn obj_export_declares_the_implicit_material() {
+13
View File
@@ -122,6 +122,19 @@ fn projects_round_trip_and_meshes_export_their_own_uvs() {
let body = obj.strip_prefix("o ").or_else(|| obj.split_once("\no ").map(|(_, rest)| rest)).unwrap();
assert!(String::from_utf8(with_mtl).unwrap().ends_with(body));
assert_eq!(files.len(), 1, "the MTL alone");
// GLB export embeds the UDIM tile atlas and reopens with UVs in [0, 1] atlas space.
let dir = std::env::temp_dir().join(format!("tc_udim_glb_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let glb_reply = execute(&mut s, "file.exportMesh", &json!({ "path": dir.join("plane.glb"), "format": "glb" })).unwrap();
let glb_bytes = std::fs::read(glb_reply["path"].as_str().unwrap()).unwrap();
let back = texturecraft_formats::import_mesh_bytes("plane.glb", &glb_bytes).unwrap();
assert_eq!(back.mesh.triangle_count(), s.mesh.triangle_count());
assert_eq!(back.mesh.materials, s.mesh.materials);
assert!(back.mesh.uvs.iter().all(|uv| (0.0..=1.0).contains(&uv.x) && (0.0..=1.0).contains(&uv.y)));
assert!(glb_bytes.windows(9).filter(|w| *w == b"image/png").count() >= 3);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
+15 -16
View File
@@ -1534,25 +1534,24 @@ fn keep_first_names<T>(files: Vec<T>, name: impl Fn(&T) -> &str, warnings: impl
kept
}
/// Write exported files into `dir` (created if missing), each atomically (`name.tmp`, then
/// rename). Names must be plain file names.
/// Write exported files into `dir` (created if missing) via [`crate::write_batch_atomic`]:
/// all filenames are validated and staged before any rename, and a later rename failure rolls
/// back earlier publications (concurrent readers may briefly observe a mixed generation during
/// the rename loop). Names must be plain file names.
#[cfg(not(target_arch = "wasm32"))]
pub fn write_files(files: &[ExportedFile], dir: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
std::fs::create_dir_all(dir)?;
files
.iter()
.map(|f| {
// Cap the sanitized stem before adding the extension
let (stem, ext) = f.name.rsplit_once('.').unwrap_or((&f.name, ""));
if f.name.is_empty() || stem != sanitize_file_stem(stem) || !ext.chars().all(|c| c.is_ascii_alphanumeric()) || f.name.trim_matches('.').is_empty() {
return Err(FormatError::Invalid(format!("\"{}\" is not a plain file name", f.name)));
}
let path = dir.join(&f.name);
crate::write_atomic(&path, &f.bytes)?;
Ok(path)
})
.collect()
let mut targets = Vec::with_capacity(files.len());
for f in files {
// Cap the sanitized stem before adding the extension
let (stem, ext) = f.name.rsplit_once('.').unwrap_or((&f.name, ""));
if f.name.is_empty() || stem != sanitize_file_stem(stem) || !ext.chars().all(|c| c.is_ascii_alphanumeric()) || f.name.trim_matches('.').is_empty() {
return Err(FormatError::Invalid(format!("\"{}\" is not a plain file name", f.name)));
}
targets.push((dir.join(&f.name), f.bytes.as_slice()));
}
crate::write_batch_atomic(targets.iter().map(|(path, bytes)| (path.as_path(), *bytes)))?;
Ok(targets.into_iter().map(|(path, _)| path).collect())
}
#[cfg(test)]
+77
View File
@@ -413,6 +413,69 @@ fn jpeg_texture_export_keeps_full_chroma() {
assert!(base.warnings.iter().any(|w| w.contains("alpha")));
}
#[test]
fn jpeg_grayscale_and_depth_policy_preserve_8bit_output() {
let mut fx = Fx::new(&[Channel::Roughness]);
fx.set_all(Channel::Roughness, |_, _| [0.4; 3]);
let mut preset = builtin_preset("Separate maps").unwrap();
preset.maps = vec![
MapSpec {
file: "gray".into(),
format: FileFormat::Jpeg,
bit_depth: 8,
channels: vec![(Dest::L, SourceRef::Channel { channel: Channel::Roughness, component: 0 })],
inverted: Vec::new(),
},
MapSpec {
file: "gray_alpha".into(),
format: FileFormat::Jpeg,
bit_depth: 8,
channels: vec![(Dest::L, SourceRef::Channel { channel: Channel::Roughness, component: 0 }), (Dest::A, SourceRef::Constant(0.5))],
inverted: Vec::new(),
},
];
for depth in [8, 16, 32] {
let mut opts = ExportOptions::new(preset.clone());
opts.bit_depth_override = Some(depth);
let planned = list_outputs(&fx.project, 0, "m", &opts).unwrap();
assert!(planned.iter().all(|p| p.bit_depth == 8), "JPEG always plans 8-bit even for override {depth}");
let files = fx.render(&opts);
assert_eq!(files.len(), 2);
for file in &files {
// SOF0 header for 1-component 8-bit baseline JPEG: FF C0 00 0B 08 HH HH WW WW 01
let sof0 = [0xff, 0xc0, 0, 11, 8, 0, RES as u8, 0, RES as u8, 1];
assert!(file.bytes.windows(sof0.len()).any(|w| w == sof0), "missing 8-bit 1-component JPEG SOF0 in {}", file.name);
}
assert!(files[1].warnings.iter().any(|w| w.contains("alpha")));
}
}
#[test]
fn format_extension_aliases_and_repeated_names_policy() {
for (alias, expected) in [
("jpeg", FileFormat::Jpeg),
("jpg", FileFormat::Jpeg),
(".JPG", FileFormat::Jpeg),
("tiff", FileFormat::Tiff),
("tif", FileFormat::Tiff),
(".TIF", FileFormat::Tiff),
("targa", FileFormat::Tga),
("tga", FileFormat::Tga),
(".TGA", FileFormat::Tga),
] {
assert_eq!(FileFormat::from_extension(alias), Some(expected), "alias `{alias}`");
}
let mut files = vec![
ExportedFile { name: "Mesh_BaseColor.png".into(), bytes: vec![1], width: 1, height: 1, warnings: Vec::new() },
ExportedFile { name: "mesh_basecolor.png".into(), bytes: vec![2], width: 1, height: 1, warnings: Vec::new() },
];
crate::skip_repeated_names(&mut files);
assert_eq!(files.len(), 1);
assert_eq!(files[0].bytes, vec![1]);
assert!(files[0].warnings.iter().any(|w| w.contains("mesh_basecolor.png")));
}
#[test]
fn list_outputs_matches_render_and_presets_round_trip() {
let fx = Fx::new(&default_channels());
@@ -459,6 +522,20 @@ fn write_files_is_atomic_and_refuses_paths() {
assert!(std::fs::read_dir(&dir).unwrap().all(|e| !e.unwrap().file_name().to_string_lossy().ends_with(".tmp")));
let evil = ExportedFile { name: "../evil.png".into(), ..files[0].clone() };
assert!(write_files(&[evil], &dir).is_err());
// Batch failure: invalid second entry or directory target must publish nothing and preserve existing files.
let sentinel = ExportedFile { name: files[0].name.clone(), bytes: b"new-bytes".to_vec(), ..files[0].clone() };
let bad_second = ExportedFile { name: "../bad.png".into(), ..files[1].clone() };
assert!(write_files(&[sentinel.clone(), bad_second], &dir).is_err());
assert_eq!(std::fs::read(&paths[0]).unwrap(), files[0].bytes);
let blocked_dir = dir.join("blocked.png");
std::fs::create_dir_all(&blocked_dir).unwrap();
let dir_target = ExportedFile { name: "blocked.png".into(), ..files[1].clone() };
assert!(write_files(&[sentinel, dir_target], &dir).is_err());
assert_eq!(std::fs::read(&paths[0]).unwrap(), files[0].bytes);
assert!(std::fs::read_dir(&dir).unwrap().all(|e| !e.unwrap().file_name().to_string_lossy().ends_with(".tmp")));
std::fs::remove_dir_all(&dir).unwrap();
}
+336 -12
View File
@@ -120,15 +120,34 @@ pub(crate) fn read_capped(path: &std::path::Path, cap: u64) -> Result<Vec<u8>> {
/// Create a new temporary file beside `path`, refusing to reuse an existing (possibly symlinked) name.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn create_temp(path: &std::path::Path) -> Result<(std::path::PathBuf, std::fs::File)> {
create_temp_with_mode(path, &std::collections::HashSet::new(), None)
}
#[cfg(not(target_arch = "wasm32"))]
fn create_temp_with_mode(
path: &std::path::Path,
excluded: &std::collections::HashSet<std::path::PathBuf>,
#[cfg_attr(not(unix), allow(unused_variables))] unix_mode: Option<u32>,
) -> Result<(std::path::PathBuf, std::fs::File)> {
let parent = path.parent().unwrap_or_else(|| std::path::Path::new("."));
let name = path.file_name().and_then(|name| name.to_str()).unwrap_or("out");
// Bound retries when stale files or concurrent writers occupy candidate names
const MAX_TEMP_ATTEMPTS: u32 = 100;
// Bound retries when stale files, batch targets, or concurrent writers occupy candidate names
const MAX_TEMP_ATTEMPTS: u32 = 256;
for attempt in 0..MAX_TEMP_ATTEMPTS {
let temporary_path = parent.join(format!(".{name}.{}.{attempt}.tmp", std::process::id()));
match std::fs::OpenOptions::new().write(true).create_new(true).open(&temporary_path) {
if excluded.contains(&temporary_path) {
continue;
}
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
if let Some(mode) = unix_mode {
use std::os::unix::fs::OpenOptionsExt;
options.mode(mode);
}
match options.open(&temporary_path) {
Ok(file) => return Ok((temporary_path, file)),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(error) => return Err(error.into()),
@@ -141,23 +160,302 @@ pub(crate) fn create_temp(path: &std::path::Path) -> Result<(std::path::PathBuf,
/// Write `bytes` to `path` atomically: a new temporary file first, then rename over the target.
#[cfg(not(target_arch = "wasm32"))]
pub fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<()> {
write_batch_atomic([(path, bytes)])
}
/// Write every `(path, bytes)` pair by staging and syncing all temporary files first (excluding
/// all batch targets), then renaming them into place one at a time. Concurrent readers can briefly
/// observe a mixed generation during the rename loop. Single-file batches rename directly without
/// creating a backup. For multi-file batches, if a later rename fails, earlier publications are
/// rolled back (restoring backups for overwritten targets and removing newly created targets), or
/// an explicit error is returned naming any incomplete rollback paths.
#[cfg(not(target_arch = "wasm32"))]
pub fn write_batch_atomic<'a>(entries: impl IntoIterator<Item = (&'a std::path::Path, &'a [u8])>) -> Result<()> {
write_batch_atomic_with_hook(entries, |_, _| Ok(()))
}
#[cfg(not(target_arch = "wasm32"))]
enum RollbackBackup {
File(std::path::PathBuf),
#[cfg(unix)]
Symlink(std::path::PathBuf),
}
#[cfg(not(target_arch = "wasm32"))]
fn target_matches_bytes(target_path: &std::path::Path, expected_bytes: &[u8]) -> bool {
use std::io::Read;
let Ok(meta) = std::fs::symlink_metadata(target_path) else { return false };
if !meta.file_type().is_file() {
return false;
}
let Ok(file) = std::fs::File::open(target_path) else { return false };
let Ok(handle_meta) = file.metadata() else { return false };
if !handle_meta.file_type().is_file() {
return false;
}
let mut buf = Vec::with_capacity(expected_bytes.len().min(4096));
if file.take(expected_bytes.len() as u64 + 1).read_to_end(&mut buf).is_err() {
return false;
}
buf.as_slice() == expected_bytes
}
#[cfg(not(target_arch = "wasm32"))]
fn write_batch_atomic_with_hook<'a>(
entries: impl IntoIterator<Item = (&'a std::path::Path, &'a [u8])>,
mut before_rename: impl FnMut(usize, &std::path::Path) -> std::io::Result<()>,
) -> Result<()> {
use std::io::Write;
let (temporary_path, mut file) = create_temp(path)?;
let result = file.write_all(bytes).and_then(|_| file.sync_all());
drop(file);
// Serialize in-process batch publications so concurrent tasks cannot interleave backups/rollbacks.
static BATCH_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _guard = BATCH_LOCK.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
let result = result.and_then(|_| std::fs::rename(&temporary_path, path));
if result.is_err() {
let _ = std::fs::remove_file(&temporary_path);
let entries: Vec<(&std::path::Path, &[u8])> = entries.into_iter().collect();
if entries.is_empty() {
return Ok(());
}
let mut reserved: std::collections::HashSet<std::path::PathBuf> = entries.iter().map(|(path, _)| path.to_path_buf()).collect();
let mut staged: Vec<(std::path::PathBuf, &std::path::Path, &[u8])> = Vec::with_capacity(entries.len());
let cleanup_staged = |staged: &[(std::path::PathBuf, &std::path::Path, &[u8])]| {
for (temp_path, _, _) in staged {
let _ = std::fs::remove_file(temp_path);
}
};
for (path, bytes) in entries {
if path.is_dir() {
cleanup_staged(&staged);
return Err(FormatError::Io(std::io::Error::other(format!("{} is a directory", path.display()))));
}
let (temporary_path, mut file) = match create_temp_with_mode(path, &reserved, None) {
Ok(pair) => pair,
Err(error) => {
cleanup_staged(&staged);
return Err(error);
}
};
reserved.insert(temporary_path.clone());
let write_result = file.write_all(bytes).and_then(|_| file.sync_all());
drop(file);
if let Err(error) = write_result {
let _ = std::fs::remove_file(&temporary_path);
cleanup_staged(&staged);
return Err(error.into());
}
staged.push((temporary_path, path, bytes));
}
Ok(result?)
// Single-file writes have no earlier publications to roll back, so rename directly.
if staged.len() == 1 {
let (ref temporary_path, target_path, _) = staged[0];
if let Err(error) = before_rename(0, target_path).and_then(|()| std::fs::rename(temporary_path, target_path)) {
let _ = std::fs::remove_file(temporary_path);
return Err(error.into());
}
return Ok(());
}
let mut published: Vec<(&std::path::Path, &[u8], Option<RollbackBackup>)> = Vec::with_capacity(staged.len());
let rollback = |published: &mut Vec<(&std::path::Path, &[u8], Option<RollbackBackup>)>,
remaining: &[(std::path::PathBuf, &std::path::Path, &[u8])],
cause: std::io::Error|
-> FormatError {
let mut incomplete = Vec::new();
for (target_path, expected_bytes, backup) in published.drain(..).rev() {
// Do not overwrite if an external writer modified the target after we published it.
if !target_matches_bytes(target_path, expected_bytes) {
if let Some(RollbackBackup::File(backup_path)) = backup {
incomplete.push(format!("{} (modified by another writer; backup at {})", target_path.display(), backup_path.display()));
} else {
incomplete.push(format!("{} (modified by another writer)", target_path.display()));
}
continue;
}
match backup {
Some(RollbackBackup::File(backup_path)) => {
if let Err(err) = std::fs::rename(&backup_path, target_path) {
incomplete.push(format!("{} (backup at {}): {err}", target_path.display(), backup_path.display()));
}
}
#[cfg(unix)]
Some(RollbackBackup::Symlink(link_target)) => {
if let Err(err) = std::fs::remove_file(target_path).and_then(|()| std::os::unix::fs::symlink(&link_target, target_path)) {
incomplete.push(format!("{} (symlink -> {}): {err}", target_path.display(), link_target.display()));
}
}
None => {
if let Err(err) = std::fs::remove_file(target_path) {
incomplete.push(format!("{}: {err}", target_path.display()));
}
}
}
}
cleanup_staged(remaining);
if incomplete.is_empty() {
FormatError::Io(cause)
} else {
FormatError::Io(std::io::Error::other(format!("{cause}; incomplete rollback: {}", incomplete.join("; "))))
}
};
for index in 0..staged.len() {
let target_path = staged[index].1;
let expected_bytes = staged[index].2;
if let Err(error) = before_rename(index, target_path) {
return Err(rollback(&mut published, &staged[index..], error));
}
let backup = match std::fs::symlink_metadata(target_path) {
Ok(meta) => {
#[cfg(unix)]
if meta.file_type().is_symlink() {
match std::fs::read_link(target_path) {
Ok(link_target) => Some(RollbackBackup::Symlink(link_target)),
Err(error) => return Err(rollback(&mut published, &staged[index..], error)),
}
} else if !meta.file_type().is_file() {
return Err(rollback(&mut published, &staged[index..], std::io::Error::other(format!("{} is not a regular file", target_path.display()))));
} else {
match stage_file_backup(target_path, &meta, &mut reserved) {
Ok(path) => Some(RollbackBackup::File(path)),
Err(error) => return Err(rollback(&mut published, &staged[index..], error)),
}
}
#[cfg(not(unix))]
{
if !meta.file_type().is_file() {
return Err(rollback(
&mut published,
&staged[index..],
std::io::Error::other(format!("{} is not a regular file", target_path.display())),
));
}
match stage_file_backup(target_path, &meta, &mut reserved) {
Ok(path) => Some(RollbackBackup::File(path)),
Err(error) => return Err(rollback(&mut published, &staged[index..], error)),
}
}
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
Err(error) => return Err(rollback(&mut published, &staged[index..], error)),
};
if let Err(error) = std::fs::rename(&staged[index].0, target_path) {
if let Some(RollbackBackup::File(ref backup_path)) = backup {
let _ = std::fs::remove_file(backup_path);
}
return Err(rollback(&mut published, &staged[index..], error));
}
published.push((target_path, expected_bytes, backup));
}
for (_, _, backup) in published {
if let Some(RollbackBackup::File(backup_path)) = backup {
let _ = std::fs::remove_file(backup_path);
}
}
Ok(())
}
#[cfg(not(target_arch = "wasm32"))]
fn stage_file_backup(
target_path: &std::path::Path,
meta: &std::fs::Metadata,
reserved: &mut std::collections::HashSet<std::path::PathBuf>,
) -> std::io::Result<std::path::PathBuf> {
let (backup_path, mut backup_file) = create_temp_with_mode(target_path, reserved, Some(0o600)).map_err(|e| std::io::Error::other(e.to_string()))?;
reserved.insert(backup_path.clone());
let copy_result = (|| -> std::io::Result<()> {
let mut src = std::fs::File::open(target_path)?;
std::io::copy(&mut src, &mut backup_file)?;
backup_file.set_permissions(meta.permissions())?;
backup_file.sync_all()?;
Ok(())
})();
drop(backup_file);
if let Err(error) = copy_result {
let _ = std::fs::remove_file(&backup_path);
return Err(error);
}
Ok(backup_path)
}
#[cfg(test)]
mod batch_atomic_tests {
use super::{write_batch_atomic, write_batch_atomic_with_hook};
#[test]
fn write_batch_atomic_publishes_nothing_on_failure() -> std::io::Result<()> {
let directory = std::env::temp_dir().join(format!("tc-batch-atomic-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&directory);
std::fs::create_dir_all(&directory)?;
let existing = directory.join("existing.png");
std::fs::write(&existing, b"old")?;
let blocked = directory.join("blocked_dir");
std::fs::create_dir_all(&blocked)?;
let result = write_batch_atomic([(existing.as_path(), b"new".as_slice()), (blocked.as_path(), b"data".as_slice())]);
assert!(result.is_err());
assert_eq!(std::fs::read(&existing)?, b"old");
assert!(std::fs::read_dir(&directory)?.all(|entry| !entry.unwrap().file_name().to_string_lossy().ends_with(".tmp")));
let _ = std::fs::remove_dir_all(&directory);
Ok(())
}
#[test]
fn write_batch_atomic_avoids_colliding_with_later_batch_target() -> std::io::Result<()> {
let directory = std::env::temp_dir().join(format!("tc-batch-reserve-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&directory);
std::fs::create_dir_all(&directory)?;
let temp_named_target = directory.join(format!(".a.png.{}.0.tmp", std::process::id()));
let main_target = directory.join("a.png");
write_batch_atomic([(main_target.as_path(), b"main-bytes".as_slice()), (temp_named_target.as_path(), b"temp-named-bytes".as_slice())])
.map_err(std::io::Error::other)?;
assert_eq!(std::fs::read(&main_target)?, b"main-bytes");
assert_eq!(std::fs::read(&temp_named_target)?, b"temp-named-bytes");
let _ = std::fs::remove_dir_all(&directory);
Ok(())
}
#[test]
fn write_batch_atomic_rolls_back_earlier_publications_when_later_rename_fails() -> std::io::Result<()> {
let directory = std::env::temp_dir().join(format!("tc-batch-rollback-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&directory);
std::fs::create_dir_all(&directory)?;
let existing = directory.join("existing.png");
std::fs::write(&existing, b"old")?;
let fresh = directory.join("fresh.png");
let late = directory.join("late.png");
let result = write_batch_atomic_with_hook(
[(existing.as_path(), b"new".as_slice()), (fresh.as_path(), b"fresh-bytes".as_slice()), (late.as_path(), b"late-bytes".as_slice())],
|index, target| {
if index == 2 {
std::fs::create_dir_all(target)?;
}
Ok(())
},
);
assert!(result.is_err());
assert_eq!(std::fs::read(&existing)?, b"old");
assert!(!fresh.exists());
assert!(std::fs::read_dir(&directory)?.all(|entry| !entry.unwrap().file_name().to_string_lossy().ends_with(".tmp")));
let _ = std::fs::remove_dir_all(&directory);
Ok(())
}
}
#[cfg(all(test, unix))]
mod atomic_tests {
use super::write_atomic;
use super::{write_atomic, write_batch_atomic_with_hook};
#[test]
fn write_atomic_does_not_follow_a_planted_temp_symlink() -> std::io::Result<()> {
@@ -167,7 +465,7 @@ mod atomic_tests {
let target = directory.join("out.png");
let victim = directory.join("victim.txt");
std::fs::write(&victim, b"safe")?;
std::os::unix::fs::symlink(&victim, directory.join("out.png.tmp"))?;
std::os::unix::fs::symlink(&victim, directory.join(format!(".out.png.{}.0.tmp", std::process::id())))?;
write_atomic(&target, b"new").map_err(std::io::Error::other)?;
@@ -177,4 +475,30 @@ mod atomic_tests {
let _ = std::fs::remove_dir_all(&directory);
Ok(())
}
#[test]
fn write_batch_atomic_rolls_back_symlink_target_without_touching_referent() -> std::io::Result<()> {
let directory = std::env::temp_dir().join(format!("tc-batch-symlink-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&directory);
std::fs::create_dir_all(&directory)?;
let referent = directory.join("referent.png");
std::fs::write(&referent, b"referent-bytes")?;
let link = directory.join("link.png");
std::os::unix::fs::symlink(&referent, &link)?;
let late = directory.join("late.png");
let result = write_batch_atomic_with_hook([(link.as_path(), b"new".as_slice()), (late.as_path(), b"late".as_slice())], |index, target| {
if index == 1 {
std::fs::create_dir_all(target)?;
}
Ok(())
});
assert!(result.is_err());
assert_eq!(std::fs::read_link(&link)?, referent);
assert_eq!(std::fs::read(&referent)?, b"referent-bytes");
let _ = std::fs::remove_dir_all(&directory);
Ok(())
}
}
+41
View File
@@ -166,4 +166,45 @@ mod tests {
assert!(mesh_map_png(0, 1, &[]).is_err());
assert!(mesh_map_png(2, 2, &[0.0; 8]).is_err());
}
#[test]
fn mesh_maps_encode_gray16_and_rgb16_with_exact_png_headers() {
let decode_raw_16 = |bytes: &[u8]| {
let decoder = png::Decoder::new(Cursor::new(bytes));
let mut reader = decoder.read_info().unwrap();
let info = reader.info().clone();
let mut buf = vec![0u8; reader.output_buffer_size().unwrap()];
let frame = reader.next_frame(&mut buf).unwrap();
let samples: Vec<u16> = buf[..frame.buffer_size()].as_chunks::<2>().0.iter().copied().map(u16::from_be_bytes).collect();
(info, samples)
};
let gray = mesh_map_png(2, 1, &[-0.5, 0.25, 0.5, 1.5]).unwrap();
let (gray_info, gray_samples) = decode_raw_16(&gray);
assert_eq!(gray_info.bit_depth, png::BitDepth::Sixteen);
assert_eq!(gray_info.color_type, png::ColorType::Grayscale);
assert_eq!(gray_samples, [0, 16384, 32768, 65535]);
let rgb = mesh_map_png(2, 3, &[0.0, 0.25, 0.5, 1.0, 0.75, 0.125, 0.5, 0.5, 0.5, 0.0, 1.0, 0.0]).unwrap();
let (rgb_info, rgb_samples) = decode_raw_16(&rgb);
assert_eq!(rgb_info.bit_depth, png::BitDepth::Sixteen);
assert_eq!(rgb_info.color_type, png::ColorType::Rgb);
assert_eq!(rgb_samples, [0, 16384, 32768, 65535, 49151, 8192, 32768, 32768, 32768, 0, 65535, 0]);
}
#[test]
fn write_mesh_map_png_propagates_writer_errors() {
struct FailingWriter;
impl Write for FailingWriter {
fn write(&mut self, _buf: &[u8]) -> std::io::Result<usize> {
Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "disk full"))
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
let error = write_mesh_map_png(FailingWriter, 2, 1, &[0.0, 0.25, 0.5, 1.0]).unwrap_err();
assert!(matches!(error, FormatError::Invalid(_)), "{error}");
}
}
+54
View File
@@ -279,6 +279,60 @@ mod tests {
}
}
#[test]
fn glb_writer_rejects_empty_mesh_and_invalid_corner_indices() {
let mut empty = texturecraft_geom::primitives::cube(1.0);
empty.positions.clear();
assert!(matches!(glb_bytes(&empty, "empty", &[]).unwrap_err(), FormatError::Invalid(_)));
let mut invalid = texturecraft_geom::primitives::cube(1.0);
invalid.indices[0] = u32::MAX;
let error = glb_bytes(&invalid, "invalid", &[]).unwrap_err();
assert!(matches!(error, FormatError::Invalid(_)));
assert!(error.to_string().contains("corner index is out of range"), "{error}");
}
#[test]
fn obj_with_mtl_links_all_material_texture_slots() {
let mesh = texturecraft_geom::primitives::cube(1.0);
let mut png = std::io::Cursor::new(Vec::new());
image::write_buffer_with_format(&mut png, &[200u8, 200, 200], 1, 1, image::ColorType::Rgb8, image::ImageFormat::Png).unwrap();
let png_bytes = png.into_inner();
let file = |name: &str| ExportedFile { name: name.into(), bytes: png_bytes.clone(), width: 1, height: 1, warnings: Vec::new() };
let textures = vec![vec![
file("BaseColor.png"),
file("Roughness.png"),
file("Metallic.png"),
file("Normal.png"),
file("Emissive.png"),
file("Opacity.png"),
file("Clearcoat.png"),
file("ClearcoatRoughness.png"),
]];
let (obj, files) = obj_with_mtl(&mesh, "project", "cube", &textures).unwrap();
let obj = String::from_utf8(obj).unwrap();
assert!(obj.contains("mtllib cube.mtl\n"));
assert!(obj.contains("usemtl Cube\n"));
let mtl = String::from_utf8(files[0].1.clone()).unwrap();
assert_eq!(files[0].0, "cube.mtl");
for (key, slot) in [
("map_Kd", "BaseColor"),
("map_Pr", "Roughness"),
("map_Pm", "Metallic"),
("norm", "Normal"),
("map_Bump", "Normal"),
("map_Ke", "Emissive"),
("map_d", "Opacity"),
("map_Pc", "Clearcoat"),
("map_Pcr", "ClearcoatRoughness"),
] {
let expected = format!("{key} cube_Cube_{slot}.png");
assert!(mtl.lines().any(|line| line == expected), "missing `{expected}` in:\n{mtl}");
}
assert!(mtl.contains("Ke 1 1 1\nmap_Ke cube_Cube_Emissive.png"));
assert_eq!(files.len(), 9);
}
#[test]
fn name_with_newline_stays_in_the_header_comment() {
let mesh = texturecraft_geom::primitives::cube(1.0);