fix(setup): preserve the selected destination during configuration publication
Replace configuration-specific write-file-atomic delegation with a verified input snapshot, exclusive private staging, and an explicit canonical-parent destination. Existing files publish by rename; first-run files use an exclusive link so a newly created destination is retained. Both consumers read back the publication owner's selected path. Retain descriptor and staging cleanup capabilities through recovery and host retry. Preserve mode and ownership, remove temporary permission handoffs, and use positional restoration writes. Keep UTF-8 diagnostics with failed cleanup. Validation: 284 focused tests passed, one skipped across 14 files; check:fast, format:check, docs:check (including five consistency tests), two source reviews, and 12 completed isolated public CLI setup/uninstall cost controls passed. Native Windows/Linux, real editor concurrency, and providers were not verified. Final-check/publication races and same-inode concurrent writers remain unexcluded. Fixes #4904
This commit is contained in:
1 parent
2fb5e204a3
commit
d0253c99d6
12 files changed
+771
-215
No files matched your search
@@ -0,0 +1,113 @@
|
||||
import {
|
||||
ArtifactResourceRetryFailure,
|
||||
type ArtifactResourceOwner,
|
||||
type ArtifactResourceScope,
|
||||
} from "../artifacts/ArtifactResourceScope.js";
|
||||
import type { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js";
|
||||
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
||||
import type { AnalysisCleanupObservation } from "../domain/analysisErrorBase.js";
|
||||
import type { RegularFileOperationOutcome } from "../filesystem/OwnedRegularFile.js";
|
||||
import {
|
||||
RegularFileCleanupFailure,
|
||||
retryRegularFileCleanupFailures,
|
||||
} from "./RegularFileRead.js";
|
||||
|
||||
/** Completion and exact staging owner retained when publication cleanup fails. */
|
||||
export class ClientConfigurationPublicationFailure<
|
||||
Value = unknown,
|
||||
> extends ArtifactResourceRetryFailure {
|
||||
constructor(
|
||||
readonly path: string,
|
||||
readonly outcome: RegularFileOperationOutcome<Value>,
|
||||
readonly cleanupCause: unknown,
|
||||
readonly owner: Extract<
|
||||
ArtifactResourceOwner,
|
||||
{ readonly kind: "staged-output" }
|
||||
>,
|
||||
readonly cleanupLease: AnalysisCleanupLease,
|
||||
) {
|
||||
super(
|
||||
[{ owner, lease: cleanupLease }],
|
||||
"Configuration publication cleanup remains incomplete",
|
||||
{
|
||||
cause: new AggregateError(
|
||||
[...(outcome.kind === "failed" ? [outcome.cause] : []), cleanupCause],
|
||||
"Configuration operation and staging cleanup failures",
|
||||
),
|
||||
cleanup: ArtifactReaderFailure.cleanupObservation(
|
||||
cleanupCause,
|
||||
owner.resource,
|
||||
),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
type ClientConfigurationCleanupFailure =
|
||||
| RegularFileCleanupFailure
|
||||
| ClientConfigurationPublicationFailure;
|
||||
|
||||
export const isClientConfigurationCleanupFailure = (
|
||||
cause: unknown,
|
||||
): cause is ClientConfigurationCleanupFailure =>
|
||||
cause instanceof RegularFileCleanupFailure ||
|
||||
cause instanceof ClientConfigurationPublicationFailure;
|
||||
|
||||
/** Adopt only capabilities carried by explicit operation outcomes, never opaque causes. */
|
||||
export const retryClientConfigurationCleanup = async (
|
||||
failure: ClientConfigurationCleanupFailure,
|
||||
resources: ArtifactResourceScope,
|
||||
): Promise<{
|
||||
readonly primary: unknown;
|
||||
readonly failures: readonly RegularFileCleanupFailure[];
|
||||
readonly cleanup?: AnalysisCleanupObservation;
|
||||
}> => {
|
||||
const failures: RegularFileCleanupFailure[] = [];
|
||||
const observations: AnalysisCleanupObservation[] = [];
|
||||
let current = failure;
|
||||
let primary: unknown;
|
||||
for (;;) {
|
||||
let nested: unknown;
|
||||
if (current instanceof RegularFileCleanupFailure) {
|
||||
const retried = await retryRegularFileCleanupFailures(current, resources);
|
||||
failures.push(...retried.failures);
|
||||
if (retried.cleanup !== undefined) observations.push(retried.cleanup);
|
||||
primary = retried.primary;
|
||||
const last = retried.failures.at(-1)!;
|
||||
nested = last.outcome.kind === "failed" ? last.outcome.cause : undefined;
|
||||
} else {
|
||||
const released = await resources.release({
|
||||
kind: "lease",
|
||||
resource: current.owner.resource,
|
||||
lease: current.cleanupLease,
|
||||
});
|
||||
if (released.kind === "failed")
|
||||
observations.push(
|
||||
ArtifactReaderFailure.cleanupObservation(
|
||||
released.cause,
|
||||
current.owner.resource,
|
||||
),
|
||||
);
|
||||
primary =
|
||||
current.outcome.kind === "failed"
|
||||
? current.outcome.cause
|
||||
: current.cleanupCause;
|
||||
nested =
|
||||
current.outcome.kind === "failed" ? current.outcome.cause : undefined;
|
||||
}
|
||||
if (!isClientConfigurationCleanupFailure(nested)) break;
|
||||
current = nested;
|
||||
}
|
||||
return {
|
||||
primary,
|
||||
failures,
|
||||
...(observations.length === 0
|
||||
? {}
|
||||
: {
|
||||
cleanup: {
|
||||
reason: observations.map(({ reason }) => reason).join("; "),
|
||||
resources: observations.flatMap(({ resources }) => resources),
|
||||
},
|
||||
}),
|
||||
};
|
||||
};
|
||||
@@ -1,13 +1,25 @@
|
||||
import { constants } from "node:fs";
|
||||
import { lstat, open, writeFile, type FileHandle } from "node:fs/promises";
|
||||
import writeFileAtomic from "write-file-atomic";
|
||||
import { constants, type Stats } from "node:fs";
|
||||
import {
|
||||
link,
|
||||
lstat,
|
||||
mkdtemp,
|
||||
open,
|
||||
realpath,
|
||||
rename,
|
||||
type FileHandle,
|
||||
} from "node:fs/promises";
|
||||
import { basename, dirname, join, sep } from "node:path";
|
||||
|
||||
import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js";
|
||||
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
|
||||
import { withOwnedFileHandle } from "../filesystem/OwnedRegularFile.js";
|
||||
import { OwnedStagedOutput } from "../filesystem/OwnedStagedOutput.js";
|
||||
import {
|
||||
RegularFileChangedError,
|
||||
sameRegularFileState,
|
||||
} from "../filesystem/RegularFile.js";
|
||||
import type { RegularFileOperationOutcome } from "../filesystem/OwnedRegularFile.js";
|
||||
import { readRegularFileTextSnapshot } from "./RegularFileRead.js";
|
||||
import { ClientConfigurationPublicationFailure } from "./ClientConfigurationCleanup.js";
|
||||
|
||||
/** The exact staged file retained through publication, verification and recovery. */
|
||||
export class ClientConfigurationPublication {
|
||||
@@ -15,83 +27,86 @@ export class ClientConfigurationPublication {
|
||||
readonly path: string,
|
||||
private readonly contents: string,
|
||||
private readonly owner: OwnedFileHandle,
|
||||
private readonly staging: OwnedStagedOutput,
|
||||
private readonly cleanup: AnalysisCleanupLease,
|
||||
) {}
|
||||
|
||||
static async write(
|
||||
path: string,
|
||||
contents: string,
|
||||
previous: string | undefined,
|
||||
mode?: number,
|
||||
): Promise<ClientConfigurationPublication> {
|
||||
let owner: OwnedFileHandle | undefined;
|
||||
const parent = await realpath(dirname(path));
|
||||
const destination = join(parent, basename(path));
|
||||
const input = await selectedConfiguration(destination, previous);
|
||||
const directory = await mkdtemp(`${parent}${sep}.rea-config-`);
|
||||
const staging = await OwnedStagedOutput.create(directory);
|
||||
const cleanup = new AnalysisCleanupLease(
|
||||
() => staging.close(),
|
||||
[directory],
|
||||
);
|
||||
try {
|
||||
await writeFileAtomic(path, contents, {
|
||||
encoding: "utf8",
|
||||
...(mode === undefined ? {} : { mode }),
|
||||
tmpfileCreated: async (stagedPath) => {
|
||||
const writableFlags =
|
||||
constants.O_RDWR |
|
||||
const stagedPath = `${directory}${sep}content`;
|
||||
const owner = new OwnedFileHandle(
|
||||
await open(
|
||||
stagedPath,
|
||||
constants.O_RDWR |
|
||||
constants.O_CREAT |
|
||||
constants.O_EXCL |
|
||||
(constants.O_NONBLOCK ?? 0) |
|
||||
(constants.O_NOFOLLOW ?? 0);
|
||||
if (((await lstat(stagedPath)).mode & 0o200) !== 0) {
|
||||
owner = new OwnedFileHandle(await open(stagedPath, writableFlags));
|
||||
} else {
|
||||
// The atomic writer already owns a writable creation descriptor.
|
||||
// Retain ours before restoring the staged file's final read-only mode.
|
||||
const readable = new StagedConfigurationPermissions(
|
||||
await open(
|
||||
stagedPath,
|
||||
constants.O_RDONLY |
|
||||
(constants.O_NONBLOCK ?? 0) |
|
||||
(constants.O_NOFOLLOW ?? 0),
|
||||
),
|
||||
);
|
||||
await withOwnedFileHandle(
|
||||
stagedPath,
|
||||
readable,
|
||||
{},
|
||||
async (handle) => {
|
||||
const staged = await handle.stat();
|
||||
if (!staged.isFile())
|
||||
throw new Error("Staged configuration is not a regular file");
|
||||
readable.restoreMode(staged.mode & 0o7777);
|
||||
await handle.chmod((staged.mode & 0o7777) | 0o600);
|
||||
owner = new OwnedFileHandle(
|
||||
await open(stagedPath, writableFlags),
|
||||
);
|
||||
const writable = await owner.handle.stat();
|
||||
if (
|
||||
staged.ino === 0 ||
|
||||
!writable.isFile() ||
|
||||
writable.dev !== staged.dev ||
|
||||
writable.ino !== staged.ino
|
||||
)
|
||||
throw new RegularFileChangedError(stagedPath);
|
||||
},
|
||||
);
|
||||
}
|
||||
if (owner === undefined)
|
||||
throw new Error("Staged configuration has no file owner");
|
||||
if (!(await owner.handle.stat()).isFile())
|
||||
throw new Error("Staged configuration is not a regular file");
|
||||
},
|
||||
});
|
||||
if (owner === undefined)
|
||||
throw new Error("Atomic configuration publication has no file owner");
|
||||
return new ClientConfigurationPublication(path, contents, owner);
|
||||
(constants.O_NOFOLLOW ?? 0),
|
||||
0o600,
|
||||
),
|
||||
);
|
||||
staging.setFile(owner);
|
||||
await owner.handle.writeFile(contents, { encoding: "utf8" });
|
||||
const created = await owner.handle.stat();
|
||||
if (
|
||||
input !== undefined &&
|
||||
(input.uid !== created.uid || input.gid !== created.gid)
|
||||
)
|
||||
await owner.handle.chown(input.uid, input.gid);
|
||||
await owner.handle.chmod(mode ?? (input?.mode ?? 0o600) & 0o7777);
|
||||
await owner.handle.sync();
|
||||
if ((await owner.handle.stat()).ino === 0)
|
||||
throw new RegularFileChangedError(stagedPath);
|
||||
await staging.assertReadyForPublication(stagedPath);
|
||||
await assertSelectedConfiguration(destination, input);
|
||||
if (input === undefined) {
|
||||
// An exclusive link cannot overwrite a file created after absence was checked.
|
||||
await link(stagedPath, destination);
|
||||
} else {
|
||||
// The destination is explicit: rename never follows its final symlink.
|
||||
await rename(stagedPath, destination);
|
||||
staging.recordPublishedRename();
|
||||
}
|
||||
return new ClientConfigurationPublication(
|
||||
destination,
|
||||
contents,
|
||||
owner,
|
||||
staging,
|
||||
cleanup,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (owner === undefined) throw cause;
|
||||
return withOwnedFileHandle(path, owner, {}, async () => {
|
||||
throw cause;
|
||||
});
|
||||
await releaseStaging(path, staging, cleanup, { kind: "failed", cause });
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
|
||||
/** Keep the publication descriptor owned until the complete transaction ends. */
|
||||
verify<Value>(operation: () => Promise<Value>): Promise<Value> {
|
||||
return withOwnedFileHandle(this.path, this.owner, {}, operation);
|
||||
/** Keep all publication resources owned through the complete operation. */
|
||||
async verify<Value>(operation: () => Promise<Value>): Promise<Value> {
|
||||
let outcome: RegularFileOperationOutcome<Value>;
|
||||
try {
|
||||
outcome = { kind: "completed", value: await operation() };
|
||||
} catch (cause: unknown) {
|
||||
outcome = { kind: "failed", cause };
|
||||
}
|
||||
await releaseStaging(this.path, this.staging, this.cleanup, outcome);
|
||||
if (outcome.kind === "failed") throw outcome.cause;
|
||||
return outcome.value;
|
||||
}
|
||||
|
||||
/** Readback cannot establish a successful transaction through a replaced name. */
|
||||
async assertCurrent(): Promise<void> {
|
||||
const owned = await this.owner.handle.stat();
|
||||
const named = await lstat(this.path);
|
||||
@@ -100,14 +115,10 @@ export class ClientConfigurationPublication {
|
||||
named.isSymbolicLink() ||
|
||||
!sameRegularFileState(owned, named)
|
||||
)
|
||||
throw new Error(
|
||||
"Published configuration path no longer names its owned file",
|
||||
);
|
||||
throw new RegularFileChangedError(this.path);
|
||||
}
|
||||
|
||||
/** Require observed published bytes and restore through the owned descriptor.
|
||||
* State checks do not exclude a concurrent writer to the same inode.
|
||||
*/
|
||||
/** State checks do not exclude a concurrent writer to the same inode. */
|
||||
async restore(original: string | undefined): Promise<void> {
|
||||
if (original === undefined)
|
||||
throw new Error(
|
||||
@@ -122,18 +133,25 @@ export class ClientConfigurationPublication {
|
||||
named.isSymbolicLink() ||
|
||||
!sameRegularFileState(before, named) ||
|
||||
!(await this.#hasPublishedBytes(handle)) ||
|
||||
!sameRegularFileState(before, await handle.stat())
|
||||
!sameRegularFileState(before, await handle.stat()) ||
|
||||
!sameRegularFileState(before, await lstat(this.path))
|
||||
)
|
||||
throw new Error(
|
||||
"Published configuration changed; recovery requires the retained backup",
|
||||
throw new RegularFileChangedError(this.path);
|
||||
const bytes = Buffer.from(original, "utf8");
|
||||
for (let position = 0; position < bytes.length;) {
|
||||
const { bytesWritten } = await handle.write(
|
||||
bytes,
|
||||
position,
|
||||
bytes.length - position,
|
||||
position,
|
||||
);
|
||||
// Recovery writes through the retained descriptor, never through the name.
|
||||
if (!sameRegularFileState(before, await lstat(this.path)))
|
||||
throw new Error("Published configuration path changed before recovery");
|
||||
await writeFile(handle, original, { encoding: "utf8" });
|
||||
await handle.truncate(Buffer.byteLength(original, "utf8"));
|
||||
if (bytesWritten === 0)
|
||||
throw new Error("Configuration restoration made no write progress");
|
||||
position += bytesWritten;
|
||||
}
|
||||
await handle.truncate(bytes.length);
|
||||
if (!sameRegularFileState(await handle.stat(), await lstat(this.path)))
|
||||
throw new Error("Configuration path changed during recovery");
|
||||
throw new RegularFileChangedError(this.path);
|
||||
}
|
||||
|
||||
async #hasPublishedBytes(handle: FileHandle): Promise<boolean> {
|
||||
@@ -160,27 +178,60 @@ export class ClientConfigurationPublication {
|
||||
}
|
||||
}
|
||||
|
||||
/** Restore temporary staging permissions before releasing their exact descriptor. */
|
||||
class StagedConfigurationPermissions extends OwnedFileHandle {
|
||||
#mode: number | undefined;
|
||||
#restoring: Promise<void> | undefined;
|
||||
|
||||
restoreMode(mode: number): void {
|
||||
this.#mode = mode;
|
||||
const selectedConfiguration = async (
|
||||
path: string,
|
||||
previous: string | undefined,
|
||||
): Promise<Stats | undefined> => {
|
||||
let selected:
|
||||
| Awaited<ReturnType<typeof readRegularFileTextSnapshot>>
|
||||
| undefined;
|
||||
try {
|
||||
selected = await readRegularFileTextSnapshot(path, { symlinks: "reject" });
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof Error && "code" in cause && cause.code === "ENOENT"))
|
||||
throw cause;
|
||||
}
|
||||
if (selected?.text !== previous || selected?.state.ino === 0)
|
||||
throw new RegularFileChangedError(path);
|
||||
return selected?.state;
|
||||
};
|
||||
|
||||
override async close(): Promise<void> {
|
||||
if (this.#mode !== undefined) {
|
||||
this.#restoring ??= this.handle
|
||||
.chmod(this.#mode)
|
||||
.then(() => {
|
||||
this.#mode = undefined;
|
||||
})
|
||||
.finally(() => {
|
||||
this.#restoring = undefined;
|
||||
});
|
||||
await this.#restoring;
|
||||
}
|
||||
await super.close();
|
||||
const assertSelectedConfiguration = async (
|
||||
path: string,
|
||||
selected: Stats | undefined,
|
||||
): Promise<void> => {
|
||||
let current: Stats | undefined;
|
||||
try {
|
||||
current = await lstat(path);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof Error && "code" in cause && cause.code === "ENOENT"))
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
if (
|
||||
selected === undefined
|
||||
? current !== undefined
|
||||
: current === undefined ||
|
||||
current.isSymbolicLink() ||
|
||||
!sameRegularFileState(selected, current)
|
||||
)
|
||||
throw new RegularFileChangedError(path);
|
||||
};
|
||||
|
||||
const releaseStaging = async <Value>(
|
||||
path: string,
|
||||
staging: OwnedStagedOutput,
|
||||
cleanup: AnalysisCleanupLease,
|
||||
outcome: RegularFileOperationOutcome<Value>,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await cleanup.release();
|
||||
} catch (cause: unknown) {
|
||||
throw new ClientConfigurationPublicationFailure(
|
||||
path,
|
||||
outcome,
|
||||
cause,
|
||||
{ kind: "staged-output", resource: staging.directory, output: staging },
|
||||
cleanup,
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -188,25 +188,36 @@ export const readRegularFileText = (
|
||||
path: string,
|
||||
options: RegularFileReadOptions = {},
|
||||
): Promise<string> =>
|
||||
readRegularFileContent(path, (bytes) => decodeFileText(path, bytes), options);
|
||||
|
||||
/** Retain the read owner's verified file state together with its decoded text. */
|
||||
export const readRegularFileTextSnapshot = (
|
||||
path: string,
|
||||
options: RegularFileReadOptions = {},
|
||||
): Promise<{ readonly text: string; readonly state: Stats }> =>
|
||||
readRegularFileContent(
|
||||
path,
|
||||
(bytes) => {
|
||||
try {
|
||||
return new TextDecoder("utf-8", {
|
||||
fatal: true,
|
||||
ignoreBOM: true,
|
||||
}).decode(bytes);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof TypeError)) throw cause;
|
||||
throw new InvalidUtf8FileError(path, cause);
|
||||
}
|
||||
},
|
||||
(bytes, state) => ({
|
||||
text: decodeFileText(path, bytes),
|
||||
state,
|
||||
}),
|
||||
options,
|
||||
);
|
||||
|
||||
const decodeFileText = (path: string, bytes: Buffer): string => {
|
||||
try {
|
||||
return new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
|
||||
bytes,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof TypeError)) throw cause;
|
||||
throw new InvalidUtf8FileError(path, cause);
|
||||
}
|
||||
};
|
||||
|
||||
const readRegularFileContent = <Value>(
|
||||
path: string,
|
||||
decode: (bytes: Buffer) => Value,
|
||||
decode: (bytes: Buffer, state: Stats) => Value,
|
||||
options: RegularFileReadOptions,
|
||||
): Promise<Value> =>
|
||||
withRegularFile(
|
||||
@@ -219,7 +230,7 @@ const readRegularFileContent = <Value>(
|
||||
);
|
||||
if (bytes === undefined || bytes.length !== stats.size)
|
||||
throw new RegularFileChangedError(path);
|
||||
return decode(bytes);
|
||||
return decode(bytes, stats);
|
||||
},
|
||||
options,
|
||||
);
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
import { dshDocument, DshCordisDocument } from "./DshCordisPatch.js";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { dirname } from "node:path";
|
||||
import { isClientConfigurationCleanupFailure } from "./ClientConfigurationCleanup.js";
|
||||
import { ClientConfigurationPublication } from "./ClientConfigurationPublication.js";
|
||||
import {
|
||||
clientRegistrationEnvironment,
|
||||
@@ -304,10 +305,11 @@ const persistClientConfiguration = async ({
|
||||
...(legacyPath === undefined ? [] : [legacyPath]),
|
||||
...(enableOmp ? [[OMP_DISABLED_SERVERS_KEY]] : []),
|
||||
]),
|
||||
original,
|
||||
0o600,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof RegularFileCleanupFailure) throw cause;
|
||||
if (isClientConfigurationCleanupFailure(cause)) throw cause;
|
||||
// Write failure is reported by the status reason.
|
||||
void cause;
|
||||
return { status: "failed", reason: "write" };
|
||||
@@ -315,7 +317,7 @@ const persistClientConfiguration = async ({
|
||||
return publication.verify(async () => {
|
||||
try {
|
||||
const readback = parseClientConfiguration(
|
||||
await readRegularFileText(transactionPath),
|
||||
await readRegularFileText(publication.path),
|
||||
format,
|
||||
);
|
||||
await publication.assertCurrent();
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
isClientConfigurationCleanupFailure,
|
||||
retryClientConfigurationCleanup,
|
||||
} from "./ClientConfigurationCleanup.js";
|
||||
import { access } from "node:fs/promises";
|
||||
import { resolve } from "node:path";
|
||||
|
||||
@@ -43,10 +47,7 @@ import {
|
||||
} from "./SetupClientConfiguration.js";
|
||||
import { failedConfigurationMessage } from "./SetupClients.js";
|
||||
import { setupInstallFailure } from "./SetupInstallFailure.js";
|
||||
import {
|
||||
RegularFileCleanupFailure,
|
||||
retryRegularFileCleanupFailures,
|
||||
} from "./RegularFileRead.js";
|
||||
import { RegularFileCleanupFailure } from "./RegularFileRead.js";
|
||||
import {
|
||||
explicitSetupRegistrationEnvironment,
|
||||
providerRegistrationEnvironment,
|
||||
@@ -403,8 +404,8 @@ const runClientConfigurationOperation = async <Value>(
|
||||
try {
|
||||
return await operation();
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof RegularFileCleanupFailure)) throw cause;
|
||||
const cleanup = await retryRegularFileCleanupFailures(cause, resources);
|
||||
if (!isClientConfigurationCleanupFailure(cause)) throw cause;
|
||||
const cleanup = await retryClientConfigurationCleanup(cause, resources);
|
||||
const rollback = cleanup.failures.find(
|
||||
(failure) =>
|
||||
failure instanceof ClientConfigurationCleanupRollbackFailure,
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
isClientConfigurationCleanupFailure,
|
||||
retryClientConfigurationCleanup,
|
||||
} from "./ClientConfigurationCleanup.js";
|
||||
import {
|
||||
ClientConfigurationShapeError,
|
||||
parseClientConfiguration,
|
||||
@@ -34,7 +38,6 @@ import {
|
||||
InvalidUtf8FileError,
|
||||
RegularFileCleanupFailure,
|
||||
readRegularFileText,
|
||||
retryRegularFileCleanupFailures,
|
||||
} from "./RegularFileRead.js";
|
||||
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
||||
import {
|
||||
@@ -66,8 +69,12 @@ export interface UninstallFileSystem {
|
||||
publishConfiguration(
|
||||
path: string,
|
||||
contents: string,
|
||||
original: string,
|
||||
): Promise<
|
||||
Pick<ClientConfigurationPublication, "verify" | "assertCurrent" | "restore">
|
||||
Pick<
|
||||
ClientConfigurationPublication,
|
||||
"path" | "verify" | "assertCurrent" | "restore"
|
||||
>
|
||||
>;
|
||||
stat(path: string): Promise<ManagedPathStats>;
|
||||
realpath?(path: string): Promise<string>;
|
||||
@@ -83,8 +90,8 @@ const systemFileSystem = (
|
||||
readRegularText: (path) => readSkillFile(path, resources),
|
||||
copy: (source, destination) =>
|
||||
copyFile(source, destination, fsConstants.COPYFILE_EXCL),
|
||||
publishConfiguration: (path, contents) =>
|
||||
ClientConfigurationPublication.write(path, contents),
|
||||
publishConfiguration: (path, contents, original) =>
|
||||
ClientConfigurationPublication.write(path, contents, original),
|
||||
stat: (path) => lstat(path),
|
||||
realpath: (path) => realpath(path),
|
||||
remove: (path, recursive = true) => rm(path, { recursive }),
|
||||
@@ -389,13 +396,15 @@ const readClientConfiguration = async (
|
||||
try {
|
||||
original = await fileSystem.readText(transactionPath);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof RegularFileCleanupFailure) {
|
||||
await retryRegularFileCleanupFailures(cause, resources);
|
||||
if (isClientConfigurationCleanupFailure(cause)) {
|
||||
const cleanup = await retryClientConfigurationCleanup(cause, resources);
|
||||
return itemRead(
|
||||
item(
|
||||
client.name,
|
||||
"failed",
|
||||
"Configuration could not be read or its resources could not be released. Check file permissions, then rerun uninstall.",
|
||||
cleanup.primary instanceof InvalidUtf8FileError
|
||||
? "Configuration is not valid UTF-8 and was not changed; its resources could not be released. Repair it, then rerun uninstall."
|
||||
: "Configuration could not be read or its resources could not be released. Check file permissions, then rerun uninstall.",
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -570,17 +579,18 @@ const removeClient = async (
|
||||
original,
|
||||
editedPaths,
|
||||
),
|
||||
original,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof RegularFileCleanupFailure)
|
||||
await retryRegularFileCleanupFailures(cause, resources);
|
||||
if (isClientConfigurationCleanupFailure(cause))
|
||||
await retryClientConfigurationCleanup(cause, resources);
|
||||
return manualRecovery();
|
||||
}
|
||||
try {
|
||||
return await publication.verify(async () => {
|
||||
try {
|
||||
const readback = parseClientConfiguration(
|
||||
await fileSystem.readText(transactionPath),
|
||||
await fileSystem.readText(publication.path),
|
||||
client.format,
|
||||
);
|
||||
await publication.assertCurrent();
|
||||
@@ -597,8 +607,8 @@ const removeClient = async (
|
||||
`Removed registration from ${client.configPath}.`,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof RegularFileCleanupFailure)
|
||||
await retryRegularFileCleanupFailures(cause, resources);
|
||||
if (isClientConfigurationCleanupFailure(cause))
|
||||
await retryClientConfigurationCleanup(cause, resources);
|
||||
try {
|
||||
await publication.restore(original);
|
||||
} catch {
|
||||
@@ -612,8 +622,8 @@ const removeClient = async (
|
||||
}
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
if (cause instanceof RegularFileCleanupFailure)
|
||||
await retryRegularFileCleanupFailures(cause, resources);
|
||||
if (isClientConfigurationCleanupFailure(cause))
|
||||
await retryClientConfigurationCleanup(cause, resources);
|
||||
return item(
|
||||
client.name,
|
||||
"failed",
|
||||
|
||||
@@ -52,9 +52,11 @@ export class OwnedStagedOutput {
|
||||
this.#file = file;
|
||||
}
|
||||
|
||||
/** Verify that the path still names the file its descriptor observed at close. */
|
||||
/** Verify the staged name against its descriptor after the last write. */
|
||||
async assertReadyForPublication(content: string): Promise<void> {
|
||||
await this.#verifiedDirectory();
|
||||
if (this.#file !== undefined && !this.#fileClosed)
|
||||
await this.#observeFileIdentity();
|
||||
if (this.#fileIdentity === undefined)
|
||||
throw new Error("Cannot verify staged file identity", {
|
||||
cause: this.#identityFailure,
|
||||
@@ -72,6 +74,14 @@ export class OwnedStagedOutput {
|
||||
async closeFile(): Promise<void> {
|
||||
const file = this.#file;
|
||||
if (file === undefined || this.#fileClosed) return;
|
||||
await this.#observeFileIdentity();
|
||||
await file.close();
|
||||
this.#fileClosed = true;
|
||||
}
|
||||
|
||||
async #observeFileIdentity(): Promise<void> {
|
||||
const file = this.#file;
|
||||
if (file === undefined) return;
|
||||
// FAT and exFAT assign a file ID when the first write allocates storage,
|
||||
// so only the descriptor's final identity can prove the staged pathname.
|
||||
try {
|
||||
@@ -81,8 +91,6 @@ export class OwnedStagedOutput {
|
||||
this.#fileIdentity = undefined;
|
||||
this.#identityFailure = cause;
|
||||
}
|
||||
await file.close();
|
||||
this.#fileClosed = true;
|
||||
}
|
||||
|
||||
/** Record that the staged file moved to the published destination by rename. */
|
||||
|
||||
Reference in new issue
Block a user