Skip to content

Commit

Permalink
Fix realpath returning mount relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexxeh committed Sep 18, 2018
1 parent 7a5c95f commit e475edc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/backend/MountableFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default class MountableFileSystem extends BaseFileSystem implements FileS
/**
* Returns the file system that the path points to.
*/
public _getFs(path: string): {fs: FileSystem; path: string} {
public _getFs(path: string): {fs: FileSystem; path: string, mountPoint: string} {
const mountList = this.mountList, len = mountList.length;
for (let i = 0; i < len; i++) {
const mountPoint = mountList[i];
Expand All @@ -153,11 +153,11 @@ export default class MountableFileSystem extends BaseFileSystem implements FileS
if (path === '') {
path = '/';
}
return {fs: this.mntMap[mountPoint], path: path};
return {fs: this.mntMap[mountPoint], path: path, mountPoint: mountPoint};
}
}
// Query our root file system.
return {fs: this.rootFs, path: path};
return {fs: this.rootFs, path: path, mountPoint: '/'};
}

// Global information methods
Expand Down Expand Up @@ -313,6 +313,31 @@ export default class MountableFileSystem extends BaseFileSystem implements FileS
});
}

public realpathSync(p: string, cache: {[path: string]: string}): string {
const fsInfo = this._getFs(p);

try {
const mountedPath = fsInfo.fs.realpathSync(fsInfo.path, {});
// resolve is there to remove any trailing slash that may be present
return path.resolve(path.join(fsInfo.mountPoint, mountedPath));
} catch (e) {
throw this.standardizeError(e, fsInfo.path, p);
}
}

public realpath(p: string, cache: {[path: string]: string}, cb: BFSCallback<string>): void {
const fsInfo = this._getFs(p);

fsInfo.fs.realpath(fsInfo.path, {}, (err, rv) => {
if (err) {
cb(this.standardizeError(err, fsInfo.path, p));
} else {
// resolve is there to remove any trailing slash that may be present
cb(null, path.resolve(path.join(fsInfo.mountPoint, rv!)));
}
});
}

public rmdirSync(p: string): void {
const fsInfo = this._getFs(p);
if (this._containsMountPt(p)) {
Expand Down Expand Up @@ -399,7 +424,7 @@ const fsCmdMap = [
// 1 arg functions
['exists', 'unlink', 'readlink'],
// 2 arg functions
['stat', 'mkdir', 'realpath', 'truncate'],
['stat', 'mkdir', 'truncate'],
// 3 arg functions
['open', 'readFile', 'chmod', 'utimes'],
// 4 arg functions
Expand Down
15 changes: 15 additions & 0 deletions test/tests/fs/MountableFileSystem/mounting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ export default function() {
newmfs.mount('/root/anotherRoot', rootForMfs);
BrowserFS.initialize(newmfs);

const realPathSyncResult = fs.realpathSync('/root/anotherRoot');
assert.equal(
realPathSyncResult,
'/root/anotherRoot',
`Invariant fail: non-linked directly resolved to different path: ${realPathSyncResult}`,
);

fs.realpath('/root/anotherRoot', function(err, p) {
assert.equal(
p,
'/root/anotherRoot',
`Invariant fail: non-linked directly resolved to different path: ${p}`,
);
});

assert.equal(fs.readdirSync('/')[0], 'root', 'Invariant fail: Can query root directory.');

var t1text = 'Invariant fail: Can query folder that contains items and a mount point.';
Expand Down

0 comments on commit e475edc

Please sign in to comment.