diff --git a/src/Titano.Data/Archiving/SequenceTrash.cs b/src/Titano.Data/Archiving/SequenceTrash.cs index 5286fda4..87c72033 100644 --- a/src/Titano.Data/Archiving/SequenceTrash.cs +++ b/src/Titano.Data/Archiving/SequenceTrash.cs @@ -261,8 +261,18 @@ public sealed class SequenceTrash var record = new TrashRecord(row, await _db.Renders.ListBySequenceAsync(sequence.Id, cancellationToken).ConfigureAwait(false), step.Files, step.Bytes, DateTimeOffset.UtcNow, deletedBy); // The record first: a crash before the move leaves a record without a folder, which the listing ignores. - AtomicFile.WriteAllText(Path.Combine(trash, name + RecordExtension), JsonSerializer.Serialize(record, TrashJson.Default.TrashRecord)); - Directory.Move(folder.Folder, Path.Combine(trash, name)); + var recordPath = Path.Combine(trash, name + RecordExtension); + AtomicFile.WriteAllText(recordPath, JsonSerializer.Serialize(record, TrashJson.Default.TrashRecord)); + try + { + Directory.Move(folder.Folder, Path.Combine(trash, name)); + } + catch (Exception ex) when (ex is IOException or UnauthorizedAccessException) + { + // A file of the folder is open elsewhere: nothing moved, so the record goes and the rows stay. + File.Delete(recordPath); + throw; + } entry = Entry(archive.Id, name, record); } await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false))