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


Node.js fs.lchownSync()用法及代碼示例

fs.lchownSync()方法用於同步更改給定路徑的所有者和組,但是,如果路徑為1,則它不引用符號鏈接,這與fs.chownSync()方法確實取消引用鏈接到其路徑是不同的。

該函數接受可用於設置各自所有者和組的用戶ID和組ID。它不返回任何東西。

用法:

fs.lchownSync( fd, uid, gid )

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

  • fd:它是一個整數,表示必須更改所有者和組的文件的文件描述符。
  • uid:該數字表示與要設置的所有者相對應的用戶ID。
  • gid:該數字表示與要設置的組相對應的組ID。

以下示例說明了Node.js中的fs.lchownSync()方法:



範例1:本示例使用lchownSync()方法顯示所有者和組的設置。

// Node.js program to demonstrate the 
// fs.lchownSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
let filepath = "example_file.txt"; 
let symlinkpath = "symlinkFile"; 
  
// Create a symlink to the file 
fs.symlinkSync(filepath, symlinkpath); 
  
// Set the owner and group of the symbolic 
// link to a new one 
// New owner is "geeksforgeeks" with user id 1200 
// New group is "editor" with group id 1201 
fs.lchownSync(symlinkpath, 1200, 1201); 
console.log("Given uid and gid set successfully");

在運行代碼之前:

xubuntu@xubuntu:~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 05:10 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 05:15 index.js

代碼輸出:

Given uid and gid set successfully

運行代碼後:

xubuntu@xubuntu:~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 xubuntu xubuntu 4 Apr 26 05:10 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 290 Apr 26 05:15 index.js
lrwxrwxrwx 1 geeksforgeeks editor 16 Apr 26 09:15 symlinkFile -> example_file.txt

範例2:本示例顯示了chownSync()和lchownSync()在取消引用符號鏈接方麵的比較。

// Node.js program to demonstrate the 
// fs.lchownSync() method 
  
// Import the filesystem module 
const fs = require('fs'); 
  
let filepath = "example_file.txt"; 
let symlinkpath1 = "symlinkFile1"; 
let symlinkpath2 = "symlinkFile2"; 
  
// Create two symlinks to the file 
fs.symlinkSync(filepath, symlinkpath1); 
fs.symlinkSync(filepath, symlinkpath2); 
  
// Use lchownSync() on the first symlink 
// New owner is "geeksforgeeks" with user id 1200 
// New group is "owner" with group id 998 
fs.lchownSync(symlinkpath1, 1200, 998); 
console.log("lchownSync:uid and gid set successfully"); 
  
// Use chownSync() on the second symLink 
// New owner is "sam" with user id 1100 
// New group is "editor" with group id 1201 
fs.chownSync(symlinkpath2, 1100, 1201); 
console.log("chownSync:uid and gid set successfully");

在運行代碼之前:

xubuntu@xubuntu:~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 xubuntu xubuntu 6 Apr 26 09:15 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 777 Apr 26 09:16 index.js

代碼輸出:

lchownSync:uid and gid set successfully
chownSync:uid and gid set successfully

運行代碼後:

xubuntu@xubuntu:~/Desktop/fs-lchownSync$ ls -l
total 4
-rw-rw--w- 1 sam editor 6 Apr 26 09:15 example_file.txt
-rw-rw-r-- 1 xubuntu xubuntu 777 Apr 26 09:16 index.js
lrwxrwxrwx 1 geeksforgeeks owner 16 Apr 26 09:15 symlinkFile1 -> example_file.txt
lrwxrwxrwx 1 root root 16 Apr 26 09:15 symlinkFile2 -> example_file.txt

參考: https://nodejs.org/api/fs.html#fs_fs_lchownsync_path_uid_gid




相關用法


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