diff --git a/lib/file-discovery.js b/lib/file-discovery.js index 0e6482d..2f7e8d0 100644 --- a/lib/file-discovery.js +++ b/lib/file-discovery.js @@ -1,5 +1,6 @@ const fs = require('fs'); const path = require('path'); +const { isStagedSourceFile } = require('./source-cleanup-policy'); async function walkFolderAsync(rootDir, options = {}) { const fsPromises = options.fsPromises || fs.promises; @@ -22,7 +23,7 @@ async function walkFolderAsync(rootDir, options = {}) { const fullPath = pathImpl.join(dir, entry.name); if (entry.isDirectory()) { if (recursive) stack.push(fullPath); - } else if (entry.isFile()) { + } else if (entry.isFile() && !isStagedSourceFile(entry.name)) { let size = 0; try { size = (await fsPromises.stat(fullPath)).size; diff --git a/lib/folder-monitor.js b/lib/folder-monitor.js index ff9051c..d6f4c73 100644 --- a/lib/folder-monitor.js +++ b/lib/folder-monitor.js @@ -4,6 +4,7 @@ const path = require('path'); const chokidar = require('chokidar'); const { walkFolderAsync } = require('./file-discovery'); const { normalizeAutomationSettings } = require('./automation-control'); +const { isStagedSourceFile } = require('./source-cleanup-policy'); class FolderMonitor extends EventEmitter { constructor({ @@ -267,6 +268,7 @@ class FolderMonitor extends EventEmitter { _onNewFile(filePath, generation) { if (!this._acceptCallback(generation)) return; + if (isStagedSourceFile(filePath)) return; if (!this._classifyPath(filePath, this._settings).allowed) return; const normalized = this._normalizePath(filePath); const ownsSeenReservation = !!this._settings?.skipDuplicates && !this._seenFiles.has(normalized); @@ -381,6 +383,7 @@ class FolderMonitor extends EventEmitter { for (const descriptor of discovered) { if (!this._isCurrent(generation)) return []; if (!settings.recursive && this._isNestedPath(descriptor.path, settings.folderPath)) continue; + if (isStagedSourceFile(descriptor.path)) continue; const classification = this._classifyPath(descriptor.path, settings); let mtimeMs = 0; try { diff --git a/lib/source-cleanup-policy.js b/lib/source-cleanup-policy.js index b97f3fd..06cfcf2 100644 --- a/lib/source-cleanup-policy.js +++ b/lib/source-cleanup-policy.js @@ -3,6 +3,16 @@ const pathApi = typeof require === 'function' ? require('path') : null; const protectedStatuses = new Set(['done', 'error', 'aborted', 'skipped']); + const stagedSourcePattern = /^\..+\.mhu-delete-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; + + function stagedSourceName(fileName, id) { + return `.${fileName}.mhu-delete-${id}`; + } + + function isStagedSourceFile(file) { + if (typeof file !== 'string' || !file) return false; + return stagedSourcePattern.test(file.split(/[\\/]/).pop()); + } function normalizeFile(file, platform) { const value = typeof file === 'string' ? file.trim() : ''; @@ -212,7 +222,9 @@ prepareGroups, markCompleted, removeRequirement, - applyFingerprints + applyFingerprints, + stagedSourceName, + isStagedSourceFile }; if (typeof module !== 'undefined' && module.exports) { diff --git a/lib/source-file-cleanup.js b/lib/source-file-cleanup.js index 72db854..6801a3a 100644 --- a/lib/source-file-cleanup.js +++ b/lib/source-file-cleanup.js @@ -1,6 +1,7 @@ const RETRY_DELAYS = [100, 250, 500, 1000, 2000]; const TERMINAL_STATUSES = new Set(['done', 'error', 'aborted', 'skipped', 'failed', 'missing-account']); const crypto = require('node:crypto'); +const { stagedSourceName } = require('./source-cleanup-policy'); function createSourceFileCleanup(options) { if (!options || !options.fs || !options.path) { @@ -322,6 +323,7 @@ function createSourceFileCleanup(options) { await fs.promises.unlink(file); return { attempts }; } catch (error) { + if (error && error.code === 'ENOENT') return { attempts, alreadyRemoved: true }; const retryable = platform === 'win32' && error && (error.code === 'EBUSY' || error.code === 'EPERM'); if (!retryable || attempts > RETRY_DELAYS.length) throw Object.assign(error, { cleanupAttempts: attempts }); await wait(RETRY_DELAYS[attempts - 1]); @@ -346,7 +348,7 @@ function createSourceFileCleanup(options) { async function stageSource(manifest) { const stagedFile = path.join( path.dirname(manifest.file), - `.${path.basename(manifest.file)}.mhu-delete-${crypto.randomUUID()}` + stagedSourceName(path.basename(manifest.file), crypto.randomUUID()) ); if (!journal || typeof journal.plan !== 'function') { return { outcome: 'blocked', trigger: 'delete-journal-unavailable' }; @@ -464,6 +466,7 @@ function createSourceFileCleanup(options) { await emitAudit(manifest, 'deleted', { trigger: 'all-selected-hosters-succeeded', attempts: result.attempts, + ...(result.alreadyRemoved ? { alreadyRemoved: true } : {}), ...scope }); return 'deleted'; diff --git a/renderer/app.js b/renderer/app.js index 2b56f66..9c1e46d 100644 --- a/renderer/app.js +++ b/renderer/app.js @@ -446,6 +446,7 @@ function isStartableQueueJob(job) { function isAutomaticallyStartableQueueJob(job) { if (!isStartableQueueJob(job)) return false; + if (window.SourceCleanupPolicy?.isStagedSourceFile(job.file || job.fileName)) return false; if (job.status !== 'error' || !window.Stats) return true; return window.Stats.classifyErrorCategory(job.error) !== 'file-rejected'; }