📜  Node.js fsPromises.chmod() 方法

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

Node.js fsPromises.chmod() 方法

fsPromises.chmod()方法用于更改给定路径的权限。这些权限可以使用对应于它们各自文件模式的字符串常量或八进制数来指定。

注意: Windows 平台只支持修改写权限。它不支持区分用户、组或其他人的权限。它更改文件的权限,然后在成功时不带参数地解析 Promise。

句法:

fsPromises.chmod( path, mode)

参数:此方法接受上面提到的两个参数,如下所述:

  • path:字符串、Buffer或URL,表示需要更改权限的文件的路径。
  • mode:字符串或八进制整数常量,表示要授予的权限。逻辑或运算符可用于分隔多个权限。

下面的示例说明了 Node.js 中的 fsPromises.chmod() 方法:

示例 1:此示例显示了使用八进制整数常量来授予文件权限。

// Node.js program to demonstrate the 
// fsPromises.chmod() method 
     
// Import the filesystem module 
const fs = require('fs'); 
const fsPromises = require('fs').promises;
  
console.log("Granting only read access to user"); 
     
fsPromises.chmod("example.txt", 0o400 ) 
  
console.log("\nReading the file contents"); 
  
console.log(fs.readFileSync("example.txt", 'utf8')); 
     
console.log("\nTrying to write to file"); 
    
try { 
    fs.writeFileSync('example.txt', 
    "This file has now been edited."); 
} 
catch (e) { 
    console.log("Error Code:", e.code); 
} 
     
console.log("\nGranting read and write access to user"); 
  
fsPromises.chmod("example.txt", 0o600 ) 
  
console.log("Trying to write to file"); 
  
fs.writeFileSync('example.txt', 
    "This file has now been edited."); 
    
console.log("\nReading the file contents"); 
console.log(fs.readFileSync("example.txt", 'utf8')); 

输出:

Granting only read access to user

Reading the file contents     
This file has now been edited.

Trying to write to file       
Error Code: EPERM

Granting read and write access to user
Trying to write to file

Reading the file contents
This file has now been edited.

示例 2:此示例显示了使用字符串常量和或运算符来授予文件权限

// Node.js program to demonstrate the 
// fsPromises.chmod() method 
    
// Import the filesystem module 
const fs = require('fs'); 
const fsPromises = fs.promises;
    
// Grant only read permission to user 
console.log("Granting only read access to user"); 
fsPromises.chmod("example.txt", fs.constants.R_OK);
    
// Reading the file 
console.log("File Contents:", 
    fs.readFileSync("example.txt", 'utf8')); 
    
// Trying to write to file 
try { 
  console.log("\nTrying to write to file"); 
  fs.writeFileSync('example.txt', 
    "This file now has been edited."); 
} 
catch (e) { 
  console.log(
   "Error Occurred, Error Code:", e.code); 
} 
    
// Granting both read and 
// write permission 
console.log("\nGranting both read and "
        + 'write permission to user"); 
fsPromises.chmod("example.txt", 
   fs.constants.R_OK | fs.constants.W_OK);
  
// Check the file mode 
console.log("Current File Mode:", 
    fs.statSync("example.txt").mode); 
  
console.log("Trying to write to file"); 
try
{
  fs.writeFileSync('example.txt', 
    "This file now has been edited."); 
} catch (e) {
  console.log("Error ", e.code);
}
  
console.log("File Contents:", 
    fs.readFileSync("example.txt", 'utf8')); 

输出:

Granting only read access to user
File Contents: This file has now been edited.  

Trying to write to file
Error Occurred, Error Code: EPERM

Granting both read and write permission to user
Current File Mode: 33060
Trying to write to file
Error  EPERM
File Contents: This file has now been edited.

参考: https://nodejs.org/api/fs.html#fs_fspromises_chmod_path_mode