-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
377 lines (338 loc) · 9.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import du from "du";
import { filesize } from "filesize";
import { symlinkSync, unlinkSync } from "node:fs";
import { readdir, readlink, rm, stat } from "node:fs/promises";
import { dirname, join, normalize, relative, resolve, sep } from "node:path";
import { parseArgs } from "node:util";
import xmlrpc from "xmlrpc";
function method(methodName, params = []) {
return { methodName, params };
}
class RTorrent {
constructor(rpc) {
const url = new URL(rpc);
const clientCreator =
url.protocol === "https:"
? xmlrpc.createSecureClient
: xmlrpc.createClient;
this.client = clientCreator({
url: url.origin + url.pathname,
basic_auth: { user: url.username, pass: url.password },
});
}
#call(method, ...params) {
return new Promise((resolve, reject) => {
this.client.methodCall(method, params, (err, data) => {
if (err) return reject(err);
return resolve(data);
});
});
}
downloadList() {
return this.#call("download_list");
}
async getTorrent(infoHash) {
const response = await this.#call("system.multicall", [
method("d.directory", [infoHash]),
method("d.message", [infoHash]),
method("d.is_multi_file", [infoHash]),
method("d.name", [infoHash]),
method("t.url", [`${infoHash}:t0`]),
method("d.custom1", [infoHash]),
]);
const [
[directory],
[message],
[isMultiFile],
[name],
[announce],
[custom1],
] = response;
return {
infoHash,
name,
tracker: new URL(announce).hostname,
directory: isMultiFile === "1" ? dirname(directory) : directory,
basePath: isMultiFile === "1" ? directory : join(directory, name),
custom1,
message,
};
}
async removeTorrent(infoHash) {
await this.#call("d.erase", infoHash);
for (let i = 0; i < 5; i++) {
if (!(await this.downloadList()).includes(infoHash)) return;
await new Promise((r) => void setTimeout(r, 100 * Math.pow(2, i)));
}
throw new Error("Failed to remove torrent");
}
}
function printProgress(i, total) {
const ratio = Math.floor((i * 100) / total);
const lastRatio = Math.floor(((i - 1) * 100) / total);
if (ratio > lastRatio) {
console.log(`${ratio}%`);
}
}
async function getChildPaths(dataDir) {
const entries = await readdir(dataDir);
return entries.map((entry) => join(dataDir, entry));
}
async function isDirOrDirSymlink(dirent, child) {
if (dirent.isDirectory()) return true;
if (dirent.isSymbolicLink()) {
try {
const stats = await stat(child);
return stats.isDirectory();
} catch (e) {
return false;
}
}
}
async function getSymbolicLinksRecursive(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
const children = await Promise.all(
dirents.map(async (dirent) => {
const child = join(dir, dirent.name);
if (await isDirOrDirSymlink(dirent, child)) {
return getSymbolicLinksRecursive(child);
} else if (dirent.isSymbolicLink()) {
return [child];
} else {
return [];
}
}),
);
return children.flat();
}
async function getHardlinkedFilesRecursive(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
const children = await Promise.all(
dirents.map(async (dirent) => {
const child = join(dir, dirent.name);
if (await isDirOrDirSymlink(dirent, child)) {
return getHardlinkedFilesRecursive(child);
} else if ((await stat(child)).nlink > 1) {
return [child];
} else {
return [];
}
}),
);
return children.flat();
}
function editSymlink(path, target) {
unlinkSync(path);
symlinkSync(target, path);
}
async function fixSymlinks(
symlinkSourceRoots,
improperSymlinkSegment,
properSymlinkSegment,
fixSymlinks,
) {
if (improperSymlinkSegment && properSymlinkSegment) {
const symlinks = (
await Promise.all(symlinkSourceRoots.map(getSymbolicLinksRecursive))
).flat();
for (const symlink of symlinks) {
const rawLinkTarget = await readlink(symlink);
if (rawLinkTarget.includes(improperSymlinkSegment)) {
const properLinkTarget = relative(
dirname(symlink),
rawLinkTarget.replace(
improperSymlinkSegment,
properSymlinkSegment,
),
);
if (fixSymlinks) {
console.log(
`Fixing improper symlink:\n\t${rawLinkTarget}\n\t${properLinkTarget}`,
);
editSymlink(symlink, properLinkTarget);
} else {
console.log(
`Would fix improper symlink:\n\t${symlink}\n\t${rawLinkTarget}\n\t${properLinkTarget}`,
);
}
}
}
}
}
function getDedupedBasePathsFromFiles(files, dataDirs) {
return files.reduce((acc, filePath) => {
const dataDir = dataDirs.find((d) =>
normalize(filePath).startsWith(resolve(d)),
);
if (dataDir) {
acc.add(
filePath
.split(sep)
.slice(0, dataDir.split(sep).length + 1)
.join(sep),
);
} else {
console.log("Skipping escaping symlink:", filePath);
}
return acc;
}, new Set());
}
async function findSymlinkTargetPaths(dataDirs, symlinkSourceRoots) {
const symlinks = (
await Promise.all(symlinkSourceRoots.map(getSymbolicLinksRecursive))
).flat();
const realPaths = (
await Promise.all(
symlinks.map(async (symlinkPath) => {
try {
const linkTarget = await readlink(symlinkPath);
return [resolve(dirname(symlinkPath), linkTarget)];
} catch (e) {
console.error("skipping broken link:", symlinkPath);
return [];
}
}),
)
).flat();
return getDedupedBasePathsFromFiles(realPaths, dataDirs);
}
async function findHardlinkTargetPaths(dataDirs) {
const allHardlinks = (
await Promise.all(dataDirs.map(getHardlinkedFilesRecursive))
).flat();
return getDedupedBasePathsFromFiles(allHardlinks, dataDirs);
}
function isUnregistered(torrent) {
const message = torrent.message.toLowerCase();
const keywords = [
"unregistered",
"not registered",
"this torrent does not exist",
"trumped",
"infohash not found",
"complete season uploaded",
"torrent not found",
].map((e) => e.toLowerCase()); // just to be safe
return keywords.some((keyword) => message.includes(keyword));
}
async function main() {
const { values: Args } = parseArgs({
options: {
rpc: { type: "string" },
dataDir: { short: "d", type: "string", multiple: true },
fixUnregistered: { type: "boolean" },
fixOrphaned: { type: "boolean" },
symlinkSource: { type: "string", multiple: true, default: [] },
improperSymlinkSegment: { type: "string" },
properSymlinkSegment: { type: "string" },
fixSymlinks: { type: "boolean" },
retainSolelyForSeeding: { type: "boolean" },
fixMissingFiles: { type: "boolean" },
safetyThreshold: { type: "string", default: "1" },
force: { type: "boolean" },
},
});
const rtorrent = new RTorrent(Args.rpc);
const downloadList = await rtorrent.downloadList();
const session = [];
if (
Args.retainSolelyForSeeding ||
Args.fixUnregistered ||
Args.fixMissingFiles
) {
for (const [i, infoHash] of downloadList.entries()) {
printProgress(i, downloadList.length);
const torrent = await rtorrent.getTorrent(infoHash);
if (isUnregistered(torrent)) {
if (Args.fixUnregistered) {
await rtorrent.removeTorrent(infoHash);
console.log("Removed unregistered torrent:", torrent);
} else {
console.log("Would remove unregistered torrent:", torrent);
}
} else if (
torrent.message.trim() ===
"Download registered as completed, but hash check returned unfinished chunks."
) {
if (Args.fixMissingFiles) {
await rtorrent.removeTorrent(infoHash);
console.log("Removed missing files torrent:", torrent);
} else {
console.log("Would remove missing files torrent:", torrent);
}
} else {
session.push(torrent);
}
}
}
const allPaths = (
await Promise.all(Args.dataDir.map(getChildPaths))
).flat();
const pathsInSession = new Set(session.map((e) => e.basePath));
await fixSymlinks(
Args.symlinkSource,
Args.improperSymlinkSegment,
Args.properSymlinkSegment,
Args.fixSymlinks,
);
const pathsHoldingSymlinkTargets = await findSymlinkTargetPaths(
Args.dataDir,
Args.symlinkSource,
);
const pathsHoldingHardlinkTargets = await findHardlinkTargetPaths(
Args.dataDir,
);
const orphanedPaths = allPaths.filter(
(path) =>
!(
// any of these can create a 'hold' on a path
(
(Args.retainSolelyForSeeding && pathsInSession.has(path)) ||
pathsHoldingSymlinkTargets.has(path) ||
pathsHoldingHardlinkTargets.has(path)
)
),
);
if (
Args.fixOrphaned &&
!Args.force &&
(orphanedPaths.length / allPaths.length) * 100 >
Number(Args.safetyThreshold)
) {
const threshold = Number(Args.safetyThreshold);
console.error(orphanedPaths);
throw new Error(
`Orphans to delete (${
orphanedPaths.length
}) exceeded ${threshold}% of total (${allPaths.length}).
To execute, rerun with --force --safetyThreshold ${Math.ceil(
(orphanedPaths.length / allPaths.length) * 100,
)}.`,
);
}
let totalSize = 0;
for (const orphan of orphanedPaths) {
const size = await du(orphan);
totalSize += size;
if (Args.fixOrphaned) {
await rm(orphan, { recursive: true });
console.log("Removed orphan", filesize(size), orphan);
} else {
console.log("Would remove orphan", filesize(size), orphan);
}
for (const torrent of session.filter((t) => t.basePath === orphan)) {
if (Args.fixOrphaned) {
await rtorrent.removeTorrent(torrent.infoHash);
console.log("\tRemoved dangling torrent", torrent);
} else {
console.log("\tWould remove dangling torrent", torrent);
}
}
}
console.log(
`Found ${orphanedPaths.length} orphaned paths totaling ${filesize(
totalSize,
)}`,
);
}
await main();