在 Windows/mingw 上,`fcntl(fd, F_GETFL) 的等价物是什么?O_ACCMODE`?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4636875/
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 15:56:27  来源:igfitidea点击:

On Windows/mingw, what is the equivalent of `fcntl(fd, F_GETFL) | O_ACCMODE`?

cwindowsmingwfile-descriptor

提问by Adrian

I am compiling a program on Windows with Mingw. How can I get the access mode for an open file descriptor?

我正在用 Mingw 在 Windows 上编译一个程序。如何获取打开的文件描述符的访问模式?

采纳答案by sqykly

According to Win32.hlp, the API supplies the function BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation)in KERNEL32. LPBY_HANDLE_FILE_INFORMATIONis a BY_HANDLE_FILE_INFORMATION*, where BY_HANDLE_FILE_INFORMATIONis as follows:

根据 Win32.hlp,API 提供BOOL GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation)KERNEL32 中的函数。 LPBY_HANDLE_FILE_INFORMATION是 a BY_HANDLE_FILE_INFORMATION*,其中BY_HANDLE_FILE_INFORMATION如下:

typedef struct _BY_HANDLE_FILE_INFORMATION { // bhfi  
    DWORD    dwFileAttributes; 
    FILETIME ftCreationTime; 
    FILETIME ftLastAccessTime; 
    FILETIME ftLastWriteTime; 
    DWORD    dwVolumeSerialNumber; 
    DWORD    nFileSizeHigh; 
    DWORD    nFileSizeLow; 
    DWORD    nNumberOfLinks; 
    DWORD    nFileIndexHigh; 
    DWORD    nFileIndexLow; 
} BY_HANDLE_FILE_INFORMATION;

After calling said function, if it returns true, the BY_HANDLE_FILE_INFORMATIONcontains data pertinent to your file. dwFileAttributesmay contain the FILE_ATTRIBUTE_READ_ONLYflag.

调用上述函数后,如果返回 true,则BY_HANDLE_FILE_INFORMATION包含与您的文件相关的数据。 dwFileAttributes可能包含FILE_ATTRIBUTE_READ_ONLY标志。

If you want more than that, there is also:

如果您想要更多,还有:

BOOL GetKernelObjectSecurity(
 HANDLE Handle,                             // handle of object to query
 SECURITY_INFORMATION RequestedInformation, // requested information
 PSECURITY_DESCRIPTOR pSecurityDescriptor,  // address of security descriptor
 DWORD nLength,                             // size of buffer for security descriptor 
 LPDWORD lpnLengthNeeded                    // address of required size of buffer
);

The API reference is necessarily vague on what a SECURITY_DESCRIPTORis, but you can call a bunch of other functions using its address as a parameter to get specific properties. The SECURITY_INFORMATIONis just a DWORDconstant specifying which of these functions you plan to call. You can find more info at http://msdn.microsoft.com/en-us/library/aa446641%28VS.85%29.aspx

API 参考对于 aSECURITY_DESCRIPTOR是什么肯定含糊不清,但是您可以使用其地址作为参数调用一堆其他函数来获取特定属性。这SECURITY_INFORMATION只是一个DWORD常量,指定您计划调用这些函数中的哪一个。您可以在http://msdn.microsoft.com/en-us/library/aa446641%28VS.85%29.aspx 上找到更多信息

Edit - the second code section keeps coming out looking screwy, but the link to the API reference will lead you where you need to go if you dig around a bit.

编辑 - 第二个代码部分看起来很奇怪,但是如果您稍微深入研究,指向 API 参考的链接将引导您到达需要去的地方。

回答by Pibben

As far as I can tell, you cant.

据我所知,你不能。

https://web.archive.org/web/20161107234935/http://www.zemris.fer.hr/predmeti/os1/misc/Unix2Win.htmis a good guide for unix-to-windows porting.

https://web.archive.org/web/20161107234935/http://www.zemris.fer.hr/predmeti/os1/misc/Unix2Win.htm是 unix-to-windows 移植的好指南。

Maybe you could use the Cygwin POSIX "emulation"?

也许您可以使用 Cygwin POSIX“仿真”?