fs.chmodSync()方法用于同步更改给定路径的权限。可以使用字符串常量或与它们各自的文件模式相对应的八进制数字来指定这些权限。
注意:Windows平台仅支持更改写权限。它还不支持区分用户权限,组权限或其他权限。
用法:
fs.chmodSync( path, mode )
参数:此方法接受上述和以下所述的两个参数:
- path:它是一个字符串,Buffer或URL,表示必须更改其权限的文件的路径。
- mode:它是字符串或八进制整数常量,表示要授予的权限。逻辑OR运算符可用于分隔多个权限。
以下示例说明了Node.js中的fs.chmodSync()方法:
范例1:此示例显示了使用字符串常量和OR运算符来授予文件权限的用法。
// Node.js program to demonstrate the
// fs.chmodSync method
// Import the filesystem module
const fs = require('fs');
// Allowing only read permission
console.log("Giving only read permission to user");
fs.chmodSync("example.txt", fs.constants.S_IRUSR);
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example.txt").mode);
// Reading the file
console.log("File Contents:",
fs.readFileSync("example.txt", 'utf8'));
// Trying to write to file
try {
console.log("Trying to write to file");
fs.writeFileSync('example.txt', "Hello");
}
catch (e) {
console.log("Error Found, Code:", e.code);
}
// Allowing both read and write permission
console.log("\nGiving both read and write"
+ " permissions to user");
fs.chmodSync("example.txt",
fs.constants.S_IRUSR | fs.constants.S_IWUSR);
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example.txt").mode);
console.log("Trying to write to file");
fs.writeFileSync('example.txt', "World");
console.log("File Contents:",
fs.readFileSync("example.txt", 'utf8'));
输出:
Giving only read permission to user Current File Mode:33024 File Contents:Hello Trying to write to file Error Found, Code:EACCES Giving both read and write permissions to user Current File Mode:33152 Trying to write to file File Contents:World
范例2:本示例说明使用八进制整数常量来授予文件权限。
// Node.js program to demonstrate the
// fs.chmodSync method
// Import the filesystem module
const fs = require('fs');
// Allowing only read permission
console.log("Giving only read permission to everyone");
fs.chmodSync("example.txt", 0o444);
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example.txt").mode);
// Reading the file
console.log("File Contents:",
fs.readFileSync("example.txt", 'utf8'));
// Trying to write to file
try {
console.log("Trying to write to file");
fs.writeFileSync('example.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.chmodSync("example.txt", 0o666);
// Check the file mode
console.log("Current File Mode:",
fs.statSync("example.txt").mode);
console.log("Trying to write to file");
fs.writeFileSync('example.txt', "World");
console.log("File Contents:",
fs.readFileSync("example.txt", 'utf8'));
输出:
Giving only read permission to everyone Current File Mode:33060 File Contents:Hello 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:World
参考: https://nodejs.org/api/fs.html#fs_fs_chmodsync_path_mode
相关用法
- Node.js GM minify()用法及代码示例
- Node.js GM magnify()用法及代码示例
- Node.js GM write()用法及代码示例
- Node.js GM drawRectangle()用法及代码示例
- Node.js GM whitePoint()用法及代码示例
- Node.js GM whiteThreshold()用法及代码示例
- Node.js GM modulate()用法及代码示例
- Node.js GM drawEllipse()用法及代码示例
- Node.js GM drawCircle()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
注:本文由纯净天空筛选整理自sayantanm19大神的英文原创作品 Node.js | fs.chmodSync() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。