Skip to content

Commit

Permalink
Removed getRootFS
Browse files Browse the repository at this point in the history
Change mount to not make a directory
  • Loading branch information
james-pre committed Sep 23, 2023
1 parent 0ea6bc5 commit 9994369
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/emulation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
30 changes: 11 additions & 19 deletions src/emulation/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -117,24 +116,30 @@ export function fd2file(fd: number): File {
}

// mounting
export interface MountMapping {
[point: string]: FileSystem;
}

export const mounts: Map<string, FileSystem> = 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());
}

/**
* Mounts the file system at the given mount point.
*/
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);
}

Expand All @@ -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;
}
}
}

/**
Expand Down Expand Up @@ -193,10 +189,6 @@ export function fixError<E extends Error>(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.');
Expand Down
8 changes: 4 additions & 4 deletions src/generic/preload_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -165,7 +165,7 @@ export default class PreloadFile<T extends FileSystem> extends BaseFile {
*/
public truncate(len: number): Promise<void> {
this.truncateSync(len);
if (this._flag.isSynchronous() && !getRootFS()!.supportsSynch()) {
if (this._flag.isSynchronous() && !getMount('/')!.supportsSynch()) {
return this.sync();
}
}
Expand All @@ -184,7 +184,7 @@ export default class PreloadFile<T extends FileSystem> 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;
Expand All @@ -194,7 +194,7 @@ export default class PreloadFile<T extends FileSystem> 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();
}
}
Expand Down

0 comments on commit 9994369

Please sign in to comment.