Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a createFolderMount util #6163

Open
wants to merge 2 commits into
base: mael/jailfs-tests
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/yarnpkg-fslib/sources/MountFS.ts
Expand Up @@ -5,6 +5,7 @@ import {FakeFS, MkdirOptions, RmdirOptions, WriteFileOptions, OpendirOptions}
import {Dirent, SymlinkType} from './FakeFS';
import {CreateReadStreamOptions, CreateWriteStreamOptions, BasePortableFakeFS, ExtractHintOptions, WatchFileOptions, WatchFileCallback, StatWatcher} from './FakeFS';
import {NodeFS} from './NodeFS';
import {SubFS} from './SubFS';
import {watchFile, unwatchFile, unwatchAllFiles} from './algorithms/watchFile';
import * as errors from './errors';
import {Filename, FSPath, npath, PortablePath} from './path';
Expand Down Expand Up @@ -80,6 +81,37 @@ export class MountFS<MountedFS extends MountableFS> extends BasePortableFakeFS {
private notMount: Set<PortablePath> = new Set();
private realPaths: Map<PortablePath, PortablePath> = new Map();

static createFolderMount({baseFs, mountPoint, targetPath}: {baseFs: FakeFS<PortablePath>, mountPoint: PortablePath, targetPath: PortablePath}) {
const subFs = new SubFS(targetPath);

const getMountPoint = (p: PortablePath) => {
const detectedMountPoint = p === mountPoint || p.startsWith(`${mountPoint}/`) ? p.slice(0, mountPoint.length) : null;
return detectedMountPoint as PortablePath;
};

const factoryPromise = async (baseFs: FakeFS<PortablePath>, p: PortablePath) => {
return () => subFs;
};

const factorySync = (baseFs: FakeFS<PortablePath>, p: PortablePath) => {
return subFs;
};

return new MountFS({
baseFs,

getMountPoint,

factoryPromise,
factorySync,

magicByte: 21,
maxAge: Infinity,

typeCheck: null,
});
}

constructor({baseFs = new NodeFS(), filter = null, magicByte = 0x2a, maxOpenFiles = Infinity, useCache = true, maxAge = 5000, typeCheck = constants.S_IFREG, getMountPoint, factoryPromise, factorySync}: MountFSOptions<MountedFS>) {
if (Math.floor(magicByte) !== magicByte || !(magicByte > 1 && magicByte <= 127))
throw new Error(`The magic byte must be set to a round value between 1 and 127 included`);
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-fslib/sources/index.ts
Expand Up @@ -50,7 +50,7 @@ export {ProxiedFS} from './ProxiedFS';
export {SubFS} from './SubFS';
export {VirtualFS} from './VirtualFS';

export {patchFs, extendFs} from './patchFs/patchFs';
export {applyFsLayer, extendFs, patchFs} from './patchFs/patchFs';

export {xfs} from './xfs';
export type {XFS} from './xfs';
24 changes: 18 additions & 6 deletions packages/yarnpkg-fslib/sources/patchFs/patchFs.ts
@@ -1,11 +1,13 @@
import fs from 'fs';
import {promisify} from 'util';
import fs from 'fs';
import {promisify} from 'util';

import {FakeFS} from '../FakeFS';
import {NodePathFS} from '../NodePathFS';
import {NativePath} from '../path';
import {FakeFS} from '../FakeFS';
import {NodeFS} from '../NodeFS';
import {NodePathFS} from '../NodePathFS';
import {PosixFS} from '../PosixFS';
import {NativePath, PortablePath} from '../path';

import {FileHandle} from './FileHandle';
import {FileHandle} from './FileHandle';

const SYNC_IMPLEMENTATIONS = new Set([
`accessSync`,
Expand Down Expand Up @@ -136,6 +138,16 @@ type ReadArgumentsOptions = [
type ReadArgumentsCallback = [fd: number, callback: ReadCallback];
//#endregion

export function applyFsLayer(origFs: typeof fs, factory: (baseFs: FakeFS<PortablePath>) => FakeFS<PortablePath>) {
// We must copy the fs into a local, because otherwise
// 1. we would make the NodeFS instance use the function that we patched (infinite loop)
// 2. Object.create(fs) isn't enough, since it won't prevent the proto from being modified
const localFs = {...origFs};
const nodeFs = new NodeFS(localFs);

patchFs(fs, new PosixFS(factory(nodeFs)));
}

Comment on lines +141 to +149
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could find a way to avoid repeating those indirections when applying multiple mount layers ... perhaps by allowing MountFS to handle an array of mount configuration instead of a single one?

export function patchFs(patchedFs: typeof fs, fakeFs: FakeFS<NativePath>): void {
// We wrap the `fakeFs` with a `NodePathFS` to add support for all path types supported by Node
fakeFs = new NodePathFS(fakeFs);
Expand Down