bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] stat: implement GetFileInformationByHandle with Winstore apps restrictions
@ 2020-05-19 10:49 Steve Lhomme
  2020-05-30  9:41 ` Bruno Haible
  0 siblings, 1 reply; 7+ messages in thread
From: Steve Lhomme @ 2020-05-19 10:49 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Steve Lhomme

GetFileInformationByHandle() cannot be called. But the same information can be
gathered via calls to GetFileInformationByHandleEx() which is allowed.

A function pointer is used to pick between the local version using
GetFileInformationByHandleEx() and the system one.

If WINSTORECOMPAT is defined that means the app is built with mingw including
the compatibility library which also includes an GetFileInformationByHandle()
implementation, so no need to redefine it.
---
 lib/stat-w32.c | 44 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index 6900dfcf5..6699b9560 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -58,6 +58,48 @@ typedef DWORD (WINAPI * GetFinalPathNameByHandleFuncType) (HANDLE hFile,
 static GetFinalPathNameByHandleFuncType GetFinalPathNameByHandleFunc = NULL;
 static BOOL initialized = FALSE;
 
+#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && !defined(WINSTORECOMPAT)
+WINBOOL _GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpInfo)
+{
+    FILE_ID_INFO id_info;
+    FILE_STANDARD_INFO standard_info;
+    FILE_BASIC_INFO basic_info;
+    if (!GetFileInformationByHandleEx(hFile, FileIdInfo, &id_info, sizeof (id_info)) ||
+        !GetFileInformationByHandleEx(hFile, FileStandardInfo, &standard_info, sizeof (standard_info)) ||
+        !GetFileInformationByHandleEx(hFile, FileBasicInfo, &basic_info, sizeof (basic_info)))
+    {
+        return FALSE;
+    }
+    
+    lpInfo->dwFileAttributes = basic_info.FileAttributes;
+
+    lpInfo->ftCreationTime.dwHighDateTime   = basic_info.CreationTime.HighPart;
+    lpInfo->ftCreationTime.dwLowDateTime    = basic_info.CreationTime.LowPart;
+    lpInfo->ftLastAccessTime.dwHighDateTime = basic_info.LastAccessTime.HighPart;
+    lpInfo->ftLastAccessTime.dwLowDateTime  = basic_info.LastAccessTime.LowPart;
+    lpInfo->ftLastWriteTime.dwHighDateTime  = basic_info.LastWriteTime.HighPart;
+    lpInfo->ftLastWriteTime.dwLowDateTime   = basic_info.LastWriteTime.LowPart;
+
+    lpInfo->dwVolumeSerialNumber = id_info.VolumeSerialNumber;
+
+    lpInfo->nFileSizeHigh = standard_info.EndOfFile.HighPart;
+    lpInfo->nFileSizeLow  = standard_info.EndOfFile.LowPart;
+
+    lpInfo->nNumberOfLinks = standard_info.NumberOfLinks;
+
+    /* The nFileIndex from GetFileInformationByHandle is equal to the low
+             64 bits of the 128-bit FileId from GetFileInformationByHandleEx,
+             and the high 64 bits of this 128-bit FileId are zero. */
+    lpInfo->nFileIndexLow  = (id_info.FileId.Identifier[12] << 24) | (id_info.FileId.Identifier[13] << 16) | (id_info.FileId.Identifier[14] << 8) | id_info.FileId.Identifier[15];
+    lpInfo->nFileIndexHigh = (id_info.FileId.Identifier[ 8] << 24) | (id_info.FileId.Identifier[ 9] << 16) | (id_info.FileId.Identifier[10] << 8) | id_info.FileId.Identifier[11];
+
+    return TRUE;
+}
+static WINBOOL (*GetFileInformationByHandleFunc)(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpInfo) = _GetFileInformationByHandle;
+#else /* WINAPI_PARTITION_DESKTOP */
+static WINBOOL (WINAPI *GetFileInformationByHandleFunc)(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpInfo) = GetFileInformationByHandle;
+#endif /* WINAPI_PARTITION_DESKTOP */
+
 static void
 initialize (void)
 {
@@ -159,7 +201,7 @@ _gl_fstat_by_handle (HANDLE h, const char *path, struct stat *buf)
          <https://docs.microsoft.com/en-us/windows/desktop/api/winbase/ns-winbase-_file_basic_info>
          The latter requires -D_WIN32_WINNT=_WIN32_WINNT_VISTA or higher.  */
       BY_HANDLE_FILE_INFORMATION info;
-      if (! GetFileInformationByHandle (h, &info))
+      if (! GetFileInformationByHandleFunc (h, &info))
         goto failed;
 
       /* Test for error conditions before starting to fill *buf.  */
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-05-30 16:38 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-19 10:49 [PATCH] stat: implement GetFileInformationByHandle with Winstore apps restrictions Steve Lhomme
2020-05-30  9:41 ` Bruno Haible
2020-05-30 13:57   ` Steve Lhomme
2020-05-30 14:35     ` Bruno Haible
2020-05-30 14:51       ` Steve Lhomme
2020-05-30 15:47         ` Bruno Haible
2020-05-30 16:37           ` Steve Lhomme

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).