-
Notifications
You must be signed in to change notification settings - Fork 29.1k
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
win, fs: fix realpath behavior on substed drives #7559
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1562,6 +1562,8 @@ fs.unwatchFile = function(filename, listener) { | |
} | ||
}; | ||
|
||
// Cache for JS real path | ||
var realpathCache = {}; | ||
// Regexp that finds the next partion of a (partial) path | ||
// result is [base_with_slash, base], e.g. ['somedir/', 'somedir'] | ||
const nextPartRe = isWindows ? | ||
|
@@ -1669,6 +1671,25 @@ function realpathSyncJS(p, cache) { | |
return p; | ||
}; | ||
|
||
function realpathJSNeeded(resolvedPath, result, err) { | ||
if (!isWindows || err) | ||
return false; | ||
const resultStr = result.toString('utf8'); | ||
return resultStr.length > 0 && resolvedPath.length > 0 && | ||
resultStr[0].toUpperCase() !== resolvedPath[0].toUpperCase(); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the linter say anything about the extra |
||
|
||
function convertRealpathJSResult(result, encoding, err) { | ||
if (!encoding || encoding === 'utf8' || err) | ||
return result; | ||
const asBuffer = Buffer.from(result); | ||
if (encoding === 'buffer') { | ||
return asBuffer; | ||
} else { | ||
return asBuffer.toString(encoding); | ||
} | ||
}; | ||
|
||
fs.realpathSync = function realpathSync(path, options) { | ||
if (!options) | ||
options = {}; | ||
|
@@ -1677,7 +1698,16 @@ fs.realpathSync = function realpathSync(path, options) { | |
else if (typeof options !== 'object') | ||
throw new TypeError('"options" must be a string or an object'); | ||
nullCheck(path); | ||
return binding.realpath(pathModule._makeLong(path), options.encoding); | ||
const resolvedPath = pathModule.resolve(path.toString('utf8')); | ||
const uvResult = binding.realpath(pathModule._makeLong(resolvedPath), | ||
options.encoding); | ||
if (realpathJSNeeded(resolvedPath, uvResult)) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does |
||
const jsResult = realpathSyncJS(resolvedPath, realpathCache); | ||
return convertRealpathJSResult(jsResult, options.encoding); | ||
} else { | ||
return uvResult; | ||
} | ||
}; | ||
|
||
function realpathJS(p, cache, cb) { | ||
|
@@ -1813,11 +1843,25 @@ fs.realpath = function realpath(path, options, callback) { | |
} else if (typeof options !== 'object') { | ||
throw new TypeError('"options" must be a string or an object'); | ||
} | ||
callback = makeCallback(callback); | ||
if (!nullCheck(path, callback)) | ||
return; | ||
|
||
const resolvedPath = pathModule.resolve(path.toString('utf8')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can be moved into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Same thing in realpathSync. |
||
const win32callback = function(err, result) { | ||
if (realpathJSNeeded(resolvedPath, result, err)) { | ||
realpathJS(resolvedPath, realpathCache, function(err, result) { | ||
result = convertRealpathJSResult(result, options.encoding, err); | ||
return callback(err, result); | ||
}); | ||
} else { | ||
return callback(err, result); | ||
} | ||
}; | ||
|
||
const use_callback = makeCallback(isWindows ? win32callback : callback); | ||
|
||
var req = new FSReqWrap(); | ||
req.oncomplete = callback; | ||
req.oncomplete = use_callback; | ||
binding.realpath(pathModule._makeLong(path), options.encoding, req); | ||
return; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const spawnSync = require('child_process').spawnSync; | ||
|
||
if (!common.isWindows) { | ||
common.skip("Test for Windows only"); | ||
return; | ||
} | ||
let result; | ||
|
||
// create a subst drive | ||
const driveLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ"; | ||
let driveLetter; | ||
for (var i = 0; i < driveLetters.length; ++i) { | ||
driveLetter = `${driveLetters[i]}:` | ||
result = spawnSync('subst', [driveLetter, common.fixturesDir]); | ||
if (result.status === 0) | ||
break; | ||
} | ||
if (i === driveLetters.length) { | ||
common.skip("Cannot create subst drive"); | ||
return; | ||
} | ||
|
||
var asyncCompleted = 0; | ||
// schedule cleanup (and check if all callbacks where called) | ||
process.on('exit', function() { | ||
spawnSync('subst', ['/d', driveLetter]); | ||
assert.equal(asyncCompleted, 2); | ||
}); | ||
|
||
|
||
// test: | ||
const filename = `${driveLetter}\\empty.js`; | ||
const filenameBuffer = Buffer.from(filename); | ||
|
||
result = fs.realpathSync(filename); | ||
assert.equal(typeof result, 'string'); | ||
assert.equal(result, filename); | ||
|
||
result = fs.realpathSync(filename, 'buffer'); | ||
assert(Buffer.isBuffer(result)); | ||
assert(result.equals(filenameBuffer)); | ||
|
||
fs.realpath(filename, function(err, result) { | ||
++asyncCompleted; | ||
assert(!err); | ||
assert.equal(typeof result, 'string'); | ||
assert.equal(result, filename); | ||
}); | ||
|
||
fs.realpath(filename, 'buffer', function(err, result) { | ||
++asyncCompleted; | ||
assert(!err); | ||
assert(Buffer.isBuffer(result)); | ||
assert(result.equals(filenameBuffer)); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One global
realpath
cache without some kind of cache validation is probably a no-go?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a thing that needs to be solved. One global cache is probably not a good idea. But some caching is needed for performance reasons.
I would guess user could add cache object to options. It could be also used on Linux, to cache realpath results (not the partial ones as in JS implementation)