Node.js fs.fchmod() 方法
fs.fchmod() 方法用于更改给定文件描述符的权限。这些权限可以使用与它们各自的文件模式对应的字符串常量或八进制数指定为参数。
注意: Windows 平台只支持修改写权限。它不支持区分用户、组或其他人的权限。
句法:
fs.fchmod( fd, mode, callback )
参数:此方法接受三个参数,如上所述,如下所述:
- fd:整数值,表示需要更改权限的文件的文件描述符。
- mode:字符串常量或八进制常量,表示被授予的权限。逻辑 OR运算符可用于分隔多个权限。
- 回调:它是一个在方法执行时将被调用的函数。
- err:如果方法失败会抛出一个错误。
下面的示例说明了 Node.js 中的fs.fchmod() 方法:
示例 1:此示例显示了使用字符串常量和 OR运算符来授予文件权限。
// Node.js program to demonstrate the
// fs.fchmod() method
// Import the filesystem module
const fs = require('fs');
// Getting the file descriptor
const fd = fs.openSync('example_file.txt', 'r');
// Allowing only read permission
console.log("Giving only read permission to the user");
fs.fchmod(fd, fs.constants.S_IRUSR, (err) => {
if (err) throw err;
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example_file.txt").mode);
// Reading the file
console.log("File Contents:",
fs.readFileSync("example_file.txt", 'utf8'));
// Trying to write to file
try {
console.log("Trying to write to file");
fs.writeFileSync('example_file.txt', "Hello");
}
catch (e) {
console.log("Error Found, Code:", e.code);
}
// Allowing both read and write permission
console.log("\nGiving both read and write "
+ "permission to the user");
fs.fchmod(fd, fs.constants.S_IRUSR |
fs.constants.S_IWUSR, (err) => {
if (err) throw err;
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example_file.txt").mode);
console.log("Trying to write to file");
fs.writeFileSync('example_file.txt',
"This file has been written over.");
console.log("File Contents:",
fs.readFileSync("example_file.txt", 'utf8'));
});
});
输出:
Giving only read permission to the user
Current File Mode: 33024
File Contents: This file has been written over.
Trying to write to file
Error Found, Code: EACCES
Giving both read and write permission to the user
Current File Mode: 33152
Trying to write to file
File Contents: This file has been written over.
示例 2:此示例显示了使用八进制常量来授予文件权限。
// Node.js program to demonstrate the
// fs.fchmod() method
// Import the filesystem module
const fs = require('fs');
// Getting the file descriptor
const fd = fs.openSync('example_file.txt', 'r');
// Allowing only read permission
console.log("Giving only read permission to everyone");
fs.fchmod(fd, 0o444, (err) => {
if (err) throw err;
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example_file.txt").mode);
// Reading the file
console.log("File Contents:",
fs.readFileSync("example_file.txt", 'utf8'));
// Trying to write to file
try {
console.log("Trying to write to file");
fs.writeFileSync('example_file.txt', "Hello");
}
catch (e) {
console.log("Error Found, Code:", e.code);
}
// Allowing both read and write permission
console.log("\nGiving both read and write "
+ "permission to everyone");
fs.fchmod(fd, 0o666, (err) => {
if (err) throw err;
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example_file.txt").mode);
console.log("Trying to write to file");
fs.writeFileSync('example_file.txt',
"This file has been written over.");
console.log("File Contents:",
fs.readFileSync("example_file.txt", 'utf8'));
});
});
输出:
Giving only read permission to everyone
Current File Mode: 33060
File Contents: This file has been written over.
Trying to write to file
Error Found, Code: EACCES
Giving both read and write permission to everyone
Current File Mode: 33206
Trying to write to file
File Contents: This file has been written over.
参考: https://nodejs.org/api/fs.html#fs_fs_fchmod_fd_mode_callback