bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Steve Lhomme <robux4@ycbcr.xyz>
To: bug-gnulib@gnu.org
Cc: Steve Lhomme <robux4@ycbcr.xyz>
Subject: [PATCH] stat: implement GetFileInformationByHandle with Winstore apps restrictions
Date: Tue, 19 May 2020 12:49:31 +0200	[thread overview]
Message-ID: <20200519104931.1271-1-robux4@ycbcr.xyz> (raw)

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



             reply	other threads:[~2020-05-19 10:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-19 10:49 Steve Lhomme [this message]
2020-05-30  9:41 ` [PATCH] stat: implement GetFileInformationByHandle with Winstore apps restrictions 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200519104931.1271-1-robux4@ycbcr.xyz \
    --to=robux4@ycbcr.xyz \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).