我想获取特定.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);
答案 0 :(得分:0)
此模式表示有关文件的一些信息,如:
为了过滤这个数据,OS上定义了一些掩码来提取我们需要的信息。这些面具是:
* 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');
}
场景2:是符号链接
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