📜  Node.js 文件系统。 lchownSync() 方法

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

Node.js 文件系统。 lchownSync() 方法

fs.lchownSync() 方法用于同步更改给定路径的所有者和组,但是,如果路径为 1,它不会取消引用符号链接,这与 fs.chownSync() 方法会取消引用指向它们的链接的链接。小路。
该函数接受可用于设置各自所有者和组的用户 ID 和组 ID。它不返回任何东西。
句法:

fs.lchownSync( fd, uid, gid )

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

  • fd:它是一个整数,表示必须更改所有者和组的文件的文件描述符。
  • uid:是一个数字,表示要设置的所有者对应的用户id。
  • gid:它是一个数字,表示与要设置的组对应的组ID。

下面的示例说明了 Node.js 中的fs.lchownSync() 方法
示例 1:此示例显示使用 lchownSync() 方法设置所有者和组。

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


javascript
// 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 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() 在取消引用符号链接方面的比较。

javascript

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