📜  Node.js fs.fchmodSync() 方法

📅  最后修改于: 2022-05-13 01:56:53.001000             🧑  作者: Mango

Node.js fs.fchmodSync() 方法

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