fs.fchmod()方法用于更改给定文件描述符的权限。可以使用字符串常量或与它们各自的文件模式相对应的八进制数将这些权限指定为参数。
注意:Windows平台仅支持更改写权限。它不支持区分用户权限,组权限或其他权限。
用法:
fs.fchmod( fd, mode, callback )
参数:此方法接受上述和以下所述的三个参数:
- fd:它是一个整数值,表示必须更改权限的文件的文件描述符。
- mode:它是字符串常量或八进制常量,表示要授予的权限。逻辑OR运算符可用于分隔多个权限。
- callback:该方法执行时将调用该函数。
- 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
相关用法
- Node.js GM resize()用法及代码示例
- Node.js GM threshold()用法及代码示例
- Node.js GM thumbnail()用法及代码示例
- Node.js GM transparent()用法及代码示例
- Node.js GM lower()用法及代码示例
- Node.js GM contrast()用法及代码示例
- Node.js GM negative()用法及代码示例
- Node.js GM scale()用法及代码示例
- Node.js GM operator()用法及代码示例
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.fchmod() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。