Skip to content
Merged
67 changes: 67 additions & 0 deletions apps/server/src/storageCleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3924,6 +3924,36 @@ describe("storage cleanup reports and local file policies", () => {
}),
),
);
it.live.each([
{ kind: "logs", name: "server.log.1", label: "rotated logs" },
{ kind: "browser-artifacts", name: "capture.png", label: "browser artifacts" },
] as const)("skips dangling symlinks during $kind cleanup", ({ kind, name, label }) =>
runCleanupTest(
Effect.gen(function* () {
const { service, fs, config, enableFiles, disable } = yield* cleanupFixture();
disable();
enableFiles();
const directory = kind === "logs" ? `${config.logsDir}/nested` : config.browserArtifactsDir;
yield* fs.makeDirectory(directory, { recursive: true });
const expired = `${directory}/${name}`;
const dangling = `${directory}/00-dangling-${name}`;
const linked = `${directory}/01-linked-${name}`;
yield* fs.symlink("missing", dangling);
yield* fs.symlink(directory, linked);
yield* fs.writeFileString(expired, "expired");
yield* fs.utimes(expired, 1, 1);

const report = yield* service.runNow;
expect(report.entries.filter((entry) => entry.kind === kind)).toMatchObject([
{ outcome: "removed", reason: `Removed 1 ${label}`, files: 1, bytes: 7 },
]);
expect((yield* fs.readDirectory(directory)).toSorted()).toEqual([
`00-dangling-${name}`,
`01-linked-${name}`,
]);
}),
),
);
it.live.each([
{ linked: "home", outcome: "removed", reason: "Removed 1 rotated logs" },
{ linked: "logs folder", outcome: "kept", reason: "No expired files" },
Expand Down Expand Up @@ -3960,6 +3990,43 @@ describe("storage cleanup reports and local file policies", () => {
}),
),
);
it.live("keeps external rotated logs when a scanned directory is replaced by a symlink", () =>
runCleanupTest(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const config = yield* ServerConfig.ServerConfig;
yield* fs.makeDirectory(config.logsDir, { recursive: true });
const nested = `${yield* fs.realPath(config.logsDir)}/nested`;
const outside = yield* fs.makeTempDirectoryScoped();
const expired = `${outside}/server.log.1`;
yield* fs.makeDirectory(nested);
yield* fs.writeFileString(expired, "external log");
yield* fs.utimes(expired, 1, 1);

const { service, enableFiles, disable } = yield* cleanupFixture().pipe(
Effect.provideService(FileSystem.FileSystem, {
...fs,
readDirectory: (directory, options) =>
Effect.gen(function* () {
if (directory === nested) {
yield* fs.rename(nested, `${nested}-original`);
yield* fs.symlink(outside, nested);
}
return yield* fs.readDirectory(directory, options);
}),
}),
);
disable();
enableFiles();
const report = yield* service.runNow;
expect(yield* fs.exists(expired)).toBe(true);
expect(report.entries.find((entry) => entry.kind === "logs")).toMatchObject({
outcome: "kept",
reason: "No expired files",
});
}),
),
);
it.live("reports git failures without aborting the remaining categories", () =>
runCleanupTest(
Effect.gen(function* () {
Expand Down
6 changes: 4 additions & 2 deletions apps/server/src/storageCleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1810,8 +1810,10 @@ const make = Effect.gen(function* () {
): Effect.fn.Return<void, PlatformError | ServerSettingsError> {
for (const name of yield* fs.readDirectory(directory)) {
const target = path.join(directory, name);
if ((yield* fs.realPath(target)) !== target || !inside(realRoot, target)) continue;
const stat = yield* fs.stat(target);
if (!inside(realRoot, target)) continue;
const stat = yield* fs.lstat(target);
if (stat.type === "SymbolicLink") continue;
if ((yield* fs.realPath(target)) !== target) continue;
if (stat.type === "Directory" && rotatedLogs) {
yield* visit(target);
} else if (stat.type === "File" && (!rotatedLogs || /\.(?:log|ndjson)\.\d+$/.test(name))) {
Expand Down
73 changes: 73 additions & 0 deletions scripts/scenarios/cleanup-skips-dangling-log-symlink.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Schema from "effect/Schema";

import { T3_HOME } from "../e2e.config.ts";
import { runPromise, test, WEB } from "./t3.ts";

const decodeSettings = Schema.decodeSync(
Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown)),
);

test(
"Cleanup removes expired rotated logs even with a dangling symlink in the log directory",
WEB,
async ({ t3, app, browser, screen }) => {
await t3.project("rotated-log-cleanup");
const settingsPath = `${T3_HOME}/userdata/settings.json`;
const logFixture = `${T3_HOME}/userdata/logs/00-cleanup-fixture`;
const expiredLog = `${logFixture}/server.log.1`;
const original = await runPromise(
Effect.flatMap(FileSystem.FileSystem, (fs) => fs.readFileString(settingsPath)),
);

try {
await runPromise(
Effect.flatMap(FileSystem.FileSystem, (fs) =>
fs.writeFileString(
settingsPath,
JSON.stringify({ ...decodeSettings(original), storageCleanup: { logsAfterDays: 7 } }),
),
),
);
await t3.open("/settings/maintenance");
await screen.getByLabel("Delete old rotated logs in days").waitFor();

const deleteNow = screen.getByRole("button", "Delete now", { exact: true });
await deleteNow.tap();
await screen.getByRole("button", "Deleting…", { exact: true }).waitFor({ state: "hidden" });
await screen.getByRole("button", /^Last run/).tap();
const logsResult = browser
.locator('#storage-cleanup-last-run [data-slot="settings-row"]')
.filter({ has: screen.getByText("Rotated logs", { exact: true }) });
await logsResult.getByText("No expired files", { exact: true }).waitFor();

await runPromise(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
yield* fs.makeDirectory(logFixture);
yield* fs.symlink(`${logFixture}/missing.log`, `${logFixture}/00-dangling.log.1`);
yield* fs.writeFileString(expiredLog, "Expired log");
yield* fs.utimes(expiredLog, 1, 1);
}),
);

await deleteNow.tap();
await logsResult.scrollIntoView();
try {
await expect(logsResult).toContainText("Removed 1 rotated logs");
} finally {
await app.screenshot("rotated-log-cleanup-result");
}
} finally {
await runPromise(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
yield* fs.remove(logFixture, { recursive: true, force: true });
yield* fs.writeFileString(settingsPath, original);
}),
);
}
},
);
Loading