本篇记录Linux环境编程文件拥有者和组操作之chown, fchown, lchown, fchownat函数的基本用法。
首先查看帮助文档
这里先创建一个tom用户和tom组
sudo useradd tom
查看所有用户
cat /etc/passwd
查看所有组
getent group
1.chown函数用于更改文件的所有者和组。
函数名 | chown |
相关函数 | fchown, lchown, fchownat |
表头文件 | #include <unistd.h> |
函数定义 | int chown(const char *pathname, uid_t owner, gid_t group); |
函数说明 |
更改文件的所有者和组,如果它是一个符号链接,则会被取消引用. 参数说明: pathname:文件名 owner:所有者id group:组id |
返回值 | 成功时返回0,失败则返回 -1,错误码设置在errno中。 |
示例:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int main()
{
//const char *dirname = "/home/scott/trunk/command2";
const char *filelink = "/home/scott/trunk/command2/chmod_test1_link";
if(chown(filelink, 1002, 1002) == -1) {
perror("Error changing chmod_test1_link file owner");
}
else {
printf("changing chmod_test1_link file owner successfully\n");
}
const char *filename = "/home/scott/trunk/command2/chmod_test2.txt";
if(chown(filename, 1002, 1002) == -1) {
perror("Error changing chmod_test1.txt file owner");
}
else
{
printf("changing chmod_test1.txt file owner successfully\n");
}
cout << "Hello Ubuntu1804!" << endl;
return 0;
}
运行前:
运行后:
2.fchown函数用于更改文件的所有者和组。
函数名 | fchown |
相关函数 | chown, lchown, fchownat |
表头文件 | #include <unistd.h> |
函数定义 | int fchown(int fd, uid_t owner, gid_t group); |
函数说明 |
更改文件描述符对就的文件的所有者和组,如果它是一个符号链接,则会被取消引用。 参数说明: fd:文件描述符 owner:所有者id group:组id |
返回值 | 成功时返回0,失败则返回 -1,错误码设置在errno中。 |
示例:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int main()
{
const char *filename = "/home/scott/trunk/command2/chmod_test3.txt";
int fd = open(filename, O_RDONLY); // 打开文件以只读模式
if (fd == -1) {
perror("Failed to open file");
return 1;
}
if(fchown(fd, 1002, 1002) == -1) {
perror("Error changing chmod_test3.txt file owner");
}
else
{
printf("changing chmod_test3.txt file owner successfully\n");
}
cout << "Hello Ubuntu1804!" << endl;
return 0;
}
运行结果:
3.lchown函数用于更改文件的所有者和组。
函数名 | lchown |
相关函数 | fchown, chown, fchownat |
表头文件 | #include <unistd.h> |
函数定义 | int lchown(const char *pathname, uid_t owner, gid_t group); |
函数说明 |
更改文件的所有者和组,如果它是一个符号链接,也会引用。 参数说明: pathname:文件名 owner:所有者id group:组id |
返回值 | 成功时返回0,失败则返回 -1,错误码设置在errno中。 |
示例:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int main()
{
//const char *dirname = "/home/scott/trunk/command2";
const char *filelink = "/home/scott/trunk/command2/chmod_test1_link";
if(lchown(filelink, 1002, 1002) == -1) {
perror("Error changing chmod_test1_link file owner");
}
else {
printf("changing chmod_test1_link file owner successfully\n");
}
const char *filename = "/home/scott/trunk/command2/chmod_test4.txt";
if(lchown(filename, 1002, 1002) == -1) {
perror("Error changing chmod_test4.txt file owner");
}
else
{
printf("changing chmod_test4.txt file owner successfully\n");
}
cout << "Hello Ubuntu1804!" << endl;
return 0;
}
运行结果:
4.fchownat函数用于更改文件的所有者和组。
函数名 | fchownat |
相关函数 | fchown, lchown, chown |
表头文件 |
#include <unistd.h> #include <fcntl.h> |
函数定义 | int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags); |
函数说明 |
更改文件的所有者和组,如果它是一个符号链接,则会被取消引用。如是pathname是绝对文件名,则dirfd会被忽略。 参数说明:
|
返回值 | 成功时返回0,失败则返回 -1,错误码设置在errno中。 |
示例:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int main()
{
const char *dirname = "/home/scott/trunk/command2";
const char *filename = "chmod_test5.txt";
int dir_fd = open(dirname, O_RDONLY); // 打开文件以只读模式
if (dir_fd == -1) {
perror("Failed to open file");
return 1;
}
if(fchownat(dir_fd, filename, 1002, 1002, 0) == -1) {
perror("Error changing chmod_test5.txt file owner");
}
else
{
printf("changing chmod_test5.txt file owner successfully\n");
}
cout << "Hello Ubuntu1804!" << endl;
return 0;
}
运行结果: