Method Description:
Get the real path.
You can use PROCESS.CWD to resolve relative paths.
Grammar:
Copy Code code as follows:
Fs.realpath (path, [cache], [Callback (Err, Resolvedpath)]
Because this method belongs to the FS module, it is necessary to introduce FS module (VAR fs= require ("FS") before use.
Receive parameters:
Path paths
Cache optional, a text mapping path can be used to force a specific path to resolve or avoid additional fs.stat needs to know the true path object.
Callback Callback
Err exception
Resolvedpath Real Address
Example:
Copy Code code as follows:
var cache = {'/etc ': '/private/etc '};
Fs.realpath ('/etc/passwd ', cache, function (err, Resolvedpath) {
if (err) throw err;
Console.log (Resolvedpath);
});
Source:
Copy Code code as follows:
Fs.realpath = function Realpath (p, cache, CB) {
if (!util.isfunction (CB)) {
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]);
}
var original = P,
Seenlinks = {},
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 this root exists. On the 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 + ': ' + 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 ();
}
};