我想在目录中读取一些来自第三方工具的文件。在读取这些文件之前,我会检查它们是否是常规文件
。
阅读之前
if (!S_ISREG(file_info.st_mode))
{
return false;
}
读完这个文件后,我想更改它的权限,这样它就不会被一次又一次地读取。
我的问题是如何更改文件权限 st_mode
以便我的应用程序在不是 super 用户的情况下不会再次读取它。
我的 Linux 发行版是 RedHat
最佳答案
如果你读了一个文件,不想再读一遍,你不应该测试它是否是一个普通文件,因为不管它有什么权限,一个普通文件就是一个普通文件。所以我认为你应该检查读取权限,如果设置了,读取文件,并使用 chmod()
设置权限以禁止读取。
/* Check it it is readable by the user */
if ((file_info.st_mode & S_IRUSR)==0)
{
return false;
}
/* If it is, open and read the file... */
...
...
/* Mark it as not readable by the user. file_name is assumed
to be the name of the file as you have used it upon calling
lstat */
chmod (file_name, file_info.st_mode & ~S_IRUSR);
关于c++ - 无需 super 用户即可更改文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20123072/