Browse code

Fix problems with Filesystem.cpp from pwiz #253

uly55e5 authored on 09/12/2021 07:53:32
Showing1 changed files

... ...
@@ -8,16 +8,16 @@
8 8
 //   Cedars Sinai Medical Center, Los Angeles, California  90048
9 9
 // Copyright 2008 Vanderbilt University - Nashville, TN 37232
10 10
 //
11
-// Licensed under the Apache License, Version 2.0 (the "License"); 
12
-// you may not use this file except in compliance with the License. 
13
-// You may obtain a copy of the License at 
11
+// Licensed under the Apache License, Version 2.0 (the "License");
12
+// you may not use this file except in compliance with the License.
13
+// You may obtain a copy of the License at
14 14
 //
15 15
 // http://www.apache.org/licenses/LICENSE-2.0
16 16
 //
17
-// Unless required by applicable law or agreed to in writing, software 
18
-// distributed under the License is distributed on an "AS IS" BASIS, 
19
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
20
-// See the License for the specific language governing permissions and 
17
+// Unless required by applicable law or agreed to in writing, software
18
+// distributed under the License is distributed on an "AS IS" BASIS,
19
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+// See the License for the specific language governing permissions and
21 21
 // limitations under the License.
22 22
 //
23 23
 
... ...
@@ -94,20 +94,6 @@ extern "C"
94 94
         PULONG ReturnLength
95 95
         );
96 96
 
97
-    struct SYSTEM_HANDLE {
98
-        ULONG ProcessID;
99
-        BYTE HandleType;
100
-        BYTE Flags;
101
-        USHORT Handle;
102
-        PVOID Object;
103
-        ACCESS_MASK GrantedAccess;
104
-    };
105
-
106
-    struct SYSTEM_HANDLE_INFORMATION {
107
-        ULONG HandleCount;
108
-        SYSTEM_HANDLE Handles[1];
109
-    };
110
-
111 97
     enum SECTION_INHERIT
112 98
     {
113 99
         ViewShare = 1,
... ...
@@ -154,6 +140,11 @@ extern "C"
154 140
     PVOID GetLibraryProcAddress(PSTR LibraryName, PSTR ProcName) {
155 141
         return GetProcAddress(GetModuleHandleA(LibraryName), ProcName);
156 142
     }
143
+
144
+    typedef struct __PUBLIC_OBJECT_TYPE_INFORMATION {
145
+      UNICODE_STRING TypeName;
146
+      ULONG Reserved[22];
147
+    } PUBLIC_OBJECT_TYPE_INFORMATION, *PPUBLIC_OBJECT_TYPE_INFORMATION;
157 148
 }
158 149
 
159 150
     int GetFileHandleTypeNumber(SYSTEM_HANDLE_INFORMATION* handleInfos)
... ...
@@ -170,15 +161,15 @@ extern "C"
170 161
         }
171 162
 
172 163
         map<int, int> handlesPerType;
173
-        for (size_t i = 0; i < handleInfos->HandleCount; ++i)
164
+        for (size_t i = 0; i < handleInfos->Count; ++i)
174 165
         {
175
-            if (handleInfos->Handles[i].ProcessID != currentProcessId)
166
+            if (handleInfos->Handle[i].OwnerPid != currentProcessId)
176 167
                 continue;
177 168
 
178
-            if (handleInfos->Handles[i].HandleType < 20) // this is not the File string you're looking for
169
+            if (handleInfos->Handle[i].ObjectType < 20) // this is not the File string you're looking for
179 170
                 continue;
180 171
 
181
-            const auto handle = reinterpret_cast<HANDLE>(handleInfos->Handles[i].Handle);
172
+            const auto handle = reinterpret_cast<HANDLE>(handleInfos->Handle[i].HandleValue);
182 173
             ULONG size;
183 174
             auto queryResult = NtQueryObject(handle, ObjectTypeInformation, typeInfoBytes.data(), typeInfoBytes.size(), &size);
184 175
             if (queryResult == STATUS_INFO_LENGTH_MISMATCH)
... ...
@@ -193,14 +184,15 @@ extern "C"
193 184
                 const auto type = std::wstring(typeInfo->TypeName.Buffer, typeInfo->TypeName.Length / sizeof(WCHAR));
194 185
                 if (type == fileType)
195 186
                     //return handleInfos->Handles[i].HandleType;
196
-                    ++handlesPerType[handleInfos->Handles[i].HandleType];
187
+                    ++handlesPerType[handleInfos->Handle[i].ObjectType];
197 188
             }
