-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
win, fs: fix realpath behavior on substed drives #7559
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This is a partial revert of b488b19 It restores old javascript implementation of realpath and realpathSync. The names have been changed: added JS and not exported in fs module.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1562,6 +1562,112 @@ fs.unwatchFile = function(filename, listener) { | |
| } | ||
| }; | ||
|
|
||
| // Regexp that finds the next partion of a (partial) path | ||
| // result is [base_with_slash, base], e.g. ['somedir/', 'somedir'] | ||
| const nextPartRe = isWindows ? | ||
| /(.*?)(?:[\/\\]+|$)/g : | ||
| /(.*?)(?:[\/]+|$)/g; | ||
|
|
||
| // Regex to find the device root, including trailing slash. E.g. 'c:\\'. | ||
| const splitRootRe = isWindows ? | ||
| /^(?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?[\\\/]*/ : | ||
| /^[\/]*/; | ||
|
|
||
| function realpathSyncJS(p, cache) { | ||
| // make p is absolute | ||
| p = pathModule.resolve(p); | ||
|
|
||
| if (cache && Object.prototype.hasOwnProperty.call(cache, p)) { | ||
| return cache[p]; | ||
| } | ||
|
|
||
| const original = p; | ||
| const seenLinks = {}; | ||
| const knownHard = {}; | ||
|
|
||
| // current character position in p | ||
| var pos; | ||
| // the partial path so far, including a trailing slash if any | ||
| var current; | ||
| // the partial path without a trailing slash (except when pointing at a root) | ||
| var base; | ||
| // the partial path scanned in the previous round, with slash | ||
| var previous; | ||
|
|
||
| start(); | ||
|
|
||
| function start() { | ||
| // Skip over roots | ||
| var m = splitRootRe.exec(p); | ||
| pos = m[0].length; | ||
| current = m[0]; | ||
| base = m[0]; | ||
| previous = ''; | ||
|
|
||
| // On windows, check that the root exists. On unix there is no need. | ||
| if (isWindows && !knownHard[base]) { | ||
| fs.lstatSync(base); | ||
| knownHard[base] = true; | ||
| } | ||
| } | ||
|
|
||
| // walk down the path, swapping out linked pathparts for their real | ||
| // values | ||
| // NB: p.length changes. | ||
| while (pos < p.length) { | ||
| // find the next part | ||
| nextPartRe.lastIndex = pos; | ||
| var result = nextPartRe.exec(p); | ||
| previous = current; | ||
| current += result[0]; | ||
| base = previous + result[1]; | ||
| pos = nextPartRe.lastIndex; | ||
|
|
||
| // continue if not a symlink | ||
| if (knownHard[base] || (cache && cache[base] === base)) { | ||
| continue; | ||
| } | ||
|
|
||
| var resolvedLink; | ||
| if (cache && Object.prototype.hasOwnProperty.call(cache, base)) { | ||
| // some known symbolic link. no need to stat again. | ||
| resolvedLink = cache[base]; | ||
| } else { | ||
| var stat = fs.lstatSync(base); | ||
| if (!stat.isSymbolicLink()) { | ||
| knownHard[base] = true; | ||
| if (cache) cache[base] = base; | ||
| continue; | ||
| } | ||
|
|
||
| // read the link if it wasn't read before | ||
| // dev/ino always return 0 on windows, so skip the check. | ||
| var linkTarget = null; | ||
| if (!isWindows) { | ||
| var id = stat.dev.toString(32) + ':' + stat.ino.toString(32); | ||
| if (seenLinks.hasOwnProperty(id)) { | ||
| linkTarget = seenLinks[id]; | ||
| } | ||
| } | ||
| if (linkTarget === null) { | ||
| fs.statSync(base); | ||
| linkTarget = fs.readlinkSync(base); | ||
| } | ||
| resolvedLink = pathModule.resolve(previous, linkTarget); | ||
| // track this, if given a cache. | ||
| if (cache) cache[base] = resolvedLink; | ||
| if (!isWindows) seenLinks[id] = linkTarget; | ||
| } | ||
|
|
||
| // resolve the link, then start over | ||
| p = pathModule.resolve(resolvedLink, p.slice(pos)); | ||
| start(); | ||
| } | ||
|
|
||
| if (cache) cache[original] = p; | ||
|
|
||
| return p; | ||
| }; | ||
|
|
||
| fs.realpathSync = function realpathSync(path, options) { | ||
| if (!options) | ||
|
|
@@ -1574,6 +1680,127 @@ fs.realpathSync = function realpathSync(path, options) { | |
| return binding.realpath(pathModule._makeLong(path), options.encoding); | ||
| }; | ||
|
|
||
| function realpathJS(p, cache, cb) { | ||
| if (typeof cb !== 'function') { | ||
| cb = maybeCallback(cache); | ||
| cache = null; | ||
| } | ||
|
|
||
| // make p is absolute | ||
| p = pathModule.resolve(p); | ||
|
|
||
| if (cache && Object.prototype.hasOwnProperty.call(cache, p)) { | ||
| return process.nextTick(cb.bind(null, null, cache[p])); | ||
|
Contributor
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. This can be simplified to
Contributor
Author
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. This is how it was before switch to uv realpath. I would rather leave it as it was before it was removed, at least for now. |
||
| } | ||
|
|
||
| const original = p; | ||
| const seenLinks = {}; | ||
| const knownHard = {}; | ||
|
|
||
| // current character position in p | ||
| var pos; | ||
| // the partial path so far, including a trailing slash if any | ||
| var current; | ||
| // the partial path without a trailing slash (except when pointing at a root) | ||
| var base; | ||
| // the partial path scanned in the previous round, with slash | ||
| var previous; | ||
|
|
||
| start(); | ||
|
|
||
| function start() { | ||
| // Skip over roots | ||
| var m = splitRootRe.exec(p); | ||
| pos = m[0].length; | ||
| current = m[0]; | ||
| base = m[0]; | ||
| previous = ''; | ||
|
|
||
| // On windows, check that the root exists. On unix there is no need. | ||
| if (isWindows && !knownHard[base]) { | ||
| fs.lstat(base, function(err) { | ||
| if (err) return cb(err); | ||
| knownHard[base] = true; | ||
| LOOP(); | ||
| }); | ||
| } else { | ||
| process.nextTick(LOOP); | ||
| } | ||
| } | ||
|
|
||
| // walk down the path, swapping out linked pathparts for their real | ||
| // values | ||
| function LOOP() { | ||
| // stop if scanned past end of path | ||
| if (pos >= p.length) { | ||
| if (cache) cache[original] = p; | ||
| return cb(null, p); | ||
| } | ||
|
|
||
| // find the next part | ||
| nextPartRe.lastIndex = pos; | ||
| var result = nextPartRe.exec(p); | ||
| previous = current; | ||
| current += result[0]; | ||
| base = previous + result[1]; | ||
| pos = nextPartRe.lastIndex; | ||
|
|
||
| // continue if not a symlink | ||
| if (knownHard[base] || (cache && cache[base] === base)) { | ||
| return process.nextTick(LOOP); | ||
| } | ||
|
|
||
| if (cache && Object.prototype.hasOwnProperty.call(cache, base)) { | ||
| // known symbolic link. no need to stat again. | ||
| return gotResolvedLink(cache[base]); | ||
| } | ||
|
|
||
| return fs.lstat(base, gotStat); | ||
| } | ||
|
|
||
| function gotStat(err, stat) { | ||
| if (err) return cb(err); | ||
|
|
||
| // if not a symlink, skip to the next path part | ||
| if (!stat.isSymbolicLink()) { | ||
| knownHard[base] = true; | ||
| if (cache) cache[base] = base; | ||
| return process.nextTick(LOOP); | ||
| } | ||
|
|
||
| // stat & read the link if not read before | ||
| // call gotTarget as soon as the link target is known | ||
| // dev/ino always return 0 on windows, so skip the check. | ||
| if (!isWindows) { | ||
| var id = stat.dev.toString(32) + ':' + stat.ino.toString(32); | ||
| if (seenLinks.hasOwnProperty(id)) { | ||
| return gotTarget(null, seenLinks[id], base); | ||
| } | ||
| } | ||
| fs.stat(base, function(err) { | ||
| if (err) return cb(err); | ||
|
|
||
| fs.readlink(base, function(err, target) { | ||
| if (!isWindows) seenLinks[id] = target; | ||
| gotTarget(err, target); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function gotTarget(err, target, base) { | ||
| if (err) return cb(err); | ||
|
|
||
| var resolvedLink = pathModule.resolve(previous, target); | ||
| if (cache) cache[base] = resolvedLink; | ||
| gotResolvedLink(resolvedLink); | ||
| } | ||
|
|
||
| function gotResolvedLink(resolvedLink) { | ||
| // resolve the link, then start over | ||
| p = pathModule.resolve(resolvedLink, p.slice(pos)); | ||
| start(); | ||
| } | ||
| }; | ||
|
|
||
| fs.realpath = function realpath(path, options, callback) { | ||
| if (!options) { | ||
|
|
||
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.
I am not sure removing this line is without side effects, at least for the case that the
!nullCheck()branch is taken.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.
makeCallbackcomment says "Only use this function for callbacks that are passed to the binding layer, callbacks that are invoked from JS already run in the proper scope.". I think we are good here.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.
The error thrown for
fs.realpath('\0')changes from an asynchronous one to a synchronous one with this change, and error handling changes are usually considered semver-major. This one is certainly as edge-case-y as it gets, though.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.
Right, I'll change it.