diff options
author | Bert Belder <bertbelder@gmail.com> | 2012-06-27 01:59:25 +0200 |
---|---|---|
committer | Bert Belder <bertbelder@gmail.com> | 2012-06-27 01:59:25 +0200 |
commit | 0cdeb8ed9690dcad912996a37ac149c7e7622f64 (patch) | |
tree | b75cb81d35681eabe28f1bd7284269a29e7880fa /lib | |
parent | f00c8bcef856d7ff7da40eb67a3c74f13f086256 (diff) | |
download | nodejs-0cdeb8ed9690dcad912996a37ac149c7e7622f64.tar.gz nodejs-0cdeb8ed9690dcad912996a37ac149c7e7622f64.tar.bz2 nodejs-0cdeb8ed9690dcad912996a37ac149c7e7622f64.zip |
windows: make fs.realpath(Sync) work with UNC paths
Closes #3542
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fs.js | 103 |
1 files changed, 61 insertions, 42 deletions
@@ -966,10 +966,12 @@ if (isWindows) { var nextPartRe = /(.*?)(?:[\/]+|$)/g; } -// Regex to split a windows path into three parts: [*, device, slash, -// tail] windows-only -var splitDeviceRe = - /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/; +// Regex to find the device root, including trailing slash. E.g. 'c:\\'. +if (isWindows) { + var splitRootRe = /^(?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?[\\\/]*/; +} else { + var splitRootRe = /^[\/]*/; +} fs.realpathSync = function realpathSync(p, cache) { // make p is absolute @@ -984,13 +986,30 @@ fs.realpathSync = function realpathSync(p, cache) { knownHard = {}; // current character position in p - var pos = 0; + var pos; // the partial path so far, including a trailing slash if any - var current = ''; - // the partial path without a trailing slash - var base = ''; + 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 = ''; + 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 @@ -1004,16 +1023,8 @@ fs.realpathSync = function realpathSync(p, cache) { base = previous + result[1]; pos = nextPartRe.lastIndex; - // continue if not a symlink, or if root - var isRoot = !base; - if (isWindows) { - // if it doens't have a tail, then it's the root. - var split = base.match(splitDeviceRe); - if (split) { - isRoot = !split[2]; - } - } - if (isRoot || knownHard[base] || (cache && cache[base] === base)) { + // continue if not a symlink + if (knownHard[base] || (cache && cache[base] === base)) { continue; } @@ -1050,8 +1061,7 @@ fs.realpathSync = function realpathSync(p, cache) { // resolve the link, then start over p = pathModule.resolve(resolvedLink, p.slice(pos)); - pos = 0; - previous = base = current = ''; + start(); } if (cache) cache[original] = p; @@ -1078,17 +1088,38 @@ fs.realpath = function realpath(p, cache, cb) { knownHard = {}; // current character position in p - var pos = 0; + var pos; // the partial path so far, including a trailing slash if any - var current = ''; - // the partial path without a trailing slash - var base = ''; + 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 = ''; + 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 - return process.nextTick(LOOP); function LOOP() { // stop if scanned past end of path if (pos >= p.length) { @@ -1104,16 +1135,8 @@ fs.realpath = function realpath(p, cache, cb) { base = previous + result[1]; pos = nextPartRe.lastIndex; - // continue if not a symlink, or if root - var isRoot = !base; - if (isWindows) { - // if it doens't have a tail, then it's the root. - var split = base.match(splitDeviceRe); - if (split) { - isRoot = !split[2]; - } - } - if (isRoot || knownHard[base] || (cache && cache[base] === base)) { + // continue if not a symlink + if (knownHard[base] || (cache && cache[base] === base)) { return process.nextTick(LOOP); } @@ -1163,13 +1186,9 @@ fs.realpath = function realpath(p, cache, cb) { } function gotResolvedLink(resolvedLink) { - // resolve the link, then start over p = pathModule.resolve(resolvedLink, p.slice(pos)); - pos = 0; - previous = base = current = ''; - - return process.nextTick(LOOP); + start(); } }; |