Use of fs. realpathSync in node. js _ node. js

Source: Internet
Author: User
Tags hasownproperty
This article mainly introduces node. fs. realpathSync method usage instructions. This article introduces fs. realpathSync method description, syntax, receiving parameters, use instances, and implementation source code. For more information, see Method description:

Synchronous version of fs. realpath ().

Syntax:

The Code is as follows:


Fs. realpathSync (path, [cache])

Because this method belongs to the fs module, we need to introduce the fs module (var fs = require ("fs") before use "))

Receiving parameters:

Path

Cache (optional) The ing path of a text can be used to force a specific path to solve the problem or avoid the need for extra fs. stat to know the real path object.

Example:

The Code is as follows:


Var fs = require ('fs ');

// Point number indicates the path of the current file
Var str = fs. realpathSync ('.');
Console. log (str );

Source code:

The Code is as follows:


Fs. realpathSync = function realpathSync (p, cache ){
// Make p is absolute
P = pathModule. resolve (p );
If (cache & Object. prototype. hasOwnProperty. call (cache, p )){
Return 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 (Response t 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.exe c (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.exe c (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 (util. isNull (linkTarget )){
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;
};

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.