Give backup album folders distinct names and remove stale entries (closes #103)
check / check (push) Successful in 15s
check / check (push) Successful in 15s
Within a collection's folder, files whose sanitized titles match (ignoring case) each get their file ID added before the extension, and collections whose sanitized names match get their ID added. This repeats until no name, including one with an ID added, matches another, so no symlink or JSON replaces another. Names are chosen across all collections, so a scoped run names folders the same as a full one. Each run first removes symlinks into originals/ that no longer belong to a collection, and the folders quak wrote (a sibling JSON with an album ID) for collections that are gone or renamed. Anything else is left alone; a folder still holding user files keeps its JSON. Model: opus-5-5
This commit is contained in:
+134
-11
@@ -17,7 +17,9 @@
|
||||
// temp-then-rename, so a file that exists is whole and is never re-fetched — an
|
||||
// interrupted run resumes by listing the directory. The derived views hold no
|
||||
// unique state, so they are rebuilt every run; that repairs stale sidecars and
|
||||
// missing or broken symlinks left by an earlier crash.
|
||||
// missing or broken symlinks left by an earlier crash. A rebuild also removes
|
||||
// the symlinks into originals/ that no longer belong to an album, and the
|
||||
// directories of albums that no longer exist.
|
||||
//
|
||||
// Resilience (issue #8): no per-file condition aborts the run. A failed
|
||||
// download or a failed symlink is caught, recorded in `failures.json` with a
|
||||
@@ -34,13 +36,14 @@ import {
|
||||
readdirSync,
|
||||
readFileSync,
|
||||
readlinkSync,
|
||||
rmdirSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
symlinkSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { copyFile, rename, rm } from "node:fs/promises";
|
||||
import { basename, dirname, join, relative } from "node:path";
|
||||
import { basename, dirname, extname, join, relative } from "node:path";
|
||||
|
||||
import { fsyncPath } from "./download/index.js";
|
||||
import { safeExtension, sanitizeFileName } from "./filename.js";
|
||||
@@ -227,6 +230,93 @@ const rebuildSymlink = (linkPath: string, target: string): void => {
|
||||
symlinkSync(target, linkPath);
|
||||
};
|
||||
|
||||
// The on-disk names for the entries of one directory, keyed by ID. Each name
|
||||
// is used as is unless another entry would get the same name, ignoring case
|
||||
// (two names that differ only in case are one entry on a case-insensitive
|
||||
// file system); then every entry sharing it gets ` (<id>)`, before the
|
||||
// extension when `beforeExtension` is set. A name with an ID added can match
|
||||
// another entry's own name (`IMG (6).JPG`), so this repeats until no name is
|
||||
// shared. IDs are stable, so the names are too.
|
||||
const namesByID = (
|
||||
entries: { id: number; name: string }[],
|
||||
beforeExtension: boolean,
|
||||
): Map<number, string> => {
|
||||
const withID = (id: number, name: string): string => {
|
||||
const ext = beforeExtension ? extname(name) : "";
|
||||
const stem = name.slice(0, name.length - ext.length);
|
||||
return `${stem} (${id})${ext}`;
|
||||
};
|
||||
const names = new Map<number, string>();
|
||||
for (const { id, name } of entries) names.set(id, name);
|
||||
const suffixed = new Set<number>();
|
||||
for (;;) {
|
||||
const counts = new Map<string, number>();
|
||||
for (const name of names.values()) {
|
||||
const key = name.toLowerCase();
|
||||
counts.set(key, (counts.get(key) ?? 0) + 1);
|
||||
}
|
||||
let changed = false;
|
||||
for (const { id, name } of entries) {
|
||||
if (suffixed.has(id)) continue;
|
||||
if (counts.get(name.toLowerCase()) === 1) continue;
|
||||
names.set(id, withID(id, name));
|
||||
suffixed.add(id);
|
||||
changed = true;
|
||||
}
|
||||
if (!changed) return names;
|
||||
}
|
||||
};
|
||||
|
||||
// Remove the symlinks in the album directory `dir` that point into
|
||||
// `originalsDir` and are not named in `keep`. Nothing else in the directory
|
||||
// is touched: anything else there was put there by the user.
|
||||
const removeStaleLinks = (
|
||||
dir: string,
|
||||
keep: Set<string>,
|
||||
originalsDir: string,
|
||||
): void => {
|
||||
const target = relative(dir, originalsDir);
|
||||
for (const name of readdirSync(dir)) {
|
||||
if (keep.has(name)) continue;
|
||||
const path = join(dir, name);
|
||||
if (
|
||||
lstatSync(path).isSymbolicLink() &&
|
||||
dirname(readlinkSync(path)) === target
|
||||
) {
|
||||
rmSync(path);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Remove the directories under `collectionsDir` that an earlier run wrote for
|
||||
// an album that is gone or renamed: a directory not named in `current` with a
|
||||
// `<name>.json` beside it holding an album ID, which is what a run writes. Its
|
||||
// symlinks into originals/ are removed; if that leaves it empty, it and its
|
||||
// JSON are deleted, otherwise both stay for what the user put there.
|
||||
const removeStaleAlbumDirs = (
|
||||
collectionsDir: string,
|
||||
current: Set<string>,
|
||||
originalsDir: string,
|
||||
): void => {
|
||||
for (const entry of readdirSync(collectionsDir, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory() || current.has(entry.name)) continue;
|
||||
const jsonPath = join(collectionsDir, `${entry.name}.json`);
|
||||
try {
|
||||
const album = JSON.parse(readFileSync(jsonPath, "utf-8")) as {
|
||||
id?: unknown;
|
||||
};
|
||||
if (typeof album.id !== "number") continue;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const dir = join(collectionsDir, entry.name);
|
||||
removeStaleLinks(dir, new Set(), originalsDir);
|
||||
if (readdirSync(dir).length > 0) continue;
|
||||
rmdirSync(dir);
|
||||
rmSync(jsonPath);
|
||||
}
|
||||
};
|
||||
|
||||
const loadLedger = (path: string): Map<number, FailureEntry> => {
|
||||
const ledger = new Map<number, FailureEntry>();
|
||||
try {
|
||||
@@ -303,9 +393,10 @@ export const runBackup = async (
|
||||
|
||||
// Collections in scope, and the distinct files across them (a file shared
|
||||
// by two albums is one original).
|
||||
const collections = lib
|
||||
.listCollections()
|
||||
.filter((c) => (only ? only.has(c.name) : true));
|
||||
const allCollections = lib.listCollections();
|
||||
const collections = allCollections.filter((c) =>
|
||||
only ? only.has(c.name) : true,
|
||||
);
|
||||
const collectionName = new Map<number, string>();
|
||||
for (const c of collections) collectionName.set(c.id, c.name);
|
||||
|
||||
@@ -406,23 +497,55 @@ export const runBackup = async (
|
||||
}
|
||||
}
|
||||
|
||||
// Then the per-collection symlink trees and JSON.
|
||||
// Then the per-collection symlink trees and JSON. Directory names are
|
||||
// chosen across every album, not just those in scope, so a scoped run
|
||||
// names an album the same as a full one and never takes the directory of
|
||||
// an album it skipped. Stale entries are removed before anything is
|
||||
// rebuilt, so on a case-insensitive file system removing an old name can
|
||||
// never remove the new one.
|
||||
const albumDirNames = namesByID(
|
||||
allCollections.map((c) => ({
|
||||
id: c.id,
|
||||
name: sanitizeFileName(c.name, `collection-${c.id}`),
|
||||
})),
|
||||
false,
|
||||
);
|
||||
try {
|
||||
removeStaleAlbumDirs(
|
||||
collectionsDir,
|
||||
new Set(albumDirNames.values()),
|
||||
originalsDir,
|
||||
);
|
||||
} catch (err) {
|
||||
log(`FAILED removing old album directories: ${errorMessage(err)}`);
|
||||
}
|
||||
|
||||
for (const c of collections) {
|
||||
const colDirName = sanitizeFileName(c.name, `collection-${c.id}`);
|
||||
const colDirName = albumDirNames.get(c.id)!;
|
||||
const colDir = join(collectionsDir, colDirName);
|
||||
mkdirSync(colDir, { recursive: true });
|
||||
|
||||
const files = filesByCollection.get(c.id) ?? [];
|
||||
const linkNames = namesByID(
|
||||
files.map((f) => ({
|
||||
id: f.id,
|
||||
name: sanitizeFileName(f.metadata.title, `file-${f.id}`),
|
||||
})),
|
||||
true,
|
||||
);
|
||||
try {
|
||||
removeStaleLinks(colDir, new Set(linkNames.values()), originalsDir);
|
||||
} catch (err) {
|
||||
log(`FAILED removing old links in ${c.name}: ${errorMessage(err)}`);
|
||||
}
|
||||
|
||||
const metaFiles: { id: number; metadata: EnteFile["metadata"] }[] = [];
|
||||
for (const file of files) {
|
||||
metaFiles.push({ id: file.id, metadata: file.metadata });
|
||||
if (!includeOriginals) continue;
|
||||
const orig = join(originalsDir, originalName(file));
|
||||
if (!isPresent(orig)) continue;
|
||||
const linkName = sanitizeFileName(
|
||||
file.metadata.title,
|
||||
`file-${file.id}`,
|
||||
);
|
||||
const linkName = linkNames.get(file.id)!;
|
||||
const linkPath = join(colDir, linkName);
|
||||
try {
|
||||
rebuildSymlink(linkPath, relative(colDir, orig));
|
||||
|
||||
Reference in New Issue
Block a user