windows Windows下的_stat()究竟是如何工作的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6094665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:54:38  来源:igfitidea点击:

How does _stat() under Windows exactly work

c++windowsstat

提问by oRUMOo

In my code I try to get the permissions for a file with _stat(). Currently I want to run it under Windows. The method is as follows:

在我的代码中,我尝试使用 _stat() 获取文件的权限。目前我想在 Windows 下运行它。方法如下:

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;
}

The only way to get 'false' for permission is to set the file's attribute 'read only'. Instead of this, set the security properties of the user (reject writing and reading) will get 'true' for checkPermissions(...). How to check both, the attributes and the user permissions for Windows?

获得“false”权限的唯一方法是将文件的属性设置为“只读”。取而代之的是,设置用户的安全属性(拒绝写入和读取)将为 checkPermissions(...) 设置为“true”。如何检查 Windows 的属性和用户权限?

Rumo

留沫

回答by MSalters

_statis a function that is not native to Windows. It's a helper function to ease the porting of UNIX programs to Windows. But the UNIX file model just doesn't apply to Windows, so not all fields make sense. For instance, Windows has real ACL's, not rwxbits. There's just no way to fit all the ACL information in st_mode.

_stat是非 Windows 原生的功能。它是一个帮助函数,可以简化 UNIX 程序到 Windows 的移植。但是 UNIX 文件模型不适用于 Windows,因此并非所有字段都有意义。例如,Windows 具有真正的 ACL,而不是rwx位。没有办法将所有 ACL 信息放入st_mode.

If you want to test ACL permissions, the proper way is to just try: call CreateFile()and check GetLastError(). Trying to get file permissions up front is not reliable as they can change at any time.

如果您想测试 ACL 权限,正确的方法是尝试:调用CreateFile()并检查GetLastError()。尝试预先获取文件权限并不可靠,因为它们可以随时更改。

回答by Dr.McNinja

If we're talking about the same _stat() it's pretty clear from this MSDN articleexactly what it does. Basically, you supply it a path to a file in question and a pointer to a _stat struct and it will dump the permissions to the struct if it returns 0.

如果我们谈论的是相同的 _stat(),那么从这篇 MSDN 文章中可以清楚地看出它的作用。基本上,您向它提供一个指向相关文件的路径和一个指向 _stat 结构的指针,如果它返回 0,它将转储该结构的权限。

The example C++ code in the article is pretty good.

文章中的示例C++代码相当不错。

As for testing user permissions, IsUserAnAdmin()does the job pretty well. You may be able to use this MSDN articlefor a different approach.

至于测试用户权限,IsUserAnAdmin()做得很好。您可以将这篇 MSDN 文章用于不同的方法。

I hope this helps!

我希望这有帮助!