Skip to content

Fail a Rename Onto an Existing File and Undo a Partial Rename - #1119

Merged
ptr727 merged 4 commits into
developfrom
feature/1073-1109-rename-collisions
Oct 31, 2026
Merged

ptr727 merged 4 commits into
developfrom
feature/1073-1109-rename-collisions

Conversation

@ptr727

@ptr727 ptr727 commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Summary

Handles rename collisions (#1073) and partial two-step moves (#1109) together, since both live in the same rename and move code.

Rename collisions (#1073)

  • Convert.WouldReplaceOtherFile() reports whether a rename or move onto a target would replace an existing entry other than the source itself. A case-only rename names the file itself unless the directory lists the exact target name. So a lowercase rename of the same file is not a collision on a case-insensitive file system, and a real sibling name.mkv beside name.MKV is a collision on a case-sensitive one.
  • MakeExtensionLowercase() and RemuxByExtension() check before renaming and fail the file when the target is another file. Each logs the target and the source.
  • DeleteFailedFile() keeps a file failed by a rename collision, since the collision says nothing about the media. Without this, DeleteInvalidFiles would delete the media the check just protected.
  • The check runs once, in the step, before any tool runs. The final move in Convert overwrites only when the output is the input itself. So a file created at the output name after the check fails the move instead of being replaced, and the thrown step keeps the file.
  • The lowercase rename's move to .tmp7 does not overwrite either, so a leftover name.tmp7, which may be media stranded by an earlier run, fails the step and is kept.

Recoverable two-step moves (#1109)

  • MakeExtensionLowercase() moves the file back to its original name when the second move throws, then rethrows. If that move back fails too, the step reports the .tmp7 path the media is at, with the modification.

  • Convert.ConvertToMkv() and both Convert.ReMuxToMkv() overloads share one ReplaceInput() step. A failed input delete after the move is logged and the step still returns the output, so the caller records the new path and the modification. A failed final move deletes the temp file and rethrows, so the step fails without deleting the input. A failed temp cleanup is logged so that the move failure is the exception that propagates.

  • A collision fails the file without deleting it. The collision is found before any tool runs. Process.cs is outside this change, so ProcessFile keeps the file through its own _renameCollision flag, which DeleteFailedFile() honors. That is the same outcome the skipped path gives an exception or a read-only file.

  • Residual case: a rename whose second move and move back both fail leaves the media at name.tmp7, and the row reports that path. A later run with DeleteUnwantedExtensions on deletes it as a non-MKV file, which is unchanged from before this change. The undo makes that a double failure, where the base code reached it after a single one.

  • Trade-off: the standalone reencode and deinterlace commands do not run the lowercase step. On a case-sensitive file system holding both name.MKV and a separate name.mkv, they find the collision only at the final move, after the encode. The move does not overwrite, the temp file is deleted, and nothing is lost, so only the encode time is spent.

Docs deferred

ARCHITECTURE.md (the S6 suspect-branch entry and the process diagrams) and HISTORY.md are not updated here. The maintainer chose to defer every doc update from this round to one final docs pull request, so review findings asking for those edits are declined with that reason.

Testing

  • New PlexCleanerTests/RenameCollisionTests.cs covers the collision helper (same name, missing target, other file, case-only rename of itself, case-only rename onto another file), a lowercase rename and a remux onto an existing file failing through Process.ProcessFile() with DeleteInvalidFiles on and both files kept, a lowercase rename with no sibling, a failed second lowercase move restoring the original name, a file created at the output name during a remux failing the file with both kept and the temp file deleted, a taken .tmp7 name failing the lowercase step with both kept, and a failed input delete after a remux reporting the output and the modification.
  • The behavior tests from the first commit fail against the base branch code and pass with the change.
  • Full suite passes, and the .NET Format chain is clean.
  • No real-media run is needed: the change is in the rename and move bookkeeping around the tools, and the tests drive it with fake tools and real files.

Closes on promotion: #1073, #1109

🤖 Generated with Claude Code

ptr727 and others added 3 commits October 30, 2026 20:22
A lowercase rename or a remux to .mkv that would replace a different
existing file fails the file and keeps both, on case-sensitive and
case-insensitive file systems alike. The kept file is not deleted by
DeleteInvalidFiles, since the collision says nothing about the media.

A lowercase rename whose second move fails moves the file back to its
original name, or reports the temp name it is left at. A remux or
reencode whose input delete fails after the move still reports the
output and the modification.

Fixes #1073
Fixes #1109

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A file created at the output name during a remux or reencode fails the
final move, which now deletes the temp file before rethrowing. A failed
undo of the lowercase rename reports the modification with the temp
path. The lowercase and remux steps call the same Convert check, so the
collision is logged in one place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI lite review requested due to automatic review settings October 31, 2026 03:30
@ptr727 ptr727 added the comments Permits the comment lines the pull request adds or edits, which the prose gate otherwise refuses label Oct 31, 2026
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 42 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 3476868b-2997-4711-9426-bab726843757
Commits

Reviewing files that changed from the base of the PR and between 0fe509c and f632bad.

Files selected for processing: 3
  • PlexCleaner/Convert.cs
  • PlexCleaner/ProcessFile.cs
  • PlexCleanerTests/RenameCollisionTests.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@ptr727

ptr727 commented Oct 31, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head d15d35db7a8f8ed1bcd1601889a55cbfe9b12d3d, the content this pull request carries at that commit against develop, and it recorded 0 findings.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Prevent rename collisions and recover from partial moves

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Fail colliding renames and conversions without replacing or deleting either file.
• Undo failed two-step renames and report the media’s current path when recovery fails.
• Preserve output bookkeeping after input-delete failures; test collision and recovery paths.
Diagram

graph TD
  P["Process file"] --> C{"Target collision?"} -->|no, lowercase| L["Two-step move"] --> O["Output media"]
  C -->|no, convert| T["Media tools"] --> X["Replace input"] --> O
  C -->|yes| K["Keep source"]
  L -->|failure, undo| K
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Rely solely on a non-overwriting final move
  • ➕ Avoids a separate preflight collision check and its time-of-check gap.
  • ➖ May run expensive conversion tools before discovering a collision.
  • ➖ Still needs special handling when the output names the input itself.

Recommendation: Keep the preflight check together with the guarded final move: the first avoids unnecessary tool work, while the second protects files created after that check. Retain the explicit undo and output bookkeeping because a move or subsequent delete can fail independently.

Files changed (3) +382 / -29

Bug fix (2) +137 / -29
Convert.csGuard conversion outputs and centralize final moves +90/-27

Guard conversion outputs and centralize final moves

• Adds a collision check that handles case-only names before either remux or conversion overload runs a tool. A shared finalization step avoids overwriting a newly created target, deletes temporary output if the move fails, and reports success after logging an input-delete failure.

PlexCleaner/Convert.cs

ProcessFile.csProtect colliding renames and undo incomplete lowercase moves +47/-2

Protect colliding renames and undo incomplete lowercase moves

• Checks lowercase-renaming and extension-remux targets before moving files, and prevents collision failures from triggering invalid-file deletion. If the second lowercase move fails, it attempts to restore the original name; if restoration also fails, it records the temporary path as modified.

PlexCleaner/ProcessFile.cs

Tests (1) +245 / -0
RenameCollisionTests.csExercise collisions and partial-move recovery +245/-0

Exercise collisions and partial-move recovery

• Adds filesystem and processing tests for case-sensitive and case-insensitive naming behavior, preservation of colliding files, and successful lowercase renames. Fake-tool and platform-specific tests cover a target appearing during remux, failed input deletion, and restoration after a failed second rename.

PlexCleanerTests/RenameCollisionTests.cs

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0) 🎨 UX issues (0) 🔗 Cross-repo conflicts (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. A new input can be deleted during remux 🐞 Bug ☼ Reliability
Description
ReplaceInput deletes inputName after moving the temporary output when IsSameFile returns
false, without checking whether the input path still refers to the file that was converted. On a
case-sensitive filesystem, if a case-only name.MKV to name.mkv remux is running and another
worker replaces name.MKV, the new output is moved into place and that worker's file is deleted.
Code

PlexCleaner/Convert.cs[252]

+            File.Delete(inputName);
Evidence
The remux checks for a target collision before running the tool, but the input path can change
during that work. For case-only names on a case-sensitive filesystem, the new inPlace calculation
is false; after the output move, the newly added delete acts on the current occupant of the input
path. The previous case-insensitive comparison skipped that delete.

PlexCleaner/Convert.cs[62-78]
PlexCleaner/Convert.cs[192-218]
PlexCleaner/Convert.cs[231-253]
PlexCleaner/ProcessFile.cs[189-215]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A case-only remux on a case-sensitive filesystem can delete a different file placed at the original input path while the tool runs.
## Fix Focus Areas
- PlexCleaner/Convert.cs[231-253]
## Recommended Fix
Retain an identity check for the original input and delete the input path only if it still refers to that file. If its identity changed, leave the new file in place and report that the old input could not be removed.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Failed moves can lose their traceback ✓ Resolved
Description
ReplaceInput calls File.Delete(tempName) in the move-failure handler without guarding against a
cleanup exception. If both the final move and temporary-file deletion fail, Process.ProcessFile
receives and logs only the cleanup exception, hiding the failure that initiated cleanup.
Code

PlexCleaner/Convert.cs[R239-242]

+        catch
+        {
+            File.Delete(tempName);
+            throw;
Evidence
The unguarded delete runs before throw, so any exception it raises replaces the pending move
exception. The processing boundary logs the exception it receives.

PlexCleaner/Convert.cs[235-243]
PlexCleaner/Process.cs[419-425]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
A failed temporary-file cleanup masks the original final-move failure.
## Fix Focus Areas
- PlexCleaner/Convert.cs[235-243]
## Recommended Fix
Catch and log any cleanup failure separately inside the move-failure handler, then rethrow the original move exception. Preserve both errors in the logs.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Tip of the day
💡 Did you know, you can keep summaries lean with Findings visible per group, which tucks the rest behind a View link

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Comment thread PlexCleaner/Convert.cs Resolved
Comment thread PlexCleaner/Convert.cs Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Four critical findings remain around case-only replacement, collision-state propagation, and overwriting temporary targets, including paths that can delete or destroy the original input.

4 open issues
What changed in this PR

This PR hardens rename and remux operations against file collisions and improves recovery from partial multi-step moves.

Changes:

  • Adds collision detection for lowercase renames, conversions, and remuxes.
  • Preserves colliding files and restores files after failed rename steps where possible.
  • Retains remux output when input deletion fails and adds focused regression tests.
  • Unresolved issues remain around collision-state propagation and protection of temporary rename targets.

This pull request requires changes.

File Description
PlexCleanerTests/​RenameCollisionTests.cs Adds regression coverage for collision detection, rollback, output races, and partial remux behavior.
PlexCleaner/​ProcessFile.cs Tracks rename collisions, guards temporary and lowercase renames, restores failed moves, and protects colliding inputs from deletion.
PlexCleaner/​Convert.cs Adds collision detection and shared replacement and cleanup logic for conversion and remux operations.

Review effort: Lite

Comment thread PlexCleaner/Convert.cs Resolved
Comment thread PlexCleaner/Convert.cs Resolved
Comment thread PlexCleaner/ProcessFile.cs Resolved
Comment thread PlexCleaner/ProcessFile.cs Resolved
The collision check lives in the ProcessFile steps alone, so a file
created at the output name afterwards fails the non-overwriting final
move, and the thrown step keeps the file rather than reaching
DeleteFailedFile. The lowercase rename does not overwrite an existing
temp name, and a failed temp cleanup no longer masks the move failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ptr727

ptr727 commented Oct 31, 2026

Copy link
Copy Markdown
Owner Author

A recorded local strict-review pass covers head f632bad5519538f1fcfcb582f1c6fbcf20b42864, the content this pull request carries at that commit against develop, and it recorded 0 findings.

@ptr727

ptr727 commented Oct 31, 2026

Copy link
Copy Markdown
Owner Author
  1. A new input can be deleted during remux

Declined, per GOVERNANCE.md "Trust Boundaries and Hardening Effort". The full reasoning is in the reply on its thread at Convert.cs:240 (#1119 (comment)). The case needs another process to replace the input file during a case-only conversion. The process pipeline cannot reach that, because MakeExtensionLowercase() runs first. Only the standalone reencode and deinterlace commands can reach it, and every non-in-place conversion already shared that class before this change.

@ptr727
ptr727 merged commit 2bb797b into develop Oct 31, 2026
23 checks passed
@ptr727
ptr727 deleted the feature/1073-1109-rename-collisions branch October 31, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comments Permits the comment lines the pull request adds or edits, which the prose gate otherwise refuses

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants