VirtualBox

source: kBuild/trunk/src/lib/nt_fullpath.c@ 3356

Last change on this file since 3356 was 3174, checked in by bird, 7 years ago

kmkbultin: environment fixes and stats.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.6 KB
Line 
1/* $Id: nt_fullpath.c 3174 2018-03-21 21:37:52Z bird $ */
2/** @file
3 * fixcase - fixes the case of paths, windows specific.
4 */
5
6/*
7 * Copyright (c) 2004-2010 knut st. osmundsen <[email protected]>
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
23 *
24 */
25
26/*******************************************************************************
27* Header Files *
28*******************************************************************************/
29#include <Windows.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <ctype.h>
34#include <direct.h>
35
36#include "nt_fullpath.h"
37
38
39/*
40 * Corrects the case of a path.
41 * Expects a fullpath!
42 * Added by bird for the $(abspath ) function and w32ify
43 */
44static void w32_fixcase(char *pszPath)
45{
46#if 0 /* no mp safe */
47 static char s_szLast[260];
48 size_t cchLast;
49#endif
50
51#ifndef NDEBUG
52# define my_assert(expr) \
53 do { \
54 if (!(expr)) { \
55 printf("my_assert: %s, file %s, line %d\npszPath=%s\npsz=%s\n", \
56 #expr, __FILE__, __LINE__, pszPath, psz); \
57 __debugbreak(); \
58 exit(1); \
59 } \
60 } while (0)
61#else
62# define my_assert(expr) do {} while (0)
63#endif
64
65 char *psz = pszPath;
66 if (*psz == '/' || *psz == '\\')
67 {
68 if (psz[1] == '/' || psz[1] == '\\')
69 {
70 /* UNC */
71 my_assert(psz[1] == '/' || psz[1] == '\\');
72 my_assert(psz[2] != '/' && psz[2] != '\\');
73
74 /* skip server name */
75 psz += 2;
76 while (*psz != '\\' && *psz != '/')
77 {
78 if (!*psz)
79 return;
80 *psz++ = toupper(*psz);
81 }
82
83 /* skip the share name */
84 psz++;
85 my_assert(*psz != '/' && *psz != '\\');
86 while (*psz != '\\' && *psz != '/')
87 {
88 if (!*psz)
89 return;
90 *psz++ = toupper(*psz);
91 }
92 my_assert(*psz == '/' || *psz == '\\');
93 psz++;
94 }
95 else
96 {
97 /* Unix spec */
98 psz++;
99 }
100 }
101 else
102 {
103 /* Drive letter */
104 my_assert(psz[1] == ':');
105 *psz = toupper(*psz);
106 my_assert(psz[0] >= 'A' && psz[0] <= 'Z');
107 my_assert(psz[2] == '/' || psz[2] == '\\');
108 psz += 3;
109 }
110
111#if 0 /* not mp safe */
112 /*
113 * Try make use of the result from the previous call.
114 * This is ignorant to slashes and similar, but may help even so.
115 */
116 if ( s_szLast[0] == pszPath[0]
117 && (psz - pszPath == 1 || s_szLast[1] == pszPath[1])
118 && (psz - pszPath <= 2 || s_szLast[2] == pszPath[2])
119 )
120 {
121 char *pszLast = &s_szLast[psz - pszPath];
122 char *pszCur = psz;
123 char *pszSrc0 = pszLast;
124 char *pszDst0 = pszCur;
125 for (;;)
126 {
127 const char ch1 = *pszCur;
128 const char ch2 = *pszLast;
129 if ( ch1 != ch2
130 && (ch1 != '\\' || ch2 != '/')
131 && (ch1 != '/' || ch2 != '\\')
132 && tolower(ch1) != tolower(ch2)
133 && toupper(ch1) != toupper(ch2))
134 break;
135 if (ch1 == '/' || ch1 == '\\')
136 {
137 psz = pszCur + 1;
138 *pszLast = ch1; /* preserve the slashes */
139 }
140 else if (ch1 == '\0')
141 {
142 psz = pszCur;
143 break;
144 }
145 pszCur++;
146 pszLast++;
147 }
148 if (psz != pszDst0)
149 memcpy(pszDst0, pszSrc0, psz - pszDst0);
150 }
151#endif
152
153 /*
154 * Pointing to the first char after the unc or drive specifier,
155 * or in case of a cache hit, the first non-matching char (following a slash of course).
156 */
157 while (*psz)
158 {
159 WIN32_FIND_DATA FindFileData;
160 HANDLE hDir;
161 char chSaved0;
162 char chSaved1;
163 char *pszEnd;
164 int iLongNameDiff;
165 size_t cch;
166
167
168 /* find the end of the component. */
169 pszEnd = psz;
170 while (*pszEnd && *pszEnd != '/' && *pszEnd != '\\')
171 pszEnd++;
172 cch = pszEnd - psz;
173
174 /* replace the end with "?\0" */
175 chSaved0 = pszEnd[0];
176 chSaved1 = pszEnd[1];
177 pszEnd[0] = '?';
178 pszEnd[1] = '\0';
179
180 /* find the right filename. */
181 hDir = FindFirstFile(pszPath, &FindFileData);
182 pszEnd[1] = chSaved1;
183 if (!hDir)
184 {
185#if 0 /* not MP safe */
186 cchLast = psz - pszPath;
187 memcpy(s_szLast, pszPath, cchLast + 1);
188 s_szLast[cchLast + 1] = '\0';
189#endif
190 pszEnd[0] = chSaved0;
191 return;
192 }
193 pszEnd[0] = '\0';
194 while ( (iLongNameDiff = stricmp(FindFileData.cFileName, psz))
195 && stricmp(FindFileData.cAlternateFileName, psz))
196 {
197 if (!FindNextFile(hDir, &FindFileData))
198 {
199#if 0 /* not MP safe */
200 cchLast = psz - pszPath;
201 memcpy(s_szLast, pszPath, cchLast + 1);
202 s_szLast[cchLast + 1] = '\0';
203#endif
204 pszEnd[0] = chSaved0;
205 return;
206 }
207 }
208 pszEnd[0] = chSaved0;
209 if ( iLongNameDiff /* matched the short name */
210 || !FindFileData.cAlternateFileName[0] /* no short name */
211 || !memchr(psz, ' ', cch)) /* no spaces in the matching name */
212 memcpy(psz, !iLongNameDiff ? FindFileData.cFileName : FindFileData.cAlternateFileName, cch);
213 else
214 {
215 /* replace spacy name with the short name. */
216 const size_t cchAlt = strlen(FindFileData.cAlternateFileName);
217 const size_t cchDelta = cch - cchAlt;
218 my_assert(cchAlt > 0);
219 if (!cchDelta)
220 memcpy(psz, FindFileData.cAlternateFileName, cch);
221 else
222 {
223 size_t cbLeft = strlen(pszEnd) + 1;
224 if ((psz - pszPath) + cbLeft + cchAlt <= _MAX_PATH)
225 {
226 memmove(psz + cchAlt, pszEnd, cbLeft);
227 pszEnd -= cchDelta;
228 memcpy(psz, FindFileData.cAlternateFileName, cchAlt);
229 }
230 else
231 fprintf(stderr, "kBuild: case & space fixed filename is growing too long (%d bytes)! '%s'\n",
232 (psz - pszPath) + cbLeft + cchAlt, pszPath);
233 }
234 }
235 my_assert(pszEnd[0] == chSaved0);
236 FindClose(hDir);
237
238 /* advance to the next component */
239 if (!chSaved0)
240 {
241 psz = pszEnd;
242 break;
243 }
244 psz = pszEnd + 1;
245 my_assert(*psz != '/' && *psz != '\\');
246 }
247
248#if 0 /* not MP safe */
249 /* *psz == '\0', the end. */
250 cchLast = psz - pszPath;
251 memcpy(s_szLast, pszPath, cchLast + 1);
252#endif
253#undef my_assert
254}
255
256#define MY_FileNameInformation 9
257typedef struct _MY_FILE_NAME_INFORMATION
258{
259 ULONG FileNameLength;
260 WCHAR FileName[1];
261} MY_FILE_NAME_INFORMATION, *PMY_FILE_NAME_INFORMATION;
262
263#define MY_FileInternalInformation 6
264typedef struct _MY_FILE_INTERNAL_INFORMATION {
265 LARGE_INTEGER IndexNumber;
266} MY_FILE_INTERNAL_INFORMATION, *PMY_FILE_INTERNAL_INFORMATION;
267
268#define MY_FileFsVolumeInformation 1
269typedef struct _MY_FILE_FS_VOLUME_INFORMATION
270{
271 LARGE_INTEGER VolumeCreationTime;
272 ULONG VolumeSerialNumber;
273 ULONG VolumeLabelLength;
274 BOOLEAN SupportsObjects;
275 WCHAR VolumeLabel[/*1*/128];
276} MY_FILE_FS_VOLUME_INFORMATION, *PMY_FILE_FS_VOLUME_INFORMATION;
277
278#define MY_FileFsAttributeInformation 5
279typedef struct _MY_FILE_FS_ATTRIBUTE_INFORMATION
280{
281 ULONG FileSystemAttributes;
282 LONG MaximumComponentNameLength;
283 ULONG FileSystemNameLength;
284 WCHAR FileSystemName[/*1*/64];
285} MY_FILE_FS_ATTRIBUTE_INFORMATION, *PMY_FILE_FS_ATTRIBUTE_INFORMATION;
286
287#define MY_FileFsDeviceInformation 4
288typedef struct MY_FILE_FS_DEVICE_INFORMATION
289{
290 ULONG DeviceType;
291 ULONG Characteristics;
292} MY_FILE_FS_DEVICE_INFORMATION, *PMY_FILE_FS_DEVICE_INFORMATION;
293#define MY_FILE_DEVICE_DISK 7
294#define MY_FILE_DEVICE_DISK_FILE_SYSTEM 8
295#define MY_FILE_DEVICE_FILE_SYSTEM 9
296#define MY_FILE_DEVICE_VIRTUAL_DISK 36
297
298
299typedef struct
300{
301 union
302 {
303 LONG Status;
304 PVOID Pointer;
305 };
306 ULONG_PTR Information;
307} MY_IO_STATUS_BLOCK, *PMY_IO_STATUS_BLOCK;
308
309static BOOL g_fInitialized = FALSE;
310static int g_afNtfsDrives['Z' - 'A' + 1];
311static MY_FILE_FS_VOLUME_INFORMATION g_aVolumeInfo['Z' - 'A' + 1];
312
313static LONG (NTAPI *g_pfnNtQueryInformationFile)(HANDLE FileHandle,
314 PMY_IO_STATUS_BLOCK IoStatusBlock, PVOID FileInformation,
315 ULONG Length, ULONG FileInformationClass);
316static LONG (NTAPI *g_pfnNtQueryVolumeInformationFile)(HANDLE FileHandle,
317 PMY_IO_STATUS_BLOCK IoStatusBlock, PVOID FsInformation,
318 ULONG Length, ULONG FsInformationClass);
319
320
321int
322nt_get_filename_info(const char *pszPath, char *pszFull, size_t cchFull)
323{
324 char abBuf[8192];
325 PMY_FILE_NAME_INFORMATION pFileNameInfo = (PMY_FILE_NAME_INFORMATION)abBuf;
326 PMY_FILE_FS_VOLUME_INFORMATION pFsVolInfo = (PMY_FILE_FS_VOLUME_INFORMATION)abBuf;
327 MY_IO_STATUS_BLOCK Ios;
328 LONG rcNt;
329 HANDLE hFile;
330 int cchOut;
331 char *psz;
332 int iDrv;
333 int rc;
334
335 /*
336 * Check for NtQueryInformationFile the first time around.
337 */
338 if (!g_fInitialized)
339 {
340 g_fInitialized = TRUE;
341 if (!getenv("KMK_DONT_USE_NT_QUERY_INFORMATION_FILE"))
342 {
343 *(FARPROC *)&g_pfnNtQueryInformationFile =
344 GetProcAddress(LoadLibrary("ntdll.dll"), "NtQueryInformationFile");
345 *(FARPROC *)&g_pfnNtQueryVolumeInformationFile =
346 GetProcAddress(LoadLibrary("ntdll.dll"), "NtQueryVolumeInformationFile");
347 }
348 if ( g_pfnNtQueryInformationFile
349 && g_pfnNtQueryVolumeInformationFile)
350 {
351 unsigned i;
352 for (i = 0; i < sizeof(g_afNtfsDrives) / sizeof(g_afNtfsDrives[0]); i++ )
353 g_afNtfsDrives[i] = -1;
354 }
355 else
356 {
357 g_pfnNtQueryVolumeInformationFile = NULL;
358 g_pfnNtQueryInformationFile = NULL;
359 }
360 }
361 if (!g_pfnNtQueryInformationFile)
362 return -1;
363
364 /*
365 * The FileNameInformation we get is relative to where the volume is mounted,
366 * so we have to extract the driveletter prefix ourselves.
367 *
368 * FIXME: This will probably not work for volumes mounted in NTFS sub-directories.
369 */
370 psz = pszFull;
371 if (pszPath[0] == '\\' || pszPath[0] == '/')
372 {
373 /* unc or root of volume */
374 if ( (pszPath[1] == '\\' || pszPath[1] == '/')
375 && (pszPath[2] != '\\' || pszPath[2] == '/'))
376 {
377#if 0 /* don't bother with unc yet. */
378 /* unc - we get the server + name back */
379 *psz++ = '\\';
380#endif
381 return -1;
382 }
383 /* root slash */
384 *psz++ = _getdrive() + 'A' - 1;
385 *psz++ = ':';
386 }
387 else if (pszPath[1] == ':' && isalpha(pszPath[0]))
388 {
389 /* drive letter */
390 *psz++ = toupper(pszPath[0]);
391 *psz++ = ':';
392 }
393 else
394 {
395 /* relative */
396 *psz++ = _getdrive() + 'A' - 1;
397 *psz++ = ':';
398 }
399 iDrv = *pszFull - 'A';
400
401 /*
402 * Fat32 doesn't return filenames with the correct case, so restrict it
403 * to NTFS volumes for now.
404 */
405 if (g_afNtfsDrives[iDrv] == -1)
406 {
407 /* FSCTL_GET_REPARSE_POINT? Enumerate mount points? */
408 g_afNtfsDrives[iDrv] = 0;
409 psz[0] = '\\';
410 psz[1] = '\0';
411#if 1
412 hFile = CreateFile(pszFull,
413 GENERIC_READ,
414 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
415 NULL,
416 OPEN_EXISTING,
417 FILE_FLAG_BACKUP_SEMANTICS,
418 NULL);
419 if (hFile != INVALID_HANDLE_VALUE)
420 {
421 PMY_FILE_FS_ATTRIBUTE_INFORMATION pFsAttrInfo = (PMY_FILE_FS_ATTRIBUTE_INFORMATION)abBuf;
422
423 memset(&Ios, 0, sizeof(Ios));
424 rcNt = g_pfnNtQueryVolumeInformationFile(hFile, &Ios, abBuf, sizeof(abBuf),
425 MY_FileFsAttributeInformation);
426 if ( rcNt >= 0
427 //&& pFsAttrInfo->FileSystemNameLength == 4
428 && pFsAttrInfo->FileSystemName[0] == 'N'
429 && pFsAttrInfo->FileSystemName[1] == 'T'
430 && pFsAttrInfo->FileSystemName[2] == 'F'
431 && pFsAttrInfo->FileSystemName[3] == 'S'
432 && pFsAttrInfo->FileSystemName[4] == '\0')
433 {
434 memset(&Ios, 0, sizeof(Ios));
435 rcNt = g_pfnNtQueryVolumeInformationFile(hFile, &Ios, &g_aVolumeInfo[iDrv],
436 sizeof(MY_FILE_FS_VOLUME_INFORMATION),
437 MY_FileFsVolumeInformation);
438 if (rcNt >= 0)
439 {
440 DWORD dwDriveType = GetDriveType(pszFull);
441 if ( dwDriveType == DRIVE_FIXED
442 || dwDriveType == DRIVE_RAMDISK)
443 g_afNtfsDrives[iDrv] = 1;
444 }
445 }
446 CloseHandle(hFile);
447 }
448#else
449 {
450 char szFSName[32];
451 if ( GetVolumeInformation(pszFull,
452 NULL, 0, /* volume name */
453 NULL, /* serial number */
454 NULL, /* max component */
455 NULL, /* volume attribs */
456 szFSName,
457 sizeof(szFSName))
458 && !strcmp(szFSName, "NTFS"))
459 {
460 g_afNtfsDrives[iDrv] = 1;
461 }
462 }
463#endif
464 }
465 if (!g_afNtfsDrives[iDrv])
466 return -1;
467
468 /*
469 * Try open the path and query its file name information.
470 */
471 hFile = CreateFile(pszPath,
472 GENERIC_READ,
473 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
474 NULL,
475 OPEN_EXISTING,
476 FILE_FLAG_BACKUP_SEMANTICS,
477 NULL);
478 if (hFile != INVALID_HANDLE_VALUE)
479 {
480 /* check that the driver letter is correct first (reparse / symlink issues). */
481 memset(&Ios, 0, sizeof(Ios));
482 rcNt = g_pfnNtQueryVolumeInformationFile(hFile, &Ios, pFsVolInfo, sizeof(*pFsVolInfo), MY_FileFsVolumeInformation);
483 if (rcNt >= 0)
484 {
485 /** @todo do a quick search and try correct the drive letter? */
486 if ( pFsVolInfo->VolumeCreationTime.QuadPart == g_aVolumeInfo[iDrv].VolumeCreationTime.QuadPart
487 && pFsVolInfo->VolumeSerialNumber == g_aVolumeInfo[iDrv].VolumeSerialNumber)
488 {
489 memset(&Ios, 0, sizeof(Ios));
490 rcNt = g_pfnNtQueryInformationFile(hFile, &Ios, abBuf, sizeof(abBuf), MY_FileNameInformation);
491 if (rcNt >= 0)
492 {
493 cchOut = WideCharToMultiByte(CP_ACP, 0,
494 pFileNameInfo->FileName, pFileNameInfo->FileNameLength / sizeof(WCHAR),
495 psz, (int)(cchFull - (psz - pszFull) - 2), NULL, NULL);
496 if (cchOut > 0)
497 {
498 const char *pszEnd;
499#if 0
500 /* upper case the server and share */
501 if (fUnc)
502 {
503 for (psz++; *psz != '/' && *psz != '\\'; psz++)
504 *psz = toupper(*psz);
505 for (psz++; *psz != '/' && *psz != '\\'; psz++)
506 *psz = toupper(*psz);
507 }
508#endif
509 /* add trailing slash on directories if input has it. */
510 pszEnd = strchr(pszPath, '\0');
511 if ( (pszEnd[-1] == '/' || pszEnd[-1] == '\\')
512 && psz[cchOut - 1] != '\\'
513 && psz[cchOut - 1] != '//')
514 psz[cchOut++] = '\\';
515
516 /* make sure it's terminated */
517 psz[cchOut] = '\0';
518 rc = 0;
519 }
520 else
521 rc = -3;
522 }
523 else
524 rc = -4;
525 }
526 else
527 rc = -5;
528 }
529 else
530 rc = -6;
531 CloseHandle(hFile);
532 }
533 else
534 rc = -7;
535 return rc;
536}
537
538/**
539 * Somewhat similar to fullpath, except that it will fix
540 * the case of existing path components.
541 */
542void
543nt_fullpath(const char *pszPath, char *pszFull, size_t cchFull)
544{
545#if 0
546 static int s_cHits = 0;
547 static int s_cFallbacks = 0;
548#endif
549
550 /*
551 * The simple case, the file / dir / whatever exists and can be
552 * queried without problems and spaces.
553 */
554 if (nt_get_filename_info(pszPath, pszFull, cchFull) == 0)
555 {
556 /** @todo make nt_get_filename_info return spaceless path. */
557 if (strchr(pszFull, ' '))
558 w32_fixcase(pszFull);
559#if 0
560 fprintf(stdout, "nt #%d - %s\n", ++s_cHits, pszFull);
561 fprintf(stdout, " #%d - %s\n", s_cHits, pszPath);
562#endif
563 return;
564 }
565 if (g_pfnNtQueryInformationFile)
566 {
567 /* do _fullpath and drop off path elements until we get a hit... - later */
568 }
569
570 /*
571 * For now, simply fall back on the old method.
572 */
573 _fullpath(pszFull, pszPath, cchFull);
574 w32_fixcase(pszFull);
575#if 0
576 fprintf(stderr, "fb #%d - %s\n", ++s_cFallbacks, pszFull);
577 fprintf(stderr, " #%d - %s\n", s_cFallbacks, pszPath);
578#endif
579}
580
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette