文件的模式


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:

    ConstantOctalDescription
    fs.constants.S_IRUSR0o400read by owner
    fs.constants.S_IWUSR0o200write by owner
    fs.constants.S_IXUSR0o100execute/search by owner
    fs.constants.S_IRGRP0o40read by group
    fs.constants.S_IWGRP0o20write by group
    fs.constants.S_IXGRP0o10execute/search by group
    fs.constants.S_IROTH0o4read by others
    fs.constants.S_IWOTH0o2write by others
    fs.constants.S_IXOTH0o1execute/search by others

    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.

    NumberDescription
    7read, write, and execute
    6read and write
    5read and execute
    4read only
    3write and execute
    2write only
    1execute only
    0no permission

    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.

    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.

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