當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js fsPromises.chown()用法及代碼示例

這個fsPromises.chown()方法用於hange文件的所有權,然後解析承諾沒有成功的爭論。該函數接受可用於設置各自所有者和組的用戶ID和組ID。

用法:

fsPromises.chown( path, uid, gid)

參數:此方法接受上述和以下所述的三個參數:

  • path:它是一個字符串,緩衝區或URL,表示必須更改所有者和組的文件的路徑。
  • uid:這是一個整數,表示與要設置的所有者相對應的用戶ID。
  • gid:這是一個整數,表示與要設置的組相對應的組ID。

Javascript

// Node.js program to demonstrate the  
// fsPromises.chown() method  
  
// Import the filesystem module  
const fs = require('fs');  
  
let filepath = "example_file.txt";  
  
// Set the owner to a new one keeping the group same  
// New owner is "GeeksforGeeks" with user id 4567 
fsPromises.chown(filepath, 4567, 999 => {  
    console.log("uid and gid set successfully.");  
}); 

在運行代碼之前:

xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js

代碼輸出:



Given uid and gid set successfully
.

運行代碼後:

xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 geeksforgeeks xubuntu 4 May 26 04:08 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:08 index.js

範例2:本示例顯示了組的設置。

Javascript

// Node.js program to demonstrate the  
// fsPromises.chown() method  
  
// Import the filesystem module  
const fs = require('fs');  
  
let filepath = "example_file.txt";  
  
// Set the owner and group both to a new one  
// New owner is "nitin" with owner id 8900  
// New group is "author" with group id 1024  
fsPromises.chown(filepath, 8900, 1024 => {  
  
console.log("uid and gid set successfully!");  
  
}); 

在運行代碼之前:

xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 xubuntu xubuntu 4 May 26 04:19 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js

代碼輸出:

Given uid and gid set successfully!

運行代碼後:

xubuntu@xubuntu:~/Desktop/fs-chown$ ls -l
total 8
-rw-rw--w- 1 nitin author 4 May 26 04:19 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 May 26 04:19 index.js

相關用法


注:本文由純淨天空篩選整理自nitin_sharma大神的英文原創作品 Node.js | fsPromises.chown() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。