Skip to content
Permalink
Browse files

fs: fix realpath{Sync} on resolving pipes/sockets

PR-URL: #13028
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information...
ebraminio authored and jasnell committed May 14, 2017
1 parent f077e51 commit d99b7bc8c9026946e9e45c319f155a0a1151de64
Showing with 59 additions and 5 deletions.
  1. +12 −0 doc/api/fs.md
  2. +13 −5 lib/fs.js
  3. +34 −0 test/parallel/test-fs-realpath-pipe.js
@@ -1840,6 +1840,9 @@ Synchronous version of [`fs.read()`][]. Returns the number of `bytesRead`.
<!-- YAML
added: v0.1.31
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/13028
description: Pipe/Socket resolve support was added.
- version: v7.6.0
pr-url: https://github.com/nodejs/node/pull/10739
description: The `path` parameter can be a WHATWG `URL` object using
@@ -1872,10 +1875,16 @@ object with an `encoding` property specifying the character encoding to use for
the path passed to the callback. If the `encoding` is set to `'buffer'`,
the path returned will be passed as a `Buffer` object.

*Note*: If `path` resolves to a socket or a pipe, the function will return a
system dependent name for that object.

## fs.realpathSync(path[, options])
<!-- YAML
added: v0.1.31
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/13028
description: Pipe/Socket resolve support was added.
- version: v7.6.0
pr-url: https://github.com/nodejs/node/pull/10739
description: The `path` parameter can be a WHATWG `URL` object using
@@ -1902,6 +1911,9 @@ object with an `encoding` property specifying the character encoding to use for
the returned value. If the `encoding` is set to `'buffer'`, the path returned
will be passed as a `Buffer` object.

*Note*: If `path` resolves to a socket or a pipe, the function will return a
system dependent name for that object.

## fs.rename(oldPath, newPath, callback)
<!-- YAML
added: v0.0.2
@@ -25,7 +25,7 @@
'use strict';

const constants = process.binding('constants').fs;
const { S_IFMT, S_IFREG, S_IFLNK } = constants;
const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants;
const util = require('util');
const pathModule = require('path');
const { isUint8Array } = process.binding('util');
@@ -228,11 +228,11 @@ Stats.prototype.isSymbolicLink = function() {
};

Stats.prototype.isFIFO = function() {
return this._checkModeProperty(constants.S_IFIFO);
return this._checkModeProperty(S_IFIFO);
};

Stats.prototype.isSocket = function() {
return this._checkModeProperty(constants.S_IFSOCK);
return this._checkModeProperty(S_IFSOCK);
};

const statValues = binding.getStatValues();
@@ -1625,8 +1625,12 @@ fs.realpathSync = function realpathSync(p, options) {
pos = result + 1;
}

// continue if not a symlink
// continue if not a symlink, break if a pipe/socket
if (knownHard[base] || (cache && cache.get(base) === base)) {
if ((statValues[1/*mode*/] & S_IFMT) === S_IFIFO ||
(statValues[1/*mode*/] & S_IFMT) === S_IFSOCK) {
break;
}
continue;
}

@@ -1752,8 +1756,12 @@ fs.realpath = function realpath(p, options, callback) {
pos = result + 1;
}

// continue if not a symlink
// continue if not a symlink, break if a pipe/socket
if (knownHard[base]) {
if ((statValues[1/*mode*/] & S_IFMT) === S_IFIFO ||
(statValues[1/*mode*/] & S_IFMT) === S_IFSOCK) {
return callback(null, encodeRealpathResult(p, options));
}
return process.nextTick(LOOP);
}

@@ -0,0 +1,34 @@
'use strict';

const common = require('../common');

if (common.isWindows || common.isAix) {
common.skip(`No /dev/stdin on ${process.platform}.`);
return;
}

const assert = require('assert');

const { spawnSync } = require('child_process');

for (const code of [
`require('fs').realpath('/dev/stdin', (err, resolvedPath) => {
if (err) {
process.exit(1);
}
if (resolvedPath) {
process.exit(2);
}
});`,
`try {
if (require('fs').realpathSync('/dev/stdin')) {
process.exit(2);
}
} catch (e) {
process.exit(1);
}`
]) {
assert.strictEqual(spawnSync(process.execPath, ['-e', code], {
stdio: 'pipe'
}).status, 2);
}

0 comments on commit d99b7bc

Please sign in to comment.
You can’t perform that action at this time.