文件模式
【File modes】
在 fs.chmod() 和 fs.chmodSync() 方法中使用的 mode 参数是一个数字位掩码,通过以下常量的逻辑或创建:
【The mode argument used in both the fs.chmod() and fs.chmodSync()
methods is a numeric bitmask created using a logical OR of the following
constants:】
| 常量 | 八进制 | 描述 |
|---|---|---|
fs.constants.S_IRUSR | 0o400 | 拥有者可读 |
fs.constants.S_IWUSR | 0o200 | 拥有者可写 |
fs.constants.S_IXUSR | 0o100 | 拥有者可执行/搜索 |
fs.constants.S_IRGRP | 0o40 | 组成员可读 |
fs.constants.S_IWGRP | 0o20 | 组成员可写 |
fs.constants.S_IXGRP | 0o10 | 组成员可执行/搜索 |
fs.constants.S_IROTH | 0o4 | 其他人可读 |
fs.constants.S_IWOTH | 0o2 | 其他人可写 |
fs.constants.S_IXOTH | 0o1 | 其他人可执行/搜索 |
构造 mode 的一个更简单的方法是使用由三个八进制数字组成的序列(例如 765)。最左边的数字(在例子中是 7)指定文件所有者的权限。中间的数字(在例子中是 6)指定组的权限。最右边的数字(在例子中是 5)指定其他用户的权限。
【An easier method of constructing the mode is to use a sequence of three
octal digits (e.g. 765). The left-most digit (7 in the example), specifies
the permissions for the file owner. The middle digit (6 in the example),
specifies permissions for the group. The right-most digit (5 in the example),
specifies the permissions for others.】
| 数字 | 描述 |
|---|---|
7 | 读、写和执行 |
6 | 读和写 |
5 | 读和执行 |
4 | 只读 |
3 | 写和执行 |
2 | 只写 |
1 | 只执行 |
0 | 无权限 |
例如,八进制值 0o765 表示:
【For example, the octal value 0o765 means:】
- 文件所有者可以读取、写入和执行该文件。
- 该组可以读取和写入该文件。
- 其他人可以读取和执行该文件。
在期望使用文件模式的情况下使用原始数字时,任何大于 0o777 的值可能会导致平台特定的行为,这些行为无法保证一致。因此,像 S_ISVTX、S_ISGID 或 S_ISUID 这样的常量在 fs.constants 中未被暴露。
【When using raw numbers where file modes are expected, any value larger than
0o777 may result in platform-specific behaviors that are not supported to work
consistently. Therefore constants like S_ISVTX, S_ISGID, or S_ISUID are
not exposed in fs.constants.】
注意事项:在 Windows 上,只有写权限可以更改,并且没有实现对组、所有者或其他用户权限的区分。
【Caveats: on Windows only the write permission can be changed, and the distinction among the permissions of group, owner, or others is not implemented.】