javascript - 如何解释从 statSync 返回的数字
问题描述
我想获取特定 .js 文件的文件状态。所以我使用了下面发布的代码。但第一个日志语句返回
33204
while the second one returned:
144
输出:
Stats {
dev: 2049,
mode: 33204,
nlink: 2,
uid: 1000,
}
但我想解释这些数字以了解文件是否可访问(读、写)
代码:
const fs = require('fs');
var mode = fs.statSync('../../var/opt/personal/guest/op/op_12201/data/persGuesOapDataFolder00/test0.js').mode;
var writePermissions = mode & 0x92; // 010010010
console.log(mode);
console.log(writePermissions);
解决方案
此模式指示有关文件的一些信息,例如:
- 权限(用于用户、组和其他)
- 文件类型
为了过滤这些数据,操作系统定义了一些掩码来提取我们需要的信息。这些面具是:
* For filter permissions (user, group and other)
The following mask values are defined for the file mode component of the st_mode field:
S_ISUID 04000 set-user-ID bit
S_ISGID 02000 set-group-ID bit (see below)
S_ISVTX 01000 sticky bit (see below)
S_IRWXU 00700 owner has read, write, and execute permission
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others (not in group) have read, write, and
execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
* For detect type of file
The following mask values are defined for the file type:
S_IFMT 0170000 bit mask for the file type bit field
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
场景 1:文件具有读取权限(适用于所有人)
const fs = require('fs');
const mode = fs.statSync('./yourfile.txt').mode;
if (mode & (fs.constants.S_IRUSR | fs.constants.S_IRGRP | fs.constants.S_IROTH)) {
console.log('file has read permissions');
}
场景二:是符号链接
if (mode & fs.constants.S_IFLNK) {
console.log("It's a symbolic link");
} else {
console.log("It's not a symbolic link");
}
希望这可以帮助您了解操作系统的工作原理(unix 系统)。更多信息: http: //man7.org/linux/man-pages/man7/inode.7.html
推荐阅读
- python - 理解中的下标元组解包表示法
- java - 滚动 ExpendableListView 后计数器的值发生变化
- html - 当一个小div的位置设置为固定时,为什么我的网站不会滚动?
- css - 如何将 a:hover CSS 应用于 H2,它位于同一个锚标记内?
- python-3.6 - Python 3.6 用户定义的板大小赢检查,带 2 个变量
- kdb - 如果我的变量存储从文件读取的数据,为什么它不受某些内置函数的影响?
- node.js - 如何使用 Node.js 中的 Mongoose 更新嵌套 json 数组中的可选字段?
- php - 复选框无法取消选中 php
- for-loop - 递归与迭代 - 何时使用一个而不是另一个
- polygon - 执行与地理相交的正确语法