fix(subsync): clean up temp dirs left by embedded and remote track syncs

Syncing an embedded or remote target kept its whole mkdtemp dir (original
extraction plus retimed copy) for the life of the machine. The original is now
removed right away; the dir holding the retimed file mpv loaded is retained
only while mpv lists it, pruned at the next run, removed on exit, and dirs a
crashed session left more than a day ago are swept once per process.
This commit is contained in:
sudacode committed 2026-11-28 03:34:55 -08:00
1 parent 732a61ee4b
commit 5b39e850f6
7 files changed
+150 -32

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { cleanupTemporaryFile, pruneRetainedSubsyncTempDirs } from './subsync-files';
test('synced embedded-track temp dirs keep only the loaded file and go away once mpv drops it', () => {
const isolatedTmp = fs.mkdtempSync(path.join(os.tmpdir(), 'subsync-files-test-'));
const previousTmpDir = process.env.TMPDIR;
process.env.TMPDIR = isolatedTmp;
try {
// Left behind by a crashed session two days ago.
const staleDir = fs.mkdtempSync(path.join(isolatedTmp, 'subminer-subsync-'));
const twoDaysAgo = (Date.now() - 2 * 24 * 60 * 60 * 1000) / 1000;
fs.utimesSync(staleDir, twoDaysAgo, twoDaysAgo);
// Extraction of an embedded target plus the retimed copy mpv loaded.
const runDir = fs.mkdtempSync(path.join(isolatedTmp, 'subminer-subsync-'));
const extractedPath = path.join(runDir, 'track_2.ass');
const retimedPath = path.join(runDir, 'track_2_retimed.ass');
fs.writeFileSync(extractedPath, 'original');
fs.writeFileSync(retimedPath, 'retimed');
cleanupTemporaryFile({ path: extractedPath, temporary: true });
assert.deepEqual(fs.readdirSync(runDir), ['track_2_retimed.ass']);
pruneRetainedSubsyncTempDirs([retimedPath]);
assert.equal(fs.existsSync(runDir), true, 'mpv still lists the retimed file');
assert.equal(fs.existsSync(staleDir), false, 'stale dirs from old sessions are swept');
pruneRetainedSubsyncTempDirs(['/media/ep02.ja.srt']);
assert.equal(fs.existsSync(runDir), false, 'dropped once mpv no longer lists it');
} finally {
if (previousTmpDir === undefined) delete process.env.TMPDIR;
else process.env.TMPDIR = previousTmpDir;
fs.rmSync(isolatedTmp, { recursive: true, force: true });
}
});
test('cleanupTemporaryFile removes a reference extraction and its dir entirely', () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-subsync-'));
const extractedPath = path.join(dir, 'track_3.srt');
fs.writeFileSync(extractedPath, 'reference');
cleanupTemporaryFile({ path: extractedPath, temporary: true });
assert.equal(fs.existsSync(dir), false);
});