[1165] | 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 | /*
|
---|
[2413] | 7 | * Copyright (c) 2004-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
---|
[1165] | 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
|
---|
[2019] | 13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
[1165] | 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
|
---|
[2019] | 22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
[1165] | 23 | *
|
---|
| 24 | */
|
---|
| 25 |
|
---|
[2019] | 26 | /*******************************************************************************
|
---|
| 27 | * Header Files *
|
---|
| 28 | *******************************************************************************/
|
---|
[1165] | 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 |
|
---|
[2849] | 36 | #include "nt_fullpath.h"
|
---|
[2848] | 37 |
|
---|
| 38 |
|
---|
[1165] | 39 | /*
|
---|
| 40 | * Corrects the case of a path.
|
---|
| 41 | * Expects a fullpath!
|
---|
| 42 | * Added by bird for the $(abspath ) function and w32ify
|
---|
| 43 | */
|
---|
| 44 | static void w32_fixcase(char *pszPath)
|
---|
| 45 | {
|
---|
[3174] | 46 | #if 0 /* no mp safe */
|
---|
[1165] | 47 | static char s_szLast[260];
|
---|
| 48 | size_t cchLast;
|
---|
[3174] | 49 | #endif
|
---|
[1165] | 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 |
|
---|
[3174] | 111 | #if 0 /* not mp safe */
|
---|
[1165] | 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;
|
---|
[1368] | 123 | char *pszSrc0 = pszLast;
|
---|
| 124 | char *pszDst0 = pszCur;
|
---|
[1165] | 125 | for (;;)
|
---|
| 126 | {
|
---|
| 127 | const char ch1 = *pszCur;
|
---|
| 128 | const char ch2 = *pszLast;
|
---|
| 129 | if ( ch1 != ch2
|
---|
| 130 | && (ch1 != '\\' || ch2 != '/')
|
---|
[1368] | 131 | && (ch1 != '/' || ch2 != '\\')
|
---|
| 132 | && tolower(ch1) != tolower(ch2)
|
---|
| 133 | && toupper(ch1) != toupper(ch2))
|
---|
| 134 | break;
|
---|
| 135 | if (ch1 == '/' || ch1 == '\\')
|
---|
[1165] | 136 | {
|
---|
[1368] | 137 | psz = pszCur + 1;
|
---|
| 138 | *pszLast = ch1; /* preserve the slashes */
|
---|
[1165] | 139 | }
|
---|
| 140 | else if (ch1 == '\0')
|
---|
| 141 | {
|
---|
| 142 | psz = pszCur;
|
---|
| 143 | break;
|
---|
| 144 | }
|
---|
| 145 | pszCur++;
|
---|
| 146 | pszLast++;
|
---|
| 147 | }
|
---|
[1368] | 148 | if (psz != pszDst0)
|
---|
| 149 | memcpy(pszDst0, pszSrc0, psz - pszDst0);
|
---|
[1165] | 150 | }
|
---|
[3174] | 151 | #endif
|
---|
[1165] | 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;
|
---|
[1248] | 165 | size_t cch;
|
---|
[1165] | 166 |
|
---|
| 167 |
|
---|
| 168 | /* find the end of the component. */
|
---|
| 169 | pszEnd = psz;
|
---|
| 170 | while (*pszEnd && *pszEnd != '/' && *pszEnd != '\\')
|
---|
| 171 | pszEnd++;
|
---|
[1248] | 172 | cch = pszEnd - psz;
|
---|
[1165] | 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 | {
|
---|
[3174] | 185 | #if 0 /* not MP safe */
|
---|
[1165] | 186 | cchLast = psz - pszPath;
|
---|
| 187 | memcpy(s_szLast, pszPath, cchLast + 1);
|
---|
[1248] | 188 | s_szLast[cchLast + 1] = '\0';
|
---|
[3174] | 189 | #endif
|
---|
[1165] | 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 | {
|
---|
[3174] | 199 | #if 0 /* not MP safe */
|
---|
[1165] | 200 | cchLast = psz - pszPath;
|
---|
| 201 | memcpy(s_szLast, pszPath, cchLast + 1);
|
---|
[1248] | 202 | s_szLast[cchLast + 1] = '\0';
|
---|
[3174] | 203 | #endif
|
---|
[1165] | 204 | pszEnd[0] = chSaved0;
|
---|
| 205 | return;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | pszEnd[0] = chSaved0;
|
---|
[1248] | 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);
|
---|
[1165] | 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 |
|
---|
[3174] | 248 | #if 0 /* not MP safe */
|
---|
[1165] | 249 | /* *psz == '\0', the end. */
|
---|
| 250 | cchLast = psz - pszPath;
|
---|
| 251 | memcpy(s_szLast, pszPath, cchLast + 1);
|
---|
[3174] | 252 | #endif
|
---|
[1165] | 253 | #undef my_assert
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | #define MY_FileNameInformation 9
|
---|
| 257 | typedef struct _MY_FILE_NAME_INFORMATION
|
---|
| 258 | {
|
---|
| 259 | ULONG FileNameLength;
|
---|
| 260 | WCHAR FileName[1];
|
---|
| 261 | } MY_FILE_NAME_INFORMATION, *PMY_FILE_NAME_INFORMATION;
|
---|
| 262 |
|
---|
[1265] | 263 | #define MY_FileInternalInformation 6
|
---|
| 264 | typedef struct _MY_FILE_INTERNAL_INFORMATION {
|
---|
| 265 | LARGE_INTEGER IndexNumber;
|
---|
| 266 | } MY_FILE_INTERNAL_INFORMATION, *PMY_FILE_INTERNAL_INFORMATION;
|
---|
| 267 |
|
---|
| 268 | #define MY_FileFsVolumeInformation 1
|
---|
| 269 | typedef 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
|
---|
| 279 | typedef 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 |
|
---|
[1643] | 287 | #define MY_FileFsDeviceInformation 4
|
---|
| 288 | typedef 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 |
|
---|
[2455] | 299 | typedef struct
|
---|
[1165] | 300 | {
|
---|
| 301 | union
|
---|
| 302 | {
|
---|
[2455] | 303 | LONG Status;
|
---|
| 304 | PVOID Pointer;
|
---|
[1165] | 305 | };
|
---|
[2455] | 306 | ULONG_PTR Information;
|
---|
[1165] | 307 | } MY_IO_STATUS_BLOCK, *PMY_IO_STATUS_BLOCK;
|
---|
| 308 |
|
---|
[1265] | 309 | static BOOL g_fInitialized = FALSE;
|
---|
| 310 | static int g_afNtfsDrives['Z' - 'A' + 1];
|
---|
| 311 | static MY_FILE_FS_VOLUME_INFORMATION g_aVolumeInfo['Z' - 'A' + 1];
|
---|
| 312 |
|
---|
[1165] | 313 | static LONG (NTAPI *g_pfnNtQueryInformationFile)(HANDLE FileHandle,
|
---|
| 314 | PMY_IO_STATUS_BLOCK IoStatusBlock, PVOID FileInformation,
|
---|
| 315 | ULONG Length, ULONG FileInformationClass);
|
---|
[1265] | 316 | static LONG (NTAPI *g_pfnNtQueryVolumeInformationFile)(HANDLE FileHandle,
|
---|
| 317 | PMY_IO_STATUS_BLOCK IoStatusBlock, PVOID FsInformation,
|
---|
| 318 | ULONG Length, ULONG FsInformationClass);
|
---|
[1165] | 319 |
|
---|
| 320 |
|
---|
| 321 | int
|
---|
| 322 | nt_get_filename_info(const char *pszPath, char *pszFull, size_t cchFull)
|
---|
| 323 | {
|
---|
[3174] | 324 | char abBuf[8192];
|
---|
[1265] | 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;
|
---|
[3174] | 329 | HANDLE hFile;
|
---|
[1265] | 330 | int cchOut;
|
---|
| 331 | char *psz;
|
---|
| 332 | int iDrv;
|
---|
| 333 | int rc;
|
---|
[1165] | 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"))
|
---|
[1265] | 342 | {
|
---|
[1165] | 343 | *(FARPROC *)&g_pfnNtQueryInformationFile =
|
---|
| 344 | GetProcAddress(LoadLibrary("ntdll.dll"), "NtQueryInformationFile");
|
---|
[1265] | 345 | *(FARPROC *)&g_pfnNtQueryVolumeInformationFile =
|
---|
| 346 | GetProcAddress(LoadLibrary("ntdll.dll"), "NtQueryVolumeInformationFile");
|
---|
| 347 | }
|
---|
| 348 | if ( g_pfnNtQueryInformationFile
|
---|
| 349 | && g_pfnNtQueryVolumeInformationFile)
|
---|
[1175] | 350 | {
|
---|
| 351 | unsigned i;
|
---|
| 352 | for (i = 0; i < sizeof(g_afNtfsDrives) / sizeof(g_afNtfsDrives[0]); i++ )
|
---|
| 353 | g_afNtfsDrives[i] = -1;
|
---|
| 354 | }
|
---|
[1265] | 355 | else
|
---|
| 356 | {
|
---|
| 357 | g_pfnNtQueryVolumeInformationFile = NULL;
|
---|
| 358 | g_pfnNtQueryInformationFile = NULL;
|
---|
| 359 | }
|
---|
[1165] | 360 | }
|
---|
| 361 | if (!g_pfnNtQueryInformationFile)
|
---|
| 362 | return -1;
|
---|
| 363 |
|
---|
| 364 | /*
|
---|
[1175] | 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 | }
|
---|
[1265] | 399 | iDrv = *pszFull - 'A';
|
---|
[1175] | 400 |
|
---|
| 401 | /*
|
---|
| 402 | * Fat32 doesn't return filenames with the correct case, so restrict it
|
---|
| 403 | * to NTFS volumes for now.
|
---|
| 404 | */
|
---|
[1265] | 405 | if (g_afNtfsDrives[iDrv] == -1)
|
---|
[1175] | 406 | {
|
---|
[1265] | 407 | /* FSCTL_GET_REPARSE_POINT? Enumerate mount points? */
|
---|
| 408 | g_afNtfsDrives[iDrv] = 0;
|
---|
[1175] | 409 | psz[0] = '\\';
|
---|
| 410 | psz[1] = '\0';
|
---|
[1265] | 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 | {
|
---|
[1643] | 440 | DWORD dwDriveType = GetDriveType(pszFull);
|
---|
| 441 | if ( dwDriveType == DRIVE_FIXED
|
---|
| 442 | || dwDriveType == DRIVE_RAMDISK)
|
---|
| 443 | g_afNtfsDrives[iDrv] = 1;
|
---|
[1265] | 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
|
---|
[1175] | 464 | }
|
---|
[1265] | 465 | if (!g_afNtfsDrives[iDrv])
|
---|
[1175] | 466 | return -1;
|
---|
| 467 |
|
---|
| 468 | /*
|
---|
[1165] | 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);
|
---|
[1265] | 478 | if (hFile != INVALID_HANDLE_VALUE)
|
---|
[1165] | 479 | {
|
---|
[1265] | 480 | /* check that the driver letter is correct first (reparse / symlink issues). */
|
---|
[1165] | 481 | memset(&Ios, 0, sizeof(Ios));
|
---|
[1265] | 482 | rcNt = g_pfnNtQueryVolumeInformationFile(hFile, &Ios, pFsVolInfo, sizeof(*pFsVolInfo), MY_FileFsVolumeInformation);
|
---|
[1165] | 483 | if (rcNt >= 0)
|
---|
| 484 | {
|
---|
[1265] | 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)
|
---|
[1165] | 488 | {
|
---|
[1265] | 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;
|
---|
[1175] | 499 | #if 0
|
---|
[1265] | 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 | }
|
---|
[1175] | 508 | #endif
|
---|
[1265] | 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++] = '\\';
|
---|
[1165] | 515 |
|
---|
[1265] | 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;
|
---|
[1165] | 525 | }
|
---|
[1265] | 526 | else
|
---|
| 527 | rc = -5;
|
---|
[1165] | 528 | }
|
---|
[1265] | 529 | else
|
---|
| 530 | rc = -6;
|
---|
| 531 | CloseHandle(hFile);
|
---|
[1165] | 532 | }
|
---|
[1265] | 533 | else
|
---|
| 534 | rc = -7;
|
---|
| 535 | return rc;
|
---|
[1165] | 536 | }
|
---|
| 537 |
|
---|
| 538 | /**
|
---|
| 539 | * Somewhat similar to fullpath, except that it will fix
|
---|
| 540 | * the case of existing path components.
|
---|
| 541 | */
|
---|
| 542 | void
|
---|
| 543 | nt_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
|
---|
[1248] | 552 | * queried without problems and spaces.
|
---|
[1165] | 553 | */
|
---|
| 554 | if (nt_get_filename_info(pszPath, pszFull, cchFull) == 0)
|
---|
| 555 | {
|
---|
[1248] | 556 | /** @todo make nt_get_filename_info return spaceless path. */
|
---|
[1250] | 557 | if (strchr(pszFull, ' '))
|
---|
| 558 | w32_fixcase(pszFull);
|
---|
[1165] | 559 | #if 0
|
---|
[1250] | 560 | fprintf(stdout, "nt #%d - %s\n", ++s_cHits, pszFull);
|
---|
| 561 | fprintf(stdout, " #%d - %s\n", s_cHits, pszPath);
|
---|
[1165] | 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);
|
---|
[1250] | 577 | fprintf(stderr, " #%d - %s\n", s_cFallbacks, pszPath);
|
---|
[1165] | 578 | #endif
|
---|
| 579 | }
|
---|
| 580 |
|
---|