Skip to content

commit 8d9d76a

abduznik edited this page May 23, 2026 · 1 revision

fix(downloader): resolve access denied errno 5 and improve dash matching

Commit: 8d9d76a20c289a1f88af32c9a4e469add1942e32

Author: abduznik

Date: 2026-04-28

Why: Fixes a bug or regression in the existing codebase.

Files Changed

lib/core/downloader/download_service.dart | 21 ++++++++++++++++++++-
 lib/core/romm/rom_scanner_service.dart    |  3 ++-
 2 files changed, 22 insertions(+), 2 deletions(-)
  • lib/core/downloader/download_service.dart
  • lib/core/romm/rom_scanner_service.dart

Diff

diff --git a/lib/core/downloader/download_service.dart b/lib/core/downloader/download_service.dart
index e4978fe..eac2c85 100644
--- a/lib/core/downloader/download_service.dart
+++ b/lib/core/downloader/download_service.dart
@@ -166,7 +166,26 @@ class DownloadService {
       }
 
       // Download complete, rename .part to final
-      File file = await partFile.rename(finalPath);
+      // On Windows, if finalPath is a directory or locked, rename() throws Access Denied (errno 5).
+      if (await Directory(finalPath).exists()) {
+        debugPrint("[DownloadService] Destination is a directory. Removing to allow rename.");
+        await Directory(finalPath).delete(recursive: true);
+      } else if (await File(finalPath).exists()) {
+        await File(finalPath).delete();
+      }
+
+      File file;
+      try {
+        file = await partFile.rename(finalPath);
+      } catch (e) {
+        debugPrint("[DownloadService] Rename failed ($e). Retrying with copy/delete fallback...");
+        // Fallback for Windows "Access Denied" or "File Locked"
+        // Wait a tiny bit for any scanner locks to release
+        await Future.delayed(const Duration(milliseconds: 200));
+        await partFile.copy(finalPath);
+        await partFile.delete();
+        file = File(finalPath);
+      }
       String currentPath = finalPath;
 
       // Extraction logic
diff --git a/lib/core/romm/rom_scanner_service.dart b/lib/core/romm/rom_scanner_service.dart
index 9b91b89..f538ff4 100644
--- a/lib/core/romm/rom_scanner_service.dart
+++ b/lib/core/romm/rom_scanner_service.dart
@@ -182,7 +182,8 @@ class RomScannerService {
   String _cleanName(String name) {
     return name.toLowerCase()
         .replaceAll(RegExp(r'[\(\[][^\]\)]*[\)\]]'), ' ') // Remove content inside () or []
-        .replaceAll(RegExp(r'[<>:"/\\|?*!\-\(\)\[\]]'), ' ') // Remove special chars
+        .replaceAll(RegExp(r'[:-]'), ' ') // Explicitly handle dashes and colons as spaces
+        .replaceAll(RegExp(r'[<>:"/\\|?*!\(\)\[\]]'), ' ') // Remove other special chars
         .replaceAll(RegExp(r'\s+'), ' ') // Collapse spaces
         .trim();
   }

Clone this wiki locally