📜  Node.js fs.chmod() 方法

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

Node.js fs.chmod() 方法

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

注意: Windows 平台只支持修改写权限。它也不支持区分用户、组或其他人的权限。

句法:

fs.chmod( path, mode, callback )

参数:此方法接受三个参数,如上所述,如下所述:

  • path:字符串、Buffer或URL,表示需要更改权限的文件的路径。
  • mode:字符串或八进制整数常量,表示要授予的权限。逻辑 OR运算符可用于分隔多个权限。
  • 回调:它是一个在方法执行时将被调用的函数。
    • err:如果方法失败会抛出一个错误。

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

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

// Node.js program to demonstrate the
// fs.chmod() method
  
// Import the filesystem module
const fs = require('fs');
  
// Grant only read permission to user
console.log("Granting only read access to user");
  
fs.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);
  }
  
  // Grant both read and write permission to user
  console.log("\nGranting read and write access to user");
  fs.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 is an example text file.

Trying to write to file
Error Code: EACCES

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

Reading the file contents
This file has now been edited.

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

// Node.js program to demonstrate the
// fs.chmod() method
  
// Import the filesystem module
const fs = require('fs');
  
// Grant only read permission to user
console.log("Granting only read access to user");
fs.chmod("example.txt", fs.constants.S_IRUSR, () => {
  
  // 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");
  fs.chmod("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', "This file now has been edited.");
  
    console.log("File Contents:", fs.readFileSync("example.txt", 'utf8'));
  });
});

输出:

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

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

Granting both read and write permission to user
Trying to write to file
File Contents: This file now has been edited.

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