summaryrefslogtreecommitdiff
path: root/lib/fs.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fs.js')
-rw-r--r--lib/fs.js103
1 files changed, 61 insertions, 42 deletions
diff --git a/lib/fs.js b/lib/fs.js
index 076ddb50c..4aac1923a 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -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();
}
};