Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Studio: keep same-name files when moving them to the Linux Trash together (#266)

DarkChunk585860e20e78
diff --git a/.changeset/linux-trash-collisions.md b/.changeset/linux-trash-collisions.md
new file mode 100644
index 0000000..a17084d
--- /dev/null
+++ b/.changeset/linux-trash-collisions.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Studio: on Linux, moving files with the same name to the Trash at the same time keeps every file and its original path. A failed move no longer leaves a record for a file that was not trashed.
diff --git a/packages/openfilm/studio/server/trash.mjs b/packages/openfilm/studio/server/trash.mjs
index e1e758d..13013f5 100644
--- a/packages/openfilm/studio/server/trash.mjs
+++ b/packages/openfilm/studio/server/trash.mjs
@@ -11,7 +11,7 @@
*/
import { execFile } from 'node:child_process';
import { existsSync } from 'node:fs';
-import { mkdir, rename, writeFile } from 'node:fs/promises';
+import { mkdir, rename, rm, writeFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { basename, extname, join } from 'node:path';
import { promisify } from 'node:util';
@@ -64,7 +64,19 @@ export async function moveToTrash(path) {
const info = join(root, 'Trash', 'info');
await mkdir(files, { recursive: true });
await mkdir(info, { recursive: true });
- const name = freeName(files, basename(path));
- await writeFile(join(info, `${name}.trashinfo`), `[Trash Info]\nPath=${encodeURI(path)}\nDeletionDate=${new Date().toISOString().slice(0, 19)}\n`);
- await rename(path, join(files, name));
+ const ext = extname(path), stem = basename(path, ext);
+ for (let n = 1; ; n++) {
+ const name = n === 1 ? basename(path) : `${stem} ${n}${ext}`;
+ if (existsSync(join(files, name))) continue;
+ const record = join(info, `${name}.trashinfo`);
+ try {
+ await writeFile(record, `[Trash Info]\nPath=${encodeURI(path)}\nDeletionDate=${new Date().toISOString().slice(0, 19)}\n`, { flag: 'wx' });
+ } catch (e) {
+ if (/** @type {NodeJS.ErrnoException} */ (e).code === 'EEXIST') continue;
+ throw e;
+ }
+ try { await rename(path, join(files, name)); }
+ catch (e) { await rm(record, { force: true }); throw e; }
+ return;
+ }
}
diff --git a/packages/openfilm/studio/server/trash.test.mjs b/packages/openfilm/studio/server/trash.test.mjs
new file mode 100644
index 0000000..4fd714c
--- /dev/null
+++ b/packages/openfilm/studio/server/trash.test.mjs
@@ -0,0 +1,58 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { moveToTrash } from './trash.mjs';
+
+const linuxTrash = (t) => {
+ const root = mkdtempSync(join(tmpdir(), 'of-linux-trash-'));
+ const platform = Object.getOwnPropertyDescriptor(process, 'platform');
+ const kept = { XDG_DATA_HOME: process.env.XDG_DATA_HOME, OPENFILM_TRASH_DIR: process.env.OPENFILM_TRASH_DIR };
+ Object.defineProperty(process, 'platform', { value: 'linux' });
+ process.env.XDG_DATA_HOME = join(root, 'data');
+ delete process.env.OPENFILM_TRASH_DIR;
+ t.after(() => {
+ Object.defineProperty(process, 'platform', platform);
+ for (const [key, value] of Object.entries(kept)) {
+ if (value === undefined) delete process.env[key]; else process.env[key] = value;
+ }
+ rmSync(root, { recursive: true, force: true });
+ });
+ return { root, files: join(root, 'data', 'Trash', 'files'), info: join(root, 'data', 'Trash', 'info') };
+};
+
+test('concurrent moves of files with the same name keep both files and their original paths', async (t) => {
+ const { root, files, info } = linuxTrash(t);
+ const paths = ['one', 'two'].map((folder) => {
+ mkdirSync(join(root, folder));
+ const path = join(root, folder, 'take.wav');
+ writeFileSync(path, folder);
+ return path;
+ });
+ await Promise.all(paths.map(moveToTrash));
+ const names = readdirSync(files).sort();
+ assert.deepEqual(names, ['take 2.wav', 'take.wav']);
+ assert.deepEqual(names.map((name) => readFileSync(join(files, name), 'utf8')).sort(), ['one', 'two']);
+ assert.ok(paths.every((path) => !existsSync(path)));
+ const originals = names.map((name) => decodeURI(readFileSync(join(info, `${name}.trashinfo`), 'utf8').match(/^Path=(.*)$/m)[1]));
+ assert.deepEqual(originals.sort(), paths.sort());
+});
+
+test('an existing trash record is not overwritten even when its file is missing', async (t) => {
+ const { root, files, info } = linuxTrash(t);
+ mkdirSync(info, { recursive: true });
+ writeFileSync(join(info, 'take.wav.trashinfo'), 'keep this record');
+ const path = join(root, 'take.wav');
+ writeFileSync(path, 'new recording');
+ await moveToTrash(path);
+ assert.equal(readFileSync(join(info, 'take.wav.trashinfo'), 'utf8'), 'keep this record');
+ assert.equal(readFileSync(join(files, 'take 2.wav'), 'utf8'), 'new recording');
+});
+
+test('a failed move leaves no new trash record', async (t) => {
+ const { root, files, info } = linuxTrash(t);
+ await assert.rejects(moveToTrash(join(root, 'missing.wav')), (e) => e.code === 'ENOENT');
+ assert.deepEqual(readdirSync(files), []);
+ assert.deepEqual(readdirSync(info), []);
+});