node.js中的fs.realpath方法使用说明_node.js

方法说明:

获取真实路径。

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

语法:

复制代码 代码如下:

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

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

接收参数:

path                             路径

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

callback                       回调

err                                异常

resolvedPath               真实地址

例子:

复制代码 代码如下:

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

时间: 2024-08-27 09:06:27

node.js中的fs.realpath方法使用说明_node.js的相关文章

node.js中的fs.realpathSync方法使用说明_node.js

方法说明: 同步版的 fs.realpath() . 语法: 复制代码 代码如下: fs.realpathSync(path, [cache]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require("fs") ) 接收参数: path                             路径 cache                           可选,一个文字的映射路径可用于强制一个特定的路径解决或避免额外的fs.stat需要知道真正的路径对象

node.js中的fs.lstat方法使用说明_node.js

方法说明: 获取文件信息(不解析符号链接). 语法: 复制代码 代码如下: fs.lstat(path, [callback(err, stats)]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require("fs") ) 接收参数: path   文件路径 callback  回调,传递两个参数,异常参数err, 文件信息数组 stats stats包含以下信息:(以下信息为案例中读取的文件信息,非默认值) 复制代码 代码如下: {    dev : 0 ,

node.js中的fs.fstat方法使用说明_node.js

方法说明: 根据文件描述符获取文件信息. 语法: 复制代码 代码如下: fs.fstat(fd, [callback(err, stats)]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require("fs") ) 接收参数: fd                 文件描述符 callback       回调,传递2个参数,异常参数err 和 文件信息参数 stats stats包含以下信息:(以下信息为案例中读取的文件信息,非默认值) 复制代码 代码如下:

node.js中的fs.write方法使用说明_node.js

方法说明: 写入文件(根据文件描述符),功能与 fs.writeFile() 类似,但该方法提供更底层的操作,实际应用中建议使用多 fs.writeFile()  . 该方法有两种形式: 1.fs.write(fd, buffer, offset, length[, position], [callback(err, bytesWritten, buffer)]) 这种写法将buffer写入文件(根据文件描述符fd来查找文件). 2.fs.write(fd, data[, position[,

node.js中的fs.writeFile方法使用说明_node.js

方法说明: 以异步的方式将data写入文件,文件已存在的情况下,原内容将被替换. 语法: 复制代码 代码如下: fs.writeFile(filename, data, [options], [callback(err)]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require("fs") ) 接收参数: filename      (String)            文件名称 data        (String | Buffer)    将要写入的内

node.js中的fs.writeFileSync方法使用说明_node.js

方法说明: 同步版的fs.writeFile()  . 语法: 复制代码 代码如下: fs.writeFileSync(filename, data, [options]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require("fs") ) 接收参数: filename      (String)            文件名称 data        (String | Buffer)    将要写入的内容,可以使字符串 或 buffer数据. optio

node.js中的fs.stat方法使用说明_node.js

方法说明: 获取文件信息. 语法: 复制代码 代码如下: fs.stat(path, [callback(err, stats)]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require("fs") ) 接收参数: path   文件路径 callback  回调,传递两个参数,异常参数err, 文件信息数组 stats stats包含以下信息:(以下信息为案例中读取的文件信息,非默认值) 复制代码 代码如下: {    dev : 0 ,    mode :

node.js中的fs.createReadStream方法使用说明_node.js

方法说明: 返回一个readStream(文件读取流,输入流)对象.(可读流) 语法: 复制代码 代码如下: fs.createReadStream(path, [options]) 由于该方法属于fs模块,使用前需要引入fs模块(var fs= require("fs") ) 接收参数: path: (string) 欲读取的文件路径 options : (object) 数组对象包含以下属性 复制代码 代码如下: { flags: 'r',   encoding: null,  

node.js中的fs.statSync方法使用说明_node.js

方法说明: 同步版的 stat() . 方法返回一个stat数组对象,包含以下信息:(以下信息为案例中读取的文件信息,非默认值) 复制代码 代码如下: {    dev : 0 ,    mode : 33206 ,    nlink : 1 ,    uid : 0 ,    gid : 0 ,    rdev : 0 ,    ino : 0 ,    size : 378(字节) ,    atime : Tue Jun 10 2014 13:57:13 GMT +0800 <中国标准时间