realpath方法使用说明,realpathSync方法使用说明

2019-07-24 作者:网站首页   |   浏览(79)

方法说明:

方法说明:

同步版的 fs.realpath()。

获取真实路径。

语法:

可以使用process.cwd解决相对路径。

复制代码 代码如下:

语法:

fs.realpathSync(path, [cache])

复制代码 代码如下:

由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) )

fs.realpath(path, [cache], [callback(err , resolvedPath)])

接收参数:

由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) )

path                             路径

接收参数:

cache                           可选,一个文字的映射路径可用于强制一个特定的路径解决或避免额外的fs.stat需要知道真正的路径对象。

path                             路径

例子:

cache                           可选,一个文字的映射路径可用于强制一个特定的路径解决或避免额外的fs.stat需要知道真正的路径对象。

复制代码 代码如下:

callback                       回调

var fs = require('fs');
 
// 点号表示当前文件所在路径
var str = fs.realpathSync('.');
console.log(str);

err                                异常

源码:

resolvedPath               真实地址

复制代码 代码如下:

例子:

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 (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 (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;
};

复制代码 代码如下:

您可能感兴趣的文章:

  • node.js中的fs.lstatSync方法使用说明
  • node.js中的fs.readFileSync方法使用说明
  • node.js中的fs.linkSync方法使用说明
  • node.js中的fs.symlink方法使用说明
  • node.js中的fs.utimes方法使用说明
  • node.js中的fs.unlinkSync方法使用说明

var cache = {'/etc':'/private/etc'};
fs.realpath('/etc/passwd', cache, function (err, resolvedPath) {
  if (err) throw err;
  console.log(resolvedPath);
});

源码:

复制代码 代码如下:

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 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();
  }
};

您可能感兴趣的文章:

  • node.js中的path.resolve方法使用说明
  • node.js中的path.join方法使用说明
  • node.js中的path.dirname方法使用说明
  • NodeJS学习笔记之(Url,QueryString,Path)模块
  • node.js中的path.extname方法使用说明
  • node.js中的path.sep方法使用说明
  • node.js中的path.normalize方法使用说明
  • node.js中的path.basename方法使用说明
  • node.js中的path.isAbsolute方法使用说明
  • Node.js中路径处理模块path详解

本文由yzc216亚洲城发布于网站首页,转载请注明出处:realpath方法使用说明,realpathSync方法使用说明

关键词: yzc216亚洲城