📜  Node.js fs.chmodSync() 方法

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

Node.js fs.chmodSync() 方法

fs.chmodSync() 方法用于同步更改给定路径的权限。这些权限可以使用对应于它们各自文件模式的字符串常量或八进制数来指定。
注意: Windows 平台只支持修改写权限。它也不支持区分用户、组或其他人的权限。
句法:

fs.chmodSync( path, mode )

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

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

下面的示例说明了 Node.js 中的fs.chmodSync() 方法
示例 1:此示例显示了使用字符串常量和 OR运算符来授予文件权限。

javascript
// 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'));


javascript
// 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 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:此示例显示了使用八进制整数常量来授予文件权限。

javascript

// 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