我尝试使用_stat()获取文件的权限。目前我想在Windows下运行它。方法如下:在我的代码中,_stat()如何正确工作
bool CFile::Private::checkPermissions(std::string sFilename, CFile::EOpenmode iOpenmode)
{
std::string sErrMsg = "";
bool bResult = true;
struct _stat buf;
int iResult = 0;
// Get data associated with "crt_stat.c":
iResult = _stat(sFilename.c_str(), &buf);
// Check if statistics are valid:
if(iResult != 0)
{
switch (errno)
{
case ENOENT:
sErrMsg = "File: " + sFilename + " not found.";
break;
case EINVAL:
sErrMsg = "Invalid parameter to _stat(filename, &buf).";
break;
default:
/* Should never be reached. */
sErrMsg = "Unexpected error in _stat(filename, &buf).";
}
throw std::runtime_error(sErrMsg);
}
else
{
if((iOpenmode & CFile::Read) && (!(buf.st_mode & S_IREAD)))
{
bResult = false;
}
if((iOpenmode & CFile::Write) && (!(buf.st_mode & S_IWRITE)))
{
bResult = false;
}
}
return bResult;
}
获得'false'权限的唯一方法是将文件的属性设置为'只读'。取而代之的是,将checkPermissions(...)设置为用户的安全属性(拒绝写入和读取)将为'true'。如何检查Windows的属性和用户权限?
入魔
如果文件已经打开,CreateFile忽略它的SECURITY_ATTRIBUTES arg,在这种情况下GetFileSecurity更好。 – 2016-10-25 00:14:12