fs.chownSync()方法用於同步更改給定路徑的所有者和組。該函數接受用戶ID和組ID,這些用戶ID和組ID可用於設置各自的所有者和組。它不返回任何東西。
用法:
fs.chownSync( path, uid, gid )
參數:該方法接受上述和以下描述的三個參數:
- path:它是一個字符串,Buffer或URL,表示必須更改所有者和組的文件的路徑。
 - uid:該數字表示與要設置的所有者相對應的用戶ID。
 - gid:該數字表示與要設置的組相對應的組ID。
 
以下示例說明了Node.js中的fs.chownSync()方法:
範例1:本示例顯示所有者的設置。
// Node.js program to demonstrate the 
// fs.chownSync() 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 1100 
// The old group is "xubuntu" with group id 999 
fs.chownSync(filepath, 1100, 999); 
console.log("Given uid and gid set successfully");在運行代碼之前:
xubuntu@xubuntu:~/Desktop/fs-chownSync$ ls -l total 8 -rw-rw--w- 1 xubuntu xubuntu 4 Apr 23 09:08 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 23 09:08 index.js
代碼輸出:
Given uid and gid set successfully
運行代碼後:
xubuntu@xubuntu:~/Desktop/fs-chownSync$ ls -l total 8 -rw-rw--w- 1 geeksforgeeks xubuntu 4 Apr 23 09:08 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 23 09:08 index.js
範例2:本示例顯示了組的設置。
// Node.js program to demonstrate the 
// fs.chownSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
let filepath = "example_file.txt"; 
  
// Set the group to a new one keeping the owner same 
// New group is "editor" with group id 1201 
fs.chownSync(filepath, 999, 1201); 
console.log("Given uid and gid set successfully");在運行代碼之前:
xubuntu@xubuntu:~/Desktop/fs-chownSync$ ls -l total 8 -rw-rw--w- 1 xubuntu xubuntu 4 Apr 23 09:08 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 23 09:08 index.js
代碼輸出:
Given uid and gid set successfully
運行代碼後:
xubuntu@xubuntu:~/Desktop/fs-chownSync$ ls -l total 8 -rw-rw--w- 1 xubuntu editor 4 Apr 23 09:08 example_file.txt -rw-rw-r-- 1 xubuntu xubuntu 290 Apr 23 09:08 index.js
參考: https://nodejs.org/api/fs.html#fs_fs_chownsync_path_uid_gid
相關用法
- Node.js GM thumbnail()用法及代碼示例
 - Node.js GM raise()用法及代碼示例
 - Node.js GM resize()用法及代碼示例
 - Node.js GM transparent()用法及代碼示例
 - Node.js GM sepia()用法及代碼示例
 - Node.js GM magnify()用法及代碼示例
 - Node.js GM threshold()用法及代碼示例
 - Node.js GM whiteThreshold()用法及代碼示例
 - Node.js GM whitePoint()用法及代碼示例
 - Node.js GM write()用法及代碼示例
 
注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 Node.js | fs.chownSync() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
