本篇记录Linux环境编程文件权限操作之chmod, fchmod, fchmodat的基本用法。
首先查看帮助文档
1.chmod函数用于修改文件权限。
函数名 | chmod |
相关函数 | fchmod, fchmodat |
表头文件 | #include <sys/stat.h> |
函数定义 | int chmod(const char *pathname, mode_t mode); |
函数说明 | chmod 函数用于修改文件权限,它允许用户设置文件的读(r)、写(w)和执行(x)权限,以及特殊权限,如设置用户ID(setuid,简称suid)和粘滞位(sticky bit)。
mode:指定新的权限模式,与 open 函数的第三个参数一样。权限模式可以通过运算符" | "组合: S_IRWXU:为文件所有者设置读、写和执行权限。 |
返回值 | 成功时返回0,失败则返回 -1,错误码设置在errno中。 |
示例:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int main()
{
//要修改权限的文件名
const char *filename1 = "/home/scott/trunk/command2/chmod_teset1.txt";
mode_t mode1 = 0644; // 设置新的权限为 644
if (chmod(filename1, mode1)) {
perror("chmod Failed =====1");
return EXIT_FAILURE;
}
const char *filename2 = "/home/scott/trunk/command2/chmod_teset2.txt";
mode_t mode2 = S_IRUSR | S_IWUSR | S_IXUSR; // 设置文件所有者的读、写和执行权限
if (chmod(filename2, mode2)) {
perror("chmod Failed =====2");
return EXIT_FAILURE;
}
printf("File permissions changed successfully.\n");
cout << "Hello Ubuntu1804!" << endl;
return 0;
}
运行结果:
2.fchmod函数用于修改文件权限。
函数名 | fchmod |
相关函数 | chmod, fchmodat |
表头文件 | #include <sys/stat.h> |
函数定义 | int fchmod(int fd, mode_t mode); |
函数说明 | fchmod 函数用于修改文件权限。
mode:指定新的权限模式 |
返回值 | 成功时返回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 *filename3 = "/home/scott/trunk/command2/chmod_teset3.txt";
// 打开或创建文件
int fd3 = open(filename3, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
mode_t mode3 = 0644; // 设置新的权限为 644
if (fchmod(fd3, mode3)) {
perror("chmod Failed =====3");
return EXIT_FAILURE;
}
close(fd3);
const char *filename4 = "/home/scott/trunk/command2/chmod_teset4.txt";
mode_t mode4 = S_IRUSR | S_IWUSR | S_IXUSR; // 设置文件所有者的读、写和执行权限
// 打开或创建文件
int fd4 = open(filename4, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fchmod(fd4, mode4)) {
perror("chmod Failed =====4");
return EXIT_FAILURE;
}
close(fd4);
printf("modd3====%d mode4===%d\n", mode3, mode4);
printf("File permissions changed successfully.\n");
cout << "Hello Ubuntu1804!" << endl;
return 0;
}
运行结果:
3.fchmodat函数用于修改文件权限。
函数名 | fchmodat |
相关函数 | chmod, fchmod |
表头文件 | #include <sys/stat.h> |
函数定义 | int fchmodat(int dirfd, const char *pathname, mode_t mode, int flags); |
函数说明 | fchmodat 函数用于修改文件权限。 参数说明:
|
返回值 | 成功时返回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_teset6.txt";
mode_t new_mode = 0644; // 设置新的权限为 644
// 打开或创建文件
int dir_fd = open(dirname, O_RDONLY | O_DIRECTORY);
if(dir_fd == -1)
{
perror("open directory");
exit(EXIT_FAILURE);
}
// 使用 fchmodat 修改文件权限
if(fchmodat(dir_fd, filename, new_mode, 0) == -1)
{
perror("fchmodat failed");
return 1;
}
else
{
printf("File '%s/%s' permissions successfully changed to %o\n", dirname, filename, new_mode);
}
cout << "Hello Ubuntu1804!" << endl;
return 0;
}
运行结果:
参考嵌入式Linux系统编程 — 3.4 access、chmod和 umask函数修改文件访问权限_linux access-CSDN博客