cursedbelt-server 4.50.0 → 4.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/dropzone/index.d.ts +5 -1
- package/dist/server/dropzone/index.js +5 -1
- package/dist/server/dropzone/links.d.ts +79 -0
- package/dist/server/dropzone/links.js +190 -0
- package/dist/server/dropzone/scan.d.ts +63 -3
- package/dist/server/dropzone/scan.js +105 -30
- package/package.json +1 -1
- package/src/server/dropzone/index.ts +21 -0
- package/src/server/dropzone/links.spec.ts +176 -0
- package/src/server/dropzone/links.ts +219 -0
- package/src/server/dropzone/scan.spec.ts +32 -1
- package/src/server/dropzone/scan.ts +142 -30
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
* 1. **A file is deleted only after the destination CONFIRMS it.** The dropzone is the one
|
|
20
20
|
* place the owner's copy may be the only copy: they dragged it in expecting it to be taken.
|
|
21
21
|
* A sweep that deletes on "the upload didn't throw" turns any catalog failure into data
|
|
22
|
-
* loss. The handler returns `stored`/`duplicate` and only then does the original go.
|
|
22
|
+
* loss. The handler returns `stored`/`duplicate` and only then does the original go. A file
|
|
23
|
+
* reached through a LINK is never deleted at all: it is the owner's copy where it lives.
|
|
23
24
|
*
|
|
24
25
|
* 2. **A file must measure the same size twice before it is touched.** Dropping a 4 GB video
|
|
25
26
|
* into a folder is not atomic. Without this the sweep ingests a truncated file, reports
|
|
@@ -33,8 +34,9 @@
|
|
|
33
34
|
*/
|
|
34
35
|
|
|
35
36
|
import type { Dirent } from "node:fs";
|
|
36
|
-
import { readdir, rm, rmdir, stat } from "node:fs/promises";
|
|
37
|
+
import { lstat, readdir, realpath, rm, rmdir, stat } from "node:fs/promises";
|
|
37
38
|
import { join, relative } from "node:path";
|
|
39
|
+
import { type LinkSource, linkSource, linkTarget, namedIn } from "./links.js";
|
|
38
40
|
import type { IngestReporter, ReportFile, ReportResult } from "./report.js";
|
|
39
41
|
|
|
40
42
|
/** A file the sweep found, with everything a handler needs to decide about it. */
|
|
@@ -51,10 +53,16 @@ export interface DroppedFile {
|
|
|
51
53
|
mtimeMs: number;
|
|
52
54
|
/** Every other FILE name in the same directory — see {@link ScanOptions.captureSiblings}. */
|
|
53
55
|
siblings: string[];
|
|
56
|
+
/**
|
|
57
|
+
* Reached through a symlink (`followLinks`): the owner's own copy, where it lives. A sweep
|
|
58
|
+
* never deletes one, whatever the handler answers — see rule 1. `realPath` is what an app keys
|
|
59
|
+
* it on, so renaming the link neither re-reads the drive nor files anything twice.
|
|
60
|
+
*/
|
|
61
|
+
linked?: { realPath: string };
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
export type DropOutcome =
|
|
57
|
-
/** Filed successfully. The original is deleted. */
|
|
65
|
+
/** Filed successfully. The original is deleted — unless it is `linked`, which stays where it is. */
|
|
58
66
|
| { kind: "stored"; detail?: string }
|
|
59
67
|
/** Already present at the destination. The original is deleted — that is the point. */
|
|
60
68
|
| { kind: "duplicate"; detail?: string }
|
|
@@ -76,7 +84,11 @@ export interface SweepReport {
|
|
|
76
84
|
unstable: string[];
|
|
77
85
|
bytesRemoved: number;
|
|
78
86
|
emptyDirsRemoved: number;
|
|
79
|
-
/**
|
|
87
|
+
/** Linked files dealt with and LEFT IN PLACE — the owner's copy on their own drive (`followLinks`). */
|
|
88
|
+
leftInPlace: number;
|
|
89
|
+
/** Links whose target is not there this sweep — a drive unplugged, or a folder gone (`followLinks`). */
|
|
90
|
+
unreachable: UnreachableLink[];
|
|
91
|
+
/** Anything left in the root when the sweep finished, links not followed. Empty is the goal. */
|
|
80
92
|
remaining: number;
|
|
81
93
|
}
|
|
82
94
|
|
|
@@ -97,6 +109,16 @@ export interface ScanOptions {
|
|
|
97
109
|
captureSiblings?: boolean;
|
|
98
110
|
/** Directory names to skip entirely at any depth (another app's queue, our own state). */
|
|
99
111
|
skipDirs?: string[];
|
|
112
|
+
/**
|
|
113
|
+
* Directory names skipped only directly under the root, compared as APFS does (case-insensitive)
|
|
114
|
+
* — an app's quarantine or `done/`, a name that is the owner's own inside a linked folder.
|
|
115
|
+
*/
|
|
116
|
+
skipTopDirs?: readonly string[];
|
|
117
|
+
/**
|
|
118
|
+
* Walk THROUGH symlinks: a linked folder is ingested in place (`links.ts`). Off by default, so
|
|
119
|
+
* no consumer changes behavior unasked. See {@link walkDropzone}.
|
|
120
|
+
*/
|
|
121
|
+
followLinks?: boolean;
|
|
100
122
|
/** Stop after this many files. Absent means everything. */
|
|
101
123
|
limit?: number;
|
|
102
124
|
}
|
|
@@ -128,6 +150,26 @@ const extOf = (name: string): string => {
|
|
|
128
150
|
return dot > 0 ? name.slice(dot).toLowerCase() : "";
|
|
129
151
|
};
|
|
130
152
|
|
|
153
|
+
/** A symlink the walk met whose target is not there this pass. */
|
|
154
|
+
export interface UnreachableLink {
|
|
155
|
+
/** The link, relative to the dropzone root. */
|
|
156
|
+
relativePath: string;
|
|
157
|
+
/** The link, absolute. */
|
|
158
|
+
path: string;
|
|
159
|
+
/**
|
|
160
|
+
* The link is in the dropzone's OWN tree (the owner put it there), not one met inside a linked
|
|
161
|
+
* folder on the owner's drive. Only an app's own link is ever its to act on.
|
|
162
|
+
*/
|
|
163
|
+
inDropzone: boolean;
|
|
164
|
+
source: Exclude<LinkSource, { state: "ok" }>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** One walk: the files, and — with `followLinks` — the links that could not be followed. */
|
|
168
|
+
export interface DropzoneScan {
|
|
169
|
+
files: DroppedFile[];
|
|
170
|
+
unreachable: UnreachableLink[];
|
|
171
|
+
}
|
|
172
|
+
|
|
131
173
|
/**
|
|
132
174
|
* Every candidate file under `root`, deepest-last, with each one's sibling listing.
|
|
133
175
|
*
|
|
@@ -136,11 +178,55 @@ const extOf = (name: string): string => {
|
|
|
136
178
|
* {@link isJunkName} is exported so a handler can classify without re-deriving the list.
|
|
137
179
|
*/
|
|
138
180
|
export async function scanDropzone(root: string, opts: ScanOptions = {}): Promise<DroppedFile[]> {
|
|
181
|
+
return (await walkDropzone(root, opts)).files;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* {@link scanDropzone}, with the links it could not follow.
|
|
186
|
+
*
|
|
187
|
+
* Without `followLinks` a symlink is skipped, as it always was. With it, a link to a directory is
|
|
188
|
+
* walked — once per REAL path, so a loop, or two links to one folder, or a link back into the
|
|
189
|
+
* dropzone, is not walked twice — and a link to a file is one file; everything reached through a
|
|
190
|
+
* link carries `linked`. A link whose target is missing is returned under `unreachable`, told
|
|
191
|
+
* apart by {@link linkSource}: `unmounted` (wait) or `deleted` (say so). A dangling link INSIDE a
|
|
192
|
+
* linked folder is the owner's drive's business; it is returned too, with `inDropzone: false`.
|
|
193
|
+
*/
|
|
194
|
+
export async function walkDropzone(root: string, opts: ScanOptions = {}): Promise<DropzoneScan> {
|
|
139
195
|
const skip = new Set(opts.skipDirs ?? []);
|
|
140
196
|
const out: DroppedFile[] = [];
|
|
197
|
+
const unreachable: UnreachableLink[] = [];
|
|
198
|
+
const walked = new Set<string>();
|
|
199
|
+
const full = (n: number): boolean => opts.limit !== undefined && n >= opts.limit;
|
|
200
|
+
|
|
201
|
+
const push = async (path: string, name: string, siblings: string[], linked: boolean): Promise<void> => {
|
|
202
|
+
try {
|
|
203
|
+
const info = await stat(path);
|
|
204
|
+
out.push({
|
|
205
|
+
path,
|
|
206
|
+
relativePath: relative(root, path),
|
|
207
|
+
name,
|
|
208
|
+
ext: extOf(name),
|
|
209
|
+
size: info.size,
|
|
210
|
+
mtimeMs: info.mtimeMs,
|
|
211
|
+
siblings,
|
|
212
|
+
...(linked ? { linked: { realPath: await realpath(path) } } : {}),
|
|
213
|
+
});
|
|
214
|
+
} catch {
|
|
215
|
+
/* gone between readdir and stat */
|
|
216
|
+
}
|
|
217
|
+
};
|
|
141
218
|
|
|
142
|
-
|
|
143
|
-
|
|
219
|
+
/** `false` when this directory's real path was already walked — the one way a link loops. */
|
|
220
|
+
const firstVisit = async (dir: string): Promise<boolean> => {
|
|
221
|
+
if (!opts.followLinks) return true;
|
|
222
|
+
const real = await realpath(dir).catch(() => null);
|
|
223
|
+
if (!real || walked.has(real)) return false;
|
|
224
|
+
walked.add(real);
|
|
225
|
+
return true;
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const walk = async (dir: string, linked: boolean): Promise<void> => {
|
|
229
|
+
if (full(out.length)) return;
|
|
144
230
|
let entries: Dirent[];
|
|
145
231
|
try {
|
|
146
232
|
entries = await readdir(dir, { withFileTypes: true });
|
|
@@ -152,33 +238,38 @@ export async function scanDropzone(root: string, opts: ScanOptions = {}): Promis
|
|
|
152
238
|
: [];
|
|
153
239
|
|
|
154
240
|
for (const entry of entries) {
|
|
155
|
-
if (
|
|
156
|
-
const
|
|
157
|
-
|
|
241
|
+
if (full(out.length)) return;
|
|
242
|
+
const path = join(dir, entry.name);
|
|
243
|
+
let isDir = entry.isDirectory();
|
|
244
|
+
let isFile = entry.isFile();
|
|
245
|
+
let viaLink = linked;
|
|
246
|
+
if (entry.isSymbolicLink()) {
|
|
247
|
+
if (!opts.followLinks) continue;
|
|
248
|
+
const target = await stat(path).catch(() => null);
|
|
249
|
+
if (!target) {
|
|
250
|
+
const source = linkSource((await linkTarget(path)) ?? path);
|
|
251
|
+
if (source.state !== "ok")
|
|
252
|
+
unreachable.push({ relativePath: relative(root, path), path, inDropzone: !linked, source });
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
isDir = target.isDirectory();
|
|
256
|
+
isFile = target.isFile();
|
|
257
|
+
viaLink = true;
|
|
258
|
+
}
|
|
259
|
+
if (isDir) {
|
|
158
260
|
if (skip.has(entry.name)) continue;
|
|
159
|
-
|
|
261
|
+
if (dir === root && namedIn(opts.skipTopDirs, entry.name)) continue;
|
|
262
|
+
if (!(await firstVisit(path))) continue;
|
|
263
|
+
await walk(path, viaLink);
|
|
160
264
|
continue;
|
|
161
265
|
}
|
|
162
|
-
if (
|
|
163
|
-
try {
|
|
164
|
-
const info = await stat(full);
|
|
165
|
-
out.push({
|
|
166
|
-
path: full,
|
|
167
|
-
relativePath: relative(root, full),
|
|
168
|
-
name: entry.name,
|
|
169
|
-
ext: extOf(entry.name),
|
|
170
|
-
size: info.size,
|
|
171
|
-
mtimeMs: info.mtimeMs,
|
|
172
|
-
siblings,
|
|
173
|
-
});
|
|
174
|
-
} catch {
|
|
175
|
-
/* gone between readdir and stat */
|
|
176
|
-
}
|
|
266
|
+
if (isFile) await push(path, entry.name, siblings, viaLink);
|
|
177
267
|
}
|
|
178
268
|
};
|
|
179
269
|
|
|
180
|
-
await
|
|
181
|
-
|
|
270
|
+
if (opts.followLinks) await firstVisit(root);
|
|
271
|
+
await walk(root, false);
|
|
272
|
+
return { files: out, unreachable };
|
|
182
273
|
}
|
|
183
274
|
|
|
184
275
|
/**
|
|
@@ -221,6 +312,11 @@ export async function isStable(file: DroppedFile, stabilityMs = 750): Promise<bo
|
|
|
221
312
|
* recursive delete here would take a file the sweep deliberately chose not to ingest — a held
|
|
222
313
|
* duplicate, a failure left in place for the owner to look at — which is precisely the set of
|
|
223
314
|
* files that must survive.
|
|
315
|
+
*
|
|
316
|
+
* 🔴 `lstat`, never `stat`: a symlink is a linked source, and following it would `rmdir` every
|
|
317
|
+
* empty folder on the owner's own drive (collections met exactly this, 2026-10-21). The one thing
|
|
318
|
+
* removed first is Finder's bookkeeping (`.DS_Store`, `._*`): Finder writes one into every folder
|
|
319
|
+
* the owner LOOKS at, and without this a tree they watched go in stays behind as empty shells.
|
|
224
320
|
*/
|
|
225
321
|
export async function pruneEmptyDirs(root: string, dir = root): Promise<number> {
|
|
226
322
|
let removed = 0;
|
|
@@ -233,13 +329,16 @@ export async function pruneEmptyDirs(root: string, dir = root): Promise<number>
|
|
|
233
329
|
for (const name of entries) {
|
|
234
330
|
const full = join(dir, name);
|
|
235
331
|
try {
|
|
236
|
-
if ((await
|
|
332
|
+
if ((await lstat(full)).isDirectory()) removed += await pruneEmptyDirs(root, full);
|
|
237
333
|
} catch {
|
|
238
334
|
/* gone */
|
|
239
335
|
}
|
|
240
336
|
}
|
|
241
337
|
if (dir === root) return removed;
|
|
242
338
|
try {
|
|
339
|
+
const left = await readdir(dir);
|
|
340
|
+
if (left.length > 0 && left.every(isFinderBookkeeping))
|
|
341
|
+
for (const name of left) await rm(join(dir, name)).catch(() => undefined);
|
|
243
342
|
await rmdir(dir);
|
|
244
343
|
return removed + 1;
|
|
245
344
|
} catch {
|
|
@@ -247,6 +346,8 @@ export async function pruneEmptyDirs(root: string, dir = root): Promise<number>
|
|
|
247
346
|
}
|
|
248
347
|
}
|
|
249
348
|
|
|
349
|
+
const isFinderBookkeeping = (name: string): boolean => name === ".DS_Store" || name.startsWith("._");
|
|
350
|
+
|
|
250
351
|
/**
|
|
251
352
|
* What the app supplies: what to do with one file. `uploading(key)` names the binary-server key its
|
|
252
353
|
* bytes stage under, once the handler knows it — station's Uploads page shows that transfer's own
|
|
@@ -325,11 +426,14 @@ async function sweep(
|
|
|
325
426
|
unstable: [],
|
|
326
427
|
bytesRemoved: 0,
|
|
327
428
|
emptyDirsRemoved: 0,
|
|
429
|
+
leftInPlace: 0,
|
|
430
|
+
unreachable: [],
|
|
328
431
|
remaining: 0,
|
|
329
432
|
};
|
|
330
433
|
|
|
331
434
|
reporter?.walking();
|
|
332
|
-
const files = await
|
|
435
|
+
const { files, unreachable } = await walkDropzone(root, opts);
|
|
436
|
+
report.unreachable = unreachable;
|
|
333
437
|
reporter?.found(files.map(as));
|
|
334
438
|
for (const [index, file] of files.entries()) {
|
|
335
439
|
report.scanned++;
|
|
@@ -366,6 +470,12 @@ async function sweep(
|
|
|
366
470
|
else if (outcome.kind === "duplicate") report.duplicate++;
|
|
367
471
|
else report.discarded++;
|
|
368
472
|
if (opts.dryRun) break;
|
|
473
|
+
// Rule 1, for a link: the destination confirming it is not permission to delete the
|
|
474
|
+
// owner's copy on their own drive. Stored, and left where it lives.
|
|
475
|
+
if (file.linked) {
|
|
476
|
+
report.leftInPlace++;
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
369
479
|
try {
|
|
370
480
|
await rm(file.path);
|
|
371
481
|
report.bytesRemoved += file.size;
|
|
@@ -391,7 +501,7 @@ async function sweep(
|
|
|
391
501
|
}
|
|
392
502
|
|
|
393
503
|
if (!opts.dryRun) report.emptyDirsRemoved = await pruneEmptyDirs(root);
|
|
394
|
-
report.remaining = (await scanDropzone(root, { skipDirs: opts.skipDirs })).length;
|
|
504
|
+
report.remaining = (await scanDropzone(root, { skipDirs: opts.skipDirs, skipTopDirs: opts.skipTopDirs })).length;
|
|
395
505
|
return report;
|
|
396
506
|
}
|
|
397
507
|
|
|
@@ -399,6 +509,8 @@ async function sweep(
|
|
|
399
509
|
export const describeSweep = (report: SweepReport): string =>
|
|
400
510
|
`${report.stored} added, ${report.duplicate} already had, ${report.discarded} discarded, ` +
|
|
401
511
|
`${report.held} held, ${report.failed.length} failed, ${report.unstable.length} still copying` +
|
|
512
|
+
(report.leftInPlace > 0 ? ` · ${report.leftInPlace} linked file(s) left in place` : "") +
|
|
513
|
+
(report.unreachable.length > 0 ? ` · ${report.unreachable.length} link(s) not reachable` : "") +
|
|
402
514
|
(report.bytesRemoved > 0
|
|
403
515
|
? ` · ${(report.bytesRemoved / 1024 ** 3).toFixed(2)} GB freed from the queue`
|
|
404
516
|
: "") +
|