From 9994369ebd924905d7dbfc2f698ebe602e1bceec Mon Sep 17 00:00:00 2001 From: "Dr. Vortex" Date: Sat, 23 Sep 2023 13:05:02 -0500 Subject: [PATCH] Removed getRootFS Change mount to not make a directory --- src/emulation/index.ts | 2 +- src/emulation/shared.ts | 30 +++++++++++------------------- src/generic/preload_file.ts | 8 ++++---- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/emulation/index.ts b/src/emulation/index.ts index 95bf29f8..75d155a6 100644 --- a/src/emulation/index.ts +++ b/src/emulation/index.ts @@ -2,4 +2,4 @@ export * from './callbacks'; export * from './sync'; export * as promises from './promises'; export * as constants from './constants'; -export { initialize, getRootFS, mount, umount } from './shared'; +export { initialize, getMount, getMounts, mount, umount } from './shared'; diff --git a/src/emulation/shared.ts b/src/emulation/shared.ts index 0a0b8867..2cb328de 100644 --- a/src/emulation/shared.ts +++ b/src/emulation/shared.ts @@ -3,9 +3,8 @@ import { posix as path } from 'path'; import { ApiError, ErrorCode } from '../ApiError'; import { Cred } from '../cred'; -import { BaseFileSystem, FileSystem } from '../filesystem'; +import { FileSystem } from '../filesystem'; import { File } from '../file'; -import { mkdirpSync } from '../utils'; import { InternalBackendConstructor } from '../backends'; /** @@ -117,10 +116,17 @@ export function fd2file(fd: number): File { } // mounting +export interface MountMapping { + [point: string]: FileSystem; +} + export const mounts: Map = new Map(); -export function getRootFS(): FileSystem | null { - return mounts.get('/'); +export function getMount(mountPoint: string): FileSystem { + return mounts.get(mountPoint); +} +export function getMounts(): MountMapping { + return Object.fromEntries(mounts.entries()); } /** @@ -128,13 +134,12 @@ export function getRootFS(): FileSystem | null { */ export function mount(mountPoint: string, fs: FileSystem): void { if (mountPoint[0] !== '/') { - mountPoint = `/${mountPoint}`; + mountPoint = '/' + mountPoint; } mountPoint = path.resolve(mountPoint); if (mounts.has(mountPoint)) { throw new ApiError(ErrorCode.EINVAL, 'Mount point ' + mountPoint + ' is already in use.'); } - mkdirpSync(mountPoint, 0o777, cred, getRootFS()); mounts.set(mountPoint, fs); } @@ -147,15 +152,6 @@ export function umount(mountPoint: string): void { throw new ApiError(ErrorCode.EINVAL, 'Mount point ' + mountPoint + ' is already unmounted.'); } mounts.delete(mountPoint); - - while (mountPoint !== '/') { - if (getRootFS().readdirSync(mountPoint, cred).length === 0) { - getRootFS().rmdirSync(mountPoint, cred); - mountPoint = path.dirname(mountPoint); - } else { - break; - } - } } /** @@ -193,10 +189,6 @@ export function fixError(e: E, paths: { [from: string]: string return e; } -export interface MountMapping { - [point: string]: FileSystem; -} - export function initialize(mountMapping: MountMapping): void { if (!mountMapping['/']) { throw new ApiError(ErrorCode.EINVAL, 'BrowserFS must be initialized with a root filesystem.'); diff --git a/src/generic/preload_file.ts b/src/generic/preload_file.ts index bc3dd14b..5eecd6fa 100644 --- a/src/generic/preload_file.ts +++ b/src/generic/preload_file.ts @@ -3,7 +3,7 @@ import { FileSystem } from '../filesystem'; import { Stats } from '../stats'; import { FileFlag } from '../file'; import { ApiError, ErrorCode } from '../ApiError'; -import { getRootFS } from '../emulation/fs'; +import { getMount } from '../emulation/shared'; import { Buffer } from 'buffer'; /** @@ -165,7 +165,7 @@ export default class PreloadFile extends BaseFile { */ public truncate(len: number): Promise { this.truncateSync(len); - if (this._flag.isSynchronous() && !getRootFS()!.supportsSynch()) { + if (this._flag.isSynchronous() && !getMount('/')!.supportsSynch()) { return this.sync(); } } @@ -184,7 +184,7 @@ export default class PreloadFile extends BaseFile { const buf = Buffer.alloc(len - this._buffer.length, 0); // Write will set @_stat.size for us. this.writeSync(buf, 0, buf.length, this._buffer.length); - if (this._flag.isSynchronous() && getRootFS()!.supportsSynch()) { + if (this._flag.isSynchronous() && getMount('/')!.supportsSynch()) { this.syncSync(); } return; @@ -194,7 +194,7 @@ export default class PreloadFile extends BaseFile { const newBuff = Buffer.alloc(len); this._buffer.copy(newBuff, 0, 0, len); this._buffer = newBuff; - if (this._flag.isSynchronous() && getRootFS()!.supportsSynch()) { + if (this._flag.isSynchronous() && getMount('/')!.supportsSynch()) { this.syncSync(); } }