Skip to content

Commit c024950

Browse files
committed
Fixed incorrect string slicing in mkdir (+Sync)
Fixed incorrectly ordered paths in `mkdirSync`
1 parent c405ff6 commit c024950

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/vfs/promises.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -783,14 +783,13 @@ export async function mkdir(
783783
const mode = normalizeMode(options?.mode, 0o777);
784784

785785
path = await realpath.call(this, path);
786-
const { fs, path: resolved, root } = resolveMount(path, this);
787-
const errorPaths: Record<string, string> = { [resolved]: path };
786+
const { fs, path: resolved } = resolveMount(path, this);
788787

789-
const __create = async (path: string, parent: InodeLike) => {
788+
const __create = async (path: string, resolved: string, parent: InodeLike) => {
790789
if (checkAccess && !hasAccess(this, parent, constants.W_OK)) throw UV('EACCES', 'mkdir', dirname(path));
791790

792791
const inode = await fs
793-
.mkdir(path, {
792+
.mkdir(resolved, {
794793
mode,
795794
uid: parent.mode & constants.S_ISUID ? parent.uid : uid,
796795
gid: parent.mode & constants.S_ISGID ? parent.gid : gid,
@@ -801,29 +800,28 @@ export async function mkdir(
801800
};
802801

803802
if (!options?.recursive) {
804-
await __create(resolved, await fs.stat(dirname(resolved)));
803+
await __create(path, resolved, await fs.stat(dirname(resolved)));
805804
return;
806805
}
807806

808-
const dirs: string[] = [];
807+
const dirs: [path: string, resolved: string][] = [];
809808
let origDir = path;
810809
for (
811810
let dir = resolved;
812811
!(await fs.exists(dir).catch(rethrow({ syscall: 'exists', path: origDir })));
813812
dir = dirname(dir), origDir = dirname(origDir)
814813
) {
815-
dirs.unshift(dir);
816-
errorPaths[dir] = origDir;
814+
dirs.unshift([origDir, dir]);
817815
}
818816

819817
if (!dirs.length) return;
820818

821-
const stats: InodeLike[] = [await fs.stat(dirname(dirs[0])).catch(rethrow({ syscall: 'stat', path: dirname(origDir) }))];
819+
const stats: InodeLike[] = [await fs.stat(dirname(dirs[0][1])).catch(rethrow({ syscall: 'stat', path: dirname(dirs[0][0]) }))];
822820

823-
for (const [i, dir] of dirs.entries()) {
824-
stats.push(await __create(dir, stats[i]));
821+
for (const [i, [path, resolved]] of dirs.entries()) {
822+
stats.push(await __create(path, resolved, stats[i]));
825823
}
826-
return root.length == 1 ? dirs[0] : dirs[0]?.slice(root.length);
824+
return dirs[0][0];
827825
}
828826
mkdir satisfies typeof promises.mkdir;
829827

src/vfs/sync.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,9 @@ export function mkdirSync(this: V_Context, path: fs.PathLike, options?: fs.Mode
472472
const mode = normalizeMode(options?.mode, 0o777);
473473

474474
path = realpathSync.call(this, path);
475-
const { fs, path: resolved, root } = resolveMount(path, this);
475+
const { fs, path: resolved } = resolveMount(path, this);
476476

477-
const __create = (resolved: string, path: string, parent: InodeLike) => {
477+
const __create = (path: string, resolved: string, parent: InodeLike) => {
478478
if (checkAccess && !hasAccess(this, parent, constants.W_OK)) throw UV('EACCES', 'mkdir', dirname(path));
479479

480480
const inode = wrap(
@@ -492,7 +492,7 @@ export function mkdirSync(this: V_Context, path: fs.PathLike, options?: fs.Mode
492492
};
493493

494494
if (!options?.recursive) {
495-
__create(resolved, path, wrap(fs, 'statSync', dirname(path))(dirname(resolved)));
495+
__create(path, resolved, wrap(fs, 'statSync', dirname(path))(dirname(resolved)));
496496
return;
497497
}
498498

@@ -503,12 +503,12 @@ export function mkdirSync(this: V_Context, path: fs.PathLike, options?: fs.Mode
503503

504504
if (!dirs.length) return;
505505

506-
const stats: InodeLike[] = [wrap(fs, 'statSync', dirname(dirs[0].resolved))(dirname(dirs[0].original))];
506+
const stats: InodeLike[] = [wrap(fs, 'statSync', dirname(dirs[0].original))(dirname(dirs[0].resolved))];
507507

508508
for (const [i, dir] of dirs.entries()) {
509-
stats.push(__create(dir.resolved, dir.original, stats[i]));
509+
stats.push(__create(dir.original, dir.resolved, stats[i]));
510510
}
511-
return root.length == 1 ? dirs[0].original : dirs[0].original.slice(root.length);
511+
return dirs[0].original;
512512
}
513513
mkdirSync satisfies typeof fs.mkdirSync;
514514

0 commit comments

Comments
 (0)