Browse Source

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

PR-URL: https://github.com/nodejs/node/pull/13028
Reviewed-By: Refael Ackermann <refack@gmail.com>
v6
Ebrahim Byagowi 8 years ago
committed by Refael Ackermann
parent
commit
b3d1e3d4c7
  1. 12
      doc/api/fs.md
  2. 18
      lib/fs.js
  3. 34
      test/parallel/test-fs-realpath-pipe.js

12
doc/api/fs.md

@ -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

18
lib/fs.js

@ -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');
@ -296,11 +296,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();
@ -1693,8 +1693,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;
}
@ -1820,8 +1824,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);
}

34
test/parallel/test-fs-realpath-pipe.js

@ -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);
}
Loading…
Cancel
Save