198 189
         }
199 190
 
200 191
         if (handlesPerType.empty())
201 192
             return 0;
202 193
 
203
-        auto typeMode = std::max_element(handlesPerType.begin(), handlesPerType.end(), [](const auto& a, const auto& b) { return a.second < b.second; });
194
+        auto typeMode = std::max_element(handlesPerType.begin(),
195
+                                         handlesPerType.end(), [](const std::pair<const int, int> a, const std::pair<const int, int> b) { return a.second < b.second; });
204 196
         return typeMode->first;
205 197
     }
206 198
 
... ...
@@ -368,16 +360,16 @@ PWIZ_API_DECL void force_close_handles_to_filepath(const std::string& filepath,
368 360
     wchar_t szPath[260];
369 361
 
370 362
     // iterate over every handle and close file handles that match the filepath
371
-    for (DWORD i = 0; i < pInfo->HandleCount; i++)
363
+    for (DWORD i = 0; i < pInfo->Count; i++)
372 364
     {
373
-        auto& handleInfo = pInfo->Handles[i];
374
-        if (handleInfo.ProcessID != currentProcessId)
365
+        auto& handleInfo = pInfo->Handle[i];
366
+        if (handleInfo.OwnerPid != currentProcessId)
375 367
             continue;
376 368
 
377
-        if (handleInfo.HandleType == fileHandleType)
369
+        if (handleInfo.ObjectType == fileHandleType)
378 370
         {
379 371
             szPath[0] = '\0';
380
-            if (!GetFileNameFromHandle((HANDLE)handleInfo.Handle, szPath, 260))
372
+            if (!GetFileNameFromHandle((HANDLE)handleInfo.HandleValue, szPath, 260))
381 373
             {
382 374
                 /*char messageBuffer[256];
383 375
                 memset(messageBuffer, 0, 256);
... ...
@@ -390,7 +382,7 @@ PWIZ_API_DECL void force_close_handles_to_filepath(const std::string& filepath,
390 382
             wchar_t* handlePath = szPath;
391 383
             if (bal::iends_with(handlePath, wideFilepathWithoutRoot.c_str()))
392 384
             {
393
-                if (!CloseHandle((HANDLE)handleInfo.Handle))
385
+                if (!CloseHandle((HANDLE)handleInfo.HandleValue))
394 386
                     fprintf(stderr, "[force_close_handles_to_filepath()] Error closing file handle.\n");
395 387
                 //else
396 388
                 //    fprintf(stderr, "[force_close_handles_to_filepath()] Closed file handle: " + gcnew String(handlePath));
... ...
@@ -402,7 +394,7 @@ PWIZ_API_DECL void force_close_handles_to_filepath(const std::string& filepath,
402 394
             SIZE_T viewSize = 1;
403 395
             PVOID viewBase = NULL;
404 396
 
405
-            NTSTATUS status = NtMapViewOfSection((HANDLE)handleInfo.Handle, currentProcessHandle, &viewBase, 0, 0, NULL, &viewSize, ViewShare, 0, PAGE_READONLY);
397
+            NTSTATUS status = NtMapViewOfSection((HANDLE)handleInfo.HandleValue, currentProcessHandle, &viewBase, 0, 0, NULL, &viewSize, ViewShare, 0, PAGE_READONLY);
406 398
 
407 399
             if (!NT_SUCCESS(status))
408 400
                 continue;
... ...
@@ -418,7 +410,7 @@ PWIZ_API_DECL void force_close_handles_to_filepath(const std::string& filepath,
418 410
             if (!bal::iends_with(wstring(mappedFilename.data()), wideFilename))
419 411
                 continue;
420 412
 
421
-            if (!CloseHandle((HANDLE)handleInfo.Handle))
413
+            if (!CloseHandle((HANDLE)handleInfo.HandleValue))
422 414
                 fprintf(stderr, "[force_close_handles_to_filepath()] Error closing section handle.\n");
423 415
             //else
424 416
             //    fprintf(stderr, "[force_close_handles_to_filepath()] Closed section handle.\n");