Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/auth/admin-secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ describe("AdminSecretManager", () => {
writeFileAtomic: () => {
throw new Error("verify() must not touch the filesystem");
},
writeFileInPlace: () => {
throw new Error("must not touch the filesystem");
},
copyFile: () => {
throw new Error("verify() must not touch the filesystem");
},
Expand Down Expand Up @@ -162,6 +165,9 @@ describe("AdminSecretManager", () => {
writeFileAtomic: () => {
throw new Error("must not touch the filesystem");
},
writeFileInPlace: () => {
throw new Error("must not touch the filesystem");
},
copyFile: () => {
throw new Error("verify() must not touch the filesystem");
},
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/android/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class Emulator {
): Promise<ProcessHandle> {
const startedAt = this.#options.clock.now();
const logPath = this.#logPath(data);
await this.#options.filesystem.writeFileAtomic(logPath, "", { mode: 0o600 });
await this.#options.filesystem.writeFileInPlace(logPath, "", { mode: 0o600 });
// why: the output goes to a file so a launch that dies at once leaves its own error line to quote (#1199); `unref()` because an emulator outlives the daemon by design
// (`AndroidDriver` adopts it after a restart) and is reaped through adb and
// by pid, never by awaiting its exit. Left referenced, its handle and stdio pipes would hold
Expand Down
7 changes: 6 additions & 1 deletion src/drivers/android/index-boot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,13 @@ describe("AndroidDriver", () => {
});
});

it("sends the emulator's output to <avd folder>/emulator.log, mode 0600, truncated at each start", async () => {
it("sends the emulator's output to <avd folder>/emulator.log, mode 0600, emptied in place at each start", async () => {
const harness = await provisionedHarness();
const logPath = `${avdDirectory}/simlock_one/simlock.avd/emulator.log`;
await harness.filesystem.writeFileAtomic(logPath, "left by an earlier start\n", {
mode: 0o644,
});
const earlier = await harness.filesystem.stat(logPath);

await harness.driver.makeReady(harness.device);

Expand All @@ -557,6 +558,10 @@ describe("AndroidDriver", () => {
expect(launch?.options.stdio).toEqual({ file: logPath });
await expect(harness.filesystem.readFile(logPath)).resolves.toBe("");
expect((await harness.filesystem.lstat(logPath)).mode & 0o777).toBe(0o600);
expect(
(await harness.filesystem.stat(logPath)).identity,
"emulator.log was replaced by another file, not emptied",
).toBe(earlier.identity);
});

it("an Android boot waits android.bootTimeoutMs, then kills the emulator and fails with BootTimeoutError", async () => {
Expand Down
17 changes: 17 additions & 0 deletions src/ports/filesystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ describe.each(implementations)("Filesystem contract: $name", ({ create, link })
]);
});

it("writes a file in place: the same file, emptied or filled, ending at the mode it is given", async () => {
const filesystem = create();
const file = `${temporaryDirectory}/emulator.log`;

await filesystem.mkdirp(temporaryDirectory);
await filesystem.writeFileAtomic(file, "old output", { mode: 0o644 });
const before = (await filesystem.stat(file)).identity;
await filesystem.writeFileInPlace(file, "", { mode: 0o600 });

await expect(filesystem.readFile(file)).resolves.toBe("");
expect((await filesystem.stat(file)).identity).toBe(before);
expect((await filesystem.stat(file)).mode).toBe(0o600);

await filesystem.writeFileInPlace(`${temporaryDirectory}/new.log`, "text", { mode: 0o600 });
await expect(filesystem.readFile(`${temporaryDirectory}/new.log`)).resolves.toBe("text");
});

it("copies a file's contents over the target and leaves the source", async () => {
const filesystem = create();
await filesystem.mkdirp(temporaryDirectory);
Expand Down
38 changes: 38 additions & 0 deletions src/ports/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ export interface Filesystem {
readFile(path: string): Promise<string>;
readFileFrom(path: string, offset: number): Promise<string>;
writeFileAtomic(path: string, contents: string, options?: WriteFileAtomicOptions): Promise<void>;
/**
* why: replaces a file's contents in the same file (same identity), then sets its mode, so an
* existing 0644 file ends at `mode`. `writeFileAtomic` renames over the path, which Windows
* refuses with `EPERM` while another process holds the file open (#1662).
*/
writeFileInPlace(
path: string,
contents: string,
options: { readonly mode: number },
): Promise<void>;
/**
* why: Writes a file only when nothing is there, failing `EEXIST` otherwise. The kernel
* decides who wins, which is the only way two processes racing to write one file both
Expand Down Expand Up @@ -283,6 +293,15 @@ export class NodeFilesystem implements Filesystem {
}
}

async writeFileInPlace(
path: string,
contents: string,
options: { readonly mode: number },
): Promise<void> {
await writeFile(path, contents, { encoding: "utf8", mode: options.mode });
await chmod(path, options.mode);
}

async writeFileExclusive(
path: string,
contents: string,
Expand Down Expand Up @@ -592,6 +611,25 @@ export class MemoryFilesystem implements Filesystem {
});
}

async writeFileInPlace(
path: string,
contents: string,
options: { readonly mode: number },
): Promise<void> {
this.#failIfDefined(path);
path = this.#underResolvedParent(path);
const existing = this.#entries.get(path);

if (existing?.kind === "file") {
existing.contents = contents;
delete existing.bytes;
existing.mode = options.mode & 0o777;
return;
}

await this.writeFileAtomic(path, contents, options);
}

async writeFileExclusive(
path: string,
contents: string,
Expand Down
Loading