Files
reverse-engineering-anything/src/application/ClientRegistrationBackup.ts
T
Vincent Koc 95ac5084cc refactor(setup): share missing-file checks across setup and uninstall (#15789)
* refactor(setup): share missing-file checks across setup and uninstall

* test(clients): assert the scoped catalog mismatch
2026-12-02 16:48:52 +08:00

92 lines
2.8 KiB
TypeScript

import { chmodSync, lstatSync } from "node:fs";
import { readFile, writeFile } from "node:fs/promises";
import {
preserveCurrentRecoveryBackup,
type RecoveryBackupPreservationFailure,
type RecoveryBackupPreservationOptions,
} from "./SetupRecoveryBackup.js";
import { err, ok, type Result } from "../domain/result.js";
import { reportedSystemCode } from "../kernel/reportedSystemCode.js";
import { errorMessage } from "../kernel/errorMessage.js";
const restrictExistingBackup = (
backupPath: string,
): Result<void, RecoveryBackupPreservationFailure> => {
let existing: ReturnType<typeof lstatSync>;
try {
existing = lstatSync(backupPath);
} catch (cause: unknown) {
if (reportedSystemCode(cause) === "ENOENT") return ok(undefined);
return err({
kind: "unreadable",
path: backupPath,
detail: `The existing client configuration backup could not be checked: ${errorMessage(cause)}`,
});
}
if (!existing.isFile())
return err({
kind: "occupied",
path: backupPath,
detail: "The existing client configuration backup is not a regular file.",
});
try {
chmodSync(backupPath, 0o600);
} catch (cause: unknown) {
return err({
kind: "unreadable",
path: backupPath,
detail: `The existing client configuration backup permissions could not be secured: ${errorMessage(cause)}`,
});
}
return ok(undefined);
};
const copyPrivateBackup = async (
sourcePath: string,
backupPath: string,
): Promise<void> => {
await writeFile(backupPath, await readFile(sourcePath), {
flag: "wx",
mode: 0o600,
});
};
/**
* Preserve a configuration's original bytes in an owner-only (0600) backup,
* restricting any backup already selected for them, so a private profile's
* contents never sit in a more permissive copy beside the requested path.
*/
export const preservePrivateRecoveryBackup = async (
firstBackupPath: string,
source: string,
original: string,
resources?: RecoveryBackupPreservationOptions["resources"],
): Promise<Result<string, RecoveryBackupPreservationFailure>> => {
const firstRestricted = restrictExistingBackup(firstBackupPath);
if (!firstRestricted.ok) return firstRestricted;
const preserved = await preserveCurrentRecoveryBackup(
firstBackupPath,
source,
original,
{
copy: copyPrivateBackup,
...(resources === undefined ? {} : { resources }),
},
);
if (!preserved.ok) return preserved;
const selectedRestricted = restrictExistingBackup(preserved.value);
if (!selectedRestricted.ok) return selectedRestricted;
return ok(preserved.value);
};
export const writeClientRegistrationBackup = (
settingsPath: string,
settingsText: string,
): Promise<Result<string, RecoveryBackupPreservationFailure>> =>
preservePrivateRecoveryBackup(
`${settingsPath}.rea.backup`,
settingsPath,
settingsText,
);