文件模式


¥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_IRUSR0o400由所有者可读
fs.constants.S_IWUSR0o200由所有者可写
fs.constants.S_IXUSR0o100由所有者可执行/搜索
fs.constants.S_IRGRP0o40由群组可读
fs.constants.S_IWGRP0o20由群组可写
fs.constants.S_IXGRP0o10由群组可执行/搜索
fs.constants.S_IROTH0o4由其他人可读
fs.constants.S_IWOTH0o2由其他人可写
fs.constants.S_IXOTH0o1由其他人可执行/搜索

构建 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:

  • 所有者可以读取、写入、以及执行文件。

    ¥The owner may read, write, and execute the file.

  • 群组可以读取和写入文件。

    ¥The group may read and write the file.

  • 其他人可以读取和执行文件。

    ¥Others may read and execute the file.

在需要文件模式的地方使用原始数字时,任何大于 0o777 的值都可能导致特定于平台的行为不支持一致工作。因此,像 S_ISVTXS_ISGIDS_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 上只能修改写权限,没有实现 group、owner、其他权限的区分。

¥Caveats: on Windows only the write permission can be changed, and the distinction among the permissions of group, owner, or others is not implemented.