node.js中的fs.realpathSync方法使用说明
作者:junjie 时间:2024-05-13 09:59:04
方法说明:
同步版的 fs.realpath() 。
语法:
fs.realpathSync(path, [cache])
由于该方法属于fs模块,使用前需要引入fs模块(var fs= require(“fs”) )
接收参数:
path 路径
cache 可选,一个文字的映射路径可用于强制一个特定的路径解决或避免额外的fs.stat需要知道真正的路径对象。
例子:
var fs = require('fs');
// 点号表示当前文件所在路径
var str = fs.realpathSync('.');
console.log(str);
源码:
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.realpathSync
0
投稿
猜你喜欢
python实现扫雷小游戏
2023-02-15 11:58:58
Python基于numpy灵活定义神经网络结构的方法
2023-09-30 15:03:52
公网远程访问局域网SQL Server数据库
2024-01-22 01:38:21
Python可视化工具Plotly的应用教程
2022-03-28 01:47:02
go GCM gin中间件的加密解密文件流处理
2024-04-26 17:32:36
PHP执行linux系统命令的常用函数使用说明
2023-10-19 11:28:39
python 正则式 概述及常用字符
2023-01-14 14:50:54
pandas.DataFrame.to_json按行转json的方法
2022-11-09 11:24:23
JavaScript 基础问答 四
2024-04-18 10:52:09
mysql 判断是否为子集的方法步骤
2024-01-26 03:53:11
css中如何使div居中(垂直水平居中)
2007-08-13 08:17:00
Python与Appium实现手机APP自动化测试的示例代码
2023-07-26 05:06:07
PyQt5中QSpinBox计数器的实现
2022-09-13 01:43:09
CMD命令操作MySql数据库的方法详解
2024-01-16 08:31:57
输入法下keyup失效的解决方案
2007-11-01 12:57:00
JS+CSS实现闪烁字体效果代码
2024-04-18 09:31:04
.NET framework 4.0 安装失败回滚问题
2023-07-11 15:50:35
JavaScript数值千分位格式化的两种简单实现方法
2023-08-31 22:59:43
MySQL用truncate命令快速清空一个数据库中的所有表
2024-01-18 16:05:29
Python lambda表达式原理及用法解析
2021-03-02 18:52:12