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
20 changes: 14 additions & 6 deletions packages/core/src/atomicFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ export async function replaceFileAtomicallyAsync(
mode?: number,
): Promise<void> {
traceWrite(filePath, content);
try {
await fsp.access(filePath, fs.constants.W_OK);
} catch (error) {
if (errorCode(error) !== "ENOENT") throw error;
}
await refuseReadOnlyAsync(filePath);
const tempPath = await writeTempSiblingAsync(filePath, content, mode);
try {
if (mode !== undefined) await fsp.chmod(tempPath, mode);
Expand Down Expand Up @@ -114,10 +110,22 @@ export function refuseReadOnly(filePath: string): void {
try {
fs.accessSync(filePath, fs.constants.W_OK);
} catch (error) {
if (errorCode(error) !== "ENOENT") throw error;
readOnlyOrMissing(error);
}
}

async function refuseReadOnlyAsync(filePath: string): Promise<void> {
try {
await fsp.access(filePath, fs.constants.W_OK);
} catch (error) {
readOnlyOrMissing(error);
}
}

function readOnlyOrMissing(error: unknown): void {
if (errorCode(error) !== "ENOENT") throw error;
}

/** Create a file that must not exist yet (EEXIST otherwise); readers never see it empty or partial. */
export function createFileAtomically(
filePath: string,
Expand Down
49 changes: 39 additions & 10 deletions packages/core/src/safePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
realpathSync,
statSync,
} from "node:fs";
import { realpath as realpathNative } from "node:fs/promises";
import { mkdir, realpath as realpathNative } from "node:fs/promises";
import { promisify } from "node:util";
import { isWithinProjectRoot } from "@hyperframes/parsers/asset-resolution";
import { projectPathSegments } from "@hyperframes/parsers/private-path";
Expand Down Expand Up @@ -308,25 +308,54 @@ export function realProjectRoot(root: string): string {
* A `dir` outside `root` is created recursively, but only while `root` exists.
*/
export function mkdirWithinProject(root: string, dir: string): void {
if (folderGone(root)) throw new ProjectRootMissingError(root);
const inside = relative(resolve(root), resolve(dir));
if (inside === ".." || inside.startsWith(`..${sep}`) || isAbsolute(inside)) {
const folders = foldersToMake(root, dir);
if (folders === null) {
mkdirSync(dir, { recursive: true });
return;
}
let path = resolve(root);
for (const part of inside.split(sep).filter(Boolean)) {
path = join(path, part);
for (const path of folders) {
try {
mkdirSync(path);
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code === "ENOENT" && folderGone(root)) throw new ProjectRootMissingError(root);
if (code !== "EEXIST") throw error;
madeOrThrow(root, error);
}
}
}

/** {@link mkdirWithinProject} on fs/promises: a write the drive never answers holds a pool thread, not the caller's. */
export async function mkdirWithinProjectAsync(root: string, dir: string): Promise<void> {
const folders = foldersToMake(root, dir);
if (folders === null) {
await mkdir(dir, { recursive: true });
return;
}
for (const path of folders) {
try {
await mkdir(path);
} catch (error) {
madeOrThrow(root, error);
}
}
}

/** Each folder from below `root` down to `dir`, or null when `dir` is outside `root`. */
function foldersToMake(root: string, dir: string): string[] | null {
if (folderGone(root)) throw new ProjectRootMissingError(root);
const inside = relative(resolve(root), resolve(dir));
if (inside === ".." || inside.startsWith(`..${sep}`) || isAbsolute(inside)) return null;
let path = resolve(root);
return inside
.split(sep)
.filter(Boolean)
.map((part) => (path = join(path, part)));
}

function madeOrThrow(root: string, error: unknown): void {
const code = (error as NodeJS.ErrnoException).code;
if (code === "ENOENT" && folderGone(root)) throw new ProjectRootMissingError(root);
if (code !== "EEXIST") throw error;
}

/** The real path; for a path not there (yet, or any more), the nearest existing folder's real path plus the rest. */
export function realFilePath(filePath: string): string {
try {
Expand Down
Loading
Loading