fs.fchmodSync()方法是fs模塊的內置應用程序編程,用於同步更改給定文件描述符的權限。可以使用字符串常量或與它們各自的文件模式相對應的八進製數將這些權限指定為參數。
注意:Windows平台僅支持更改寫權限。它還不支持區分用戶權限,組權限或其他權限。
用法:
fs.fchmodSync( fd, mode )
參數:此方法接受上述和以下所述的兩個參數:
- fd:它是一個整數,表示必須更改權限的文件的文件描述符。
- mode:它是字符串常量或八進製常量,表示要授予的權限。邏輯OR運算符可用於分隔多個權限。
以下示例說明了Node.js中的fs.fchmodSync方法:
範例1:此示例顯示了使用字符串常量和OR運算符來授予文件權限的用法。
// Node.js program to demonstrate the
// fs.fchmodSync 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 user");
fs.fchmodSync(fd, fs.constants.S_IRUSR);
// 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 user");
fs.fchmodSync(fd, fs.constants.S_IRUSR | fs.constants.S_IWUSR);
// 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 user Current File Mode:33024 File Contents:Hello World Trying to write to file Error Found, Code:EACCES Giving both read and write permission to 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.fchmodSync 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.fchmodSync(fd, 0o444);
// 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.fchmodSync(fd, 0o666);
// 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:Hello World 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_fchmodsync_fd_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.fchmodSync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。