Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Train H: #5415 leaves only the clashing Cloud file behind instead of stopping the whole folder

milind-sonie65bb9b54be6
diff --git a/electron/cloud-sync-engine.mjs b/electron/cloud-sync-engine.mjs
index 16e8e71a4f..b4e4bf6fd6 100644
--- a/electron/cloud-sync-engine.mjs
+++ b/electron/cloud-sync-engine.mjs
@@ -113,21 +113,8 @@ export async function fetchOutputs({ client, state, folder, local, platform = pr
const base = state.base(folder.id);
const { entries: pc, temps } = await scanPc(local, base, { now });
for (const temp of temps) if (now() - temp.mtimeMs > TEMP_STALE_MS) fs.rmSync(temp.file, { force: true });
- const listed = (await client.files(folder.id)).filter((file) => validWirePath(file.path) && !neverFromCloud(file.path) && !(platform === "win32" && windowsProblem(file.path)));
- if (caseInsensitive(local, platform)) {
- const names = new Map();
- for (const file of listed) {
- const parts = file.path.split("/");
- for (let length = 1; length <= parts.length; length++) {
- const name = parts.slice(0, length).join("/"), key = foldKey(name);
- const previous = names.get(key);
- if (previous !== undefined && previous !== name) {
- throw new Error(`${JSON.stringify(previous)} and ${JSON.stringify(name)} have the same name on this computer. Rename one on My Cloud before syncing.`);
- }
- names.set(key, name);
- }
- }
- }
+ let listed = (await client.files(folder.id)).filter((file) => validWirePath(file.path) && !neverFromCloud(file.path) && !(platform === "win32" && windowsProblem(file.path)));
+ if (caseInsensitive(local, platform)) listed = withoutNameClashes(listed, result);
const cloud = new Map(listed.map((file) => [file.path, file]));
const { downloads, records } = planOutputs({ base, pc, cloud });
const ctx = { client, state, folder, local, pc, cloud, now, trashItem, markDownloaded, freeBytes, result };
@@ -142,6 +129,21 @@ export async function fetchOutputs({ client, state, folder, local, platform = pr
return result;
}
+/** On a disk where two Cloud paths name the same file, keep the first by path, leave the other on My Cloud and say so. */
+function withoutNameClashes(listed, result) {
+ const kept = new Map();
+ const dropped = new Set();
+ for (const file of [...listed].sort((a, b) => (a.path < b.path ? -1 : 1))) {
+ const parts = file.path.split("/");
+ const names = parts.map((_, index) => parts.slice(0, index + 1).join("/"));
+ const clash = names.find((name) => kept.has(foldKey(name)) && kept.get(foldKey(name)) !== name);
+ if (clash === undefined) { for (const name of names) kept.set(foldKey(name), name); continue; }
+ dropped.add(file);
+ result.problem ??= `${JSON.stringify(kept.get(foldKey(clash)))} and ${JSON.stringify(clash)} have the same name on this computer. Rename one on My Cloud.`;
+ }
+ return listed.filter((file) => !dropped.has(file));
+}
+
/** Where a wire path lives in this folder: never outside it, and never through a link or a file in the way. */
function localFile(ctx, wire) {
if (!validWirePath(wire)) throw new Error("My Cloud sent a path this app will not write.");
diff --git a/electron/cloud-sync-name-collisions.node-test.mjs b/electron/cloud-sync-name-collisions.node-test.mjs
index 70eca6a52a..90ae241e62 100644
--- a/electron/cloud-sync-name-collisions.node-test.mjs
+++ b/electron/cloud-sync-name-collisions.node-test.mjs
@@ -42,17 +42,27 @@ test("a single decomposed filename still uploads using its normalized wire name"
assert.equal(ctx.cloud.get(ctx.folder.id, "café.txt"), "coffee");
});
-for (const names of [["Report.txt", "report.txt"], ["Data", "data/result.txt"], ["Docs/a.txt", "docs/b.txt"]]) test(`refuses case-aliasing Cloud paths ${names.join(" and ")} on Windows`, async (t) => {
+/** Every file under `root`, by the exact names on disk, so a case-folding disk can't answer for the alias. */
+function filesIn(root, prefix = "") {
+ return readdirSync(join(root, ...prefix.split("/").filter(Boolean)), { withFileTypes: true }).flatMap((entry) => {
+ const path = prefix ? `${prefix}/${entry.name}` : entry.name;
+ return entry.isDirectory() ? filesIn(root, path) : [path];
+ }).sort();
+}
+
+for (const names of [["Report.txt", "report.txt"], ["Data", "data/result.txt"], ["Docs/a.txt", "docs/b.txt"]]) test(`keeps only the first of case-aliasing Cloud paths ${names.join(" and ")} on Windows`, async (t) => {
const ctx = setup(t);
- ctx.cloud.put(ctx.folder.id, names[0], "first");
ctx.cloud.put(ctx.folder.id, names[1], "second");
- const downloaded = [];
- await assert.rejects(fetchOutputs({ ...ctx, platform: "win32", freeBytes: () => 100 * 1024 ** 3,
- markDownloaded: async file => { downloaded.push(file); }, trashItem: async () => { throw new Error("must not trash"); },
- }), /same.*name|name.*conflict/i);
- assert.deepEqual(downloaded, []);
- assert.deepEqual(readdirSync(ctx.local), []);
- assert.equal(ctx.state.base(ctx.folder.id).size, 0);
+ ctx.cloud.put(ctx.folder.id, names[0], "first");
+ ctx.cloud.put(ctx.folder.id, "other.txt", "unrelated");
+ const result = await fetchOutputs({ ...ctx, platform: "win32", freeBytes: () => 100 * 1024 ** 3,
+ markDownloaded: async () => {}, trashItem: async () => { throw new Error("must not trash"); },
+ });
+ assert.match(result.problem, /same name/);
+ assert.equal(result.status, "problem");
+ assert.deepEqual(filesIn(ctx.local), [names[0], "other.txt"].sort());
+ assert.equal(readFileSync(join(ctx.local, ...names[0].split("/")), "utf8"), "first");
+ assert.equal(readFileSync(join(ctx.local, "other.txt"), "utf8"), "unrelated");
});
test("keeps distinct case-sensitive Cloud filenames on a filesystem that supports them", async (t) => {