summaryrefslogtreecommitdiffstats
path: root/src/lib/nt/nthlpfs.c
blob: a85c51779ea9cb570ec29a83d47cb9ef72cbf14c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* $Id: nthlpfs.c 3223 2018-03-31 02:29:56Z bird $ */
/** @file
 * MSC + NT helpers for file system related functions.
 */

/*
 * Copyright (c) 2005-2013 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Alternatively, the content of this file may be used under the terms of the
 * GPL version 2 or later, or LGPL version 2.1 or later.
 */


/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#include "nthlp.h"
#include <stddef.h>
#include <string.h>
#include <wchar.h>
#include <errno.h>


/*******************************************************************************
*   Global Variables                                                           *
*******************************************************************************/
static int g_fHaveOpenReparsePoint = -1;



static int birdHasTrailingSlash(const char *pszPath)
{
    char ch, ch2;

    /* Skip leading slashes. */
    while ((ch = *pszPath) == '/' || ch == '\\')
        pszPath++;
    if (ch == '\0')
        return 0;

    /* Find the last char. */
    while ((ch2 = *++pszPath) != '\0')
        ch = ch2;

    return ch == '/' || ch == '\\' || ch == ':';
}


static int birdHasTrailingSlashW(const wchar_t *pwszPath)
{
    wchar_t wc, wc2;

    /* Skip leading slashes. */
    while ((wc = *pwszPath) == '/' || wc == '\\')
        pwszPath++;
    if (wc == '\0')
        return 0;

    /* Find the last char. */
    while ((wc2 = *++pwszPath) != '\0')
        wc = wc2;

    return wc == '/' || wc == '\\' || wc == ':';
}


int birdIsPathDirSpec(const char *pszPath)
{
    char ch, ch2;

    /* Check for empty string. */
    ch = *pszPath;
    if (ch == '\0')
        return 0;

    /* Find the last char. */
    while ((ch2 = *++pszPath) != '\0')
        ch = ch2;

    return ch == '/' || ch == '\\' || ch == ':';
}


static int birdIsPathDirSpecW(const wchar_t *pwszPath)
{
    wchar_t wc, wc2;

    /* Check for empty string. */
    wc = *pwszPath;
    if (wc == '\0')
        return 0;

    /* Find the last char. */
    while ((wc2 = *++pwszPath) != '\0')
        wc = wc2;

    return wc == '/' || wc == '\\' || wc == ':';
}


int birdDosToNtPath(const char *pszPath, MY_UNICODE_STRING *pNtPath)
{
    MY_NTSTATUS         rcNt;
    WCHAR               wszTmp[4096];
    MY_UNICODE_STRING   TmpUniStr;
    MY_ANSI_STRING      Src;

    birdResolveImports();

    pNtPath->Length = pNtPath->MaximumLength = 0;
    pNtPath->Buffer = NULL;

    /*
     * Convert the input to wide char.
     */
    Src.Buffer              = (PCHAR)pszPath;
    Src.MaximumLength       = Src.Length = (USHORT)strlen(pszPath);

    TmpUniStr.Length        = 0;
    TmpUniStr.MaximumLength = sizeof(wszTmp) - sizeof(WCHAR);
    TmpUniStr.Buffer        = wszTmp;

    rcNt = g_pfnRtlAnsiStringToUnicodeString(&TmpUniStr, &Src, FALSE);
    if (MY_NT_SUCCESS(rcNt))
    {
        if (TmpUniStr.Length > 0 && !(TmpUniStr.Length & 1))
        {
            wszTmp[TmpUniStr.Length / sizeof(WCHAR)] = '\0';

            /*
             * Convert the wide DOS path to an NT path.
             */
            if (g_pfnRtlDosPathNameToNtPathName_U(wszTmp, pNtPath, NULL, FALSE))
                return 0;
        }
        rcNt = -1;
    }
    return birdSetErrnoFromNt(rcNt);
}


int birdDosToNtPathW(const wchar_t *pwszPath, MY_UNICODE_STRING *pNtPath)
{
    birdResolveImports();

    pNtPath->Length = pNtPath->MaximumLength = 0;
    pNtPath->Buffer = NULL;

    /*
     * Convert the wide DOS path to an NT path.
     */
    if (g_pfnRtlDosPathNameToNtPathName_U(pwszPath, pNtPath, NULL, FALSE))
        return 0;
    return birdSetErrnoFromNt(STATUS_NO_MEMORY);
}


/**
 * Converts UNIX slashes to DOS ones.
 *
 * @returns 0
 * @param   pNtPath     The relative NT path to fix up.
 */
static int birdFixRelativeNtPathSlashesAndReturn0(MY_UNICODE_STRING *pNtPath)
{
    size_t   cwcLeft  = pNtPath->Length / sizeof(wchar_t);
    wchar_t *pwcStart = pNtPath->Buffer;
    wchar_t *pwcHit;

    /* Convert slashes. */
    while ((pwcHit = wmemchr(pwcStart,  '/', cwcLeft)) != NULL)
    {
        *pwcHit = '\\';
        cwcLeft -= pwcHit - pwcStart;
        pwcHit = pwcStart;
    }

#if 0
    /* Strip trailing slashes (NT doesn't like them). */
    while (   pNtPath->Length >= sizeof(wchar_t)
           && pNtPath->Buffer[(pNtPath->Length - sizeof(wchar_t)) / sizeof(wchar_t)] == '\\')
    {
        pNtPath->Length -= sizeof(wchar_t);
        pNtPath->Buffer[pNtPath->Length / sizeof(wchar_t)] = '\0';
    }

    /* If it was all trailing slashes we convert it to a dot path. */
    if (   pNtPath->Length == 0
        && pNtPath->MaximumLength >= sizeof(wchar_t) * 2)
    {
        pNtPath->Length = sizeof(wchar_t);
        pNtPath->Buffer[0] = '.';
        pNtPath->Buffer[1] = '\0';
    }
#endif

    return 0;
}


/**
 * Similar to birdDosToNtPath, but it does call RtlDosPathNameToNtPathName_U.
 *
 * @returns 0 on success, -1 + errno on failure.
 * @param   pszPath     The relative path.
 * @param   pNtPath     Where to return the NT path.  Call birdFreeNtPath when done.
 */
int birdDosToRelativeNtPath(const char *pszPath, MY_UNICODE_STRING *pNtPath)
{
    MY_NTSTATUS         rcNt;
    MY_ANSI_STRING      Src;

    birdResolveImports();

    /*
     * Just convert to wide char.
     */
    pNtPath->Length   = pNtPath->MaximumLength = 0;
    pNtPath->Buffer   = NULL;

    Src.Buffer        = (PCHAR)pszPath;
    Src.MaximumLength = Src.Length = (USHORT)strlen(pszPath);

    rcNt = g_pfnRtlAnsiStringToUnicodeString(pNtPath, &Src, TRUE /* Allocate */);
    if (MY_NT_SUCCESS(rcNt))
        return birdFixRelativeNtPathSlashesAndReturn0(pNtPath);
    return birdSetErrnoFromNt(rcNt);
}


/**
 * Similar to birdDosToNtPathW, but it does call RtlDosPathNameToNtPathName_U.
 *
 * @returns 0 on success, -1 + errno on failure.
 * @param   pwszPath    The relative path.
 * @param   pNtPath     Where to return the NT path.  Call birdFreeNtPath when done.
 */
int birdDosToRelativeNtPathW(const wchar_t *pwszPath, MY_UNICODE_STRING *pNtPath)
{
    size_t cwcPath = wcslen(pwszPath);
    if (cwcPath < 0xfffe)
    {
        pNtPath->Length = (USHORT)(cwcPath * sizeof(wchar_t));
        pNtPath->MaximumLength = pNtPath->Length + sizeof(wchar_t);
        pNtPath->Buffer = HeapAlloc(GetProcessHeap(), 0, pNtPath->MaximumLength);
        if (pNtPath->Buffer)
        {
            memcpy(pNtPath->Buffer, pwszPath, pNtPath->MaximumLength);
            return birdFixRelativeNtPathSlashesAndReturn0(pNtPath);
        }
        errno = ENOMEM;
    }
    else
        errno = ENAMETOOLONG;
    return -1;
}


/**
 * Frees a string returned by birdDosToNtPath, birdDosToNtPathW or
 * birdDosToRelativeNtPath.
 *
 * @param   pNtPath             The the NT path to free.
 */
void birdFreeNtPath(MY_UNICODE_STRING *pNtPath)
{
    HeapFree(GetProcessHeap(), 0, pNtPath->Buffer);
    pNtPath->Buffer = NULL;
    pNtPath->Length = 0;
    pNtPath->MaximumLength = 0;
}


MY_NTSTATUS birdOpenFileUniStr(HANDLE hRoot, MY_UNICODE_STRING *pNtPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                               ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs,
                               HANDLE *phFile)
{
    MY_IO_STATUS_BLOCK      Ios;
    MY_OBJECT_ATTRIBUTES    ObjAttr;
    MY_NTSTATUS             rcNt;

    birdResolveImports();

    if (  (fCreateOptions & FILE_OPEN_REPARSE_POINT)
        && g_fHaveOpenReparsePoint == 0)
        fCreateOptions &= ~FILE_OPEN_REPARSE_POINT;

    Ios.Information = -1;
    Ios.u.Status = 0;
    MyInitializeObjectAttributes(&ObjAttr, pNtPath, fObjAttribs, hRoot, NULL /*pSecAttr*/);

    rcNt = g_pfnNtCreateFile(phFile,
                             fDesiredAccess,
                             &ObjAttr,
                             &Ios,
                             NULL,   /* cbFileInitialAlloc */
                             fFileAttribs,
                             fShareAccess,
                             fCreateDisposition,
                             fCreateOptions,
                             NULL,   /* pEaBuffer */
                             0);     /* cbEaBuffer*/
    if (   rcNt == STATUS_INVALID_PARAMETER
        && g_fHaveOpenReparsePoint < 0
        && (fCreateOptions & FILE_OPEN_REPARSE_POINT))
    {
        fCreateOptions &= ~FILE_OPEN_REPARSE_POINT;

        Ios.Information = -1;
        Ios.u.Status = 0;
        MyInitializeObjectAttributes(&ObjAttr, pNtPath, fObjAttribs, NULL /*hRoot*/, NULL /*pSecAttr*/);

        rcNt = g_pfnNtCreateFile(phFile,
                                 fDesiredAccess,
                                 &ObjAttr,
                                 &Ios,
                                 NULL,   /* cbFileInitialAlloc */
                                 fFileAttribs,
                                 fShareAccess,
                                 fCreateDisposition,
                                 fCreateOptions,
                                 NULL,   /* pEaBuffer */
                                 0);     /* cbEaBuffer*/
        if (rcNt != STATUS_INVALID_PARAMETER)
            g_fHaveOpenReparsePoint = 0;
    }
    return rcNt;
}


HANDLE birdOpenFile(const char *pszPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                    ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs)
{
    MY_UNICODE_STRING   NtPath;
    MY_NTSTATUS         rcNt;

    /*
     * Adjust inputs.
     */
    if (birdIsPathDirSpec(pszPath))
        fCreateOptions |= FILE_DIRECTORY_FILE;

    /*
     * Convert the path and call birdOpenFileUniStr to do the real work.
     */
    if (birdDosToNtPath(pszPath, &NtPath) == 0)
    {
        HANDLE hFile;
        rcNt = birdOpenFileUniStr(NULL /*hRoot*/, &NtPath, fDesiredAccess, fFileAttribs, fShareAccess,
                                  fCreateDisposition, fCreateOptions, fObjAttribs, &hFile);
        birdFreeNtPath(&NtPath);
        if (MY_NT_SUCCESS(rcNt))
            return hFile;
        birdSetErrnoFromNt(rcNt);
    }

    return INVALID_HANDLE_VALUE;
}


HANDLE birdOpenFileW(const wchar_t *pwszPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                     ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs)
{
    MY_UNICODE_STRING   NtPath;
    MY_NTSTATUS         rcNt;

    /*
     * Adjust inputs.
     */
    if (birdIsPathDirSpecW(pwszPath))
        fCreateOptions |= FILE_DIRECTORY_FILE;

    /*
     * Convert the path and call birdOpenFileUniStr to do the real work.
     */
    if (birdDosToNtPathW(pwszPath, &NtPath) == 0)
    {
        HANDLE hFile;
        rcNt = birdOpenFileUniStr(NULL /*hRoot*/, &NtPath, fDesiredAccess, fFileAttribs, fShareAccess,
                                  fCreateDisposition, fCreateOptions, fObjAttribs, &hFile);
        birdFreeNtPath(&NtPath);
        if (MY_NT_SUCCESS(rcNt))
            return hFile;
        birdSetErrnoFromNt(rcNt);
    }

    return INVALID_HANDLE_VALUE;
}


HANDLE birdOpenFileEx(HANDLE hRoot, const char *pszPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                      ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs)
{
    MY_UNICODE_STRING   NtPath;
    MY_NTSTATUS         rcNt;

    /*
     * Adjust inputs.
     */
    if (birdIsPathDirSpec(pszPath))
        fCreateOptions |= FILE_DIRECTORY_FILE;

    /*
     * Convert the path and call birdOpenFileUniStr to do the real work.
     */
    if (hRoot == INVALID_HANDLE_VALUE)
        hRoot = NULL;
    if ((hRoot != NULL ? birdDosToRelativeNtPath(pszPath, &NtPath) : birdDosToNtPath(pszPath, &NtPath)) == 0)
    {
        HANDLE hFile;
        rcNt = birdOpenFileUniStr(hRoot, &NtPath, fDesiredAccess, fFileAttribs, fShareAccess,
                                  fCreateDisposition, fCreateOptions, fObjAttribs, &hFile);
        birdFreeNtPath(&NtPath);
        if (MY_NT_SUCCESS(rcNt))
            return hFile;
        birdSetErrnoFromNt(rcNt);
    }

    return INVALID_HANDLE_VALUE;
}


HANDLE birdOpenFileExW(HANDLE hRoot, const wchar_t *pwszPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                       ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs)
{
    MY_UNICODE_STRING   NtPath;
    MY_NTSTATUS         rcNt;

    /*
     * Adjust inputs.
     */
    if (birdIsPathDirSpecW(pwszPath))
        fCreateOptions |= FILE_DIRECTORY_FILE;

    /*
     * Convert the path (could save ourselves this if pwszPath is perfect) and
     * call birdOpenFileUniStr to do the real work.
     */
    if (hRoot == INVALID_HANDLE_VALUE)
        hRoot = NULL;
    if ((hRoot != NULL ? birdDosToRelativeNtPathW(pwszPath, &NtPath) : birdDosToNtPathW(pwszPath, &NtPath)) == 0)
    {
        HANDLE hFile;
        rcNt = birdOpenFileUniStr(hRoot, &NtPath, fDesiredAccess, fFileAttribs, fShareAccess,
                                  fCreateDisposition, fCreateOptions, fObjAttribs, &hFile);
        birdFreeNtPath(&NtPath);
        if (MY_NT_SUCCESS(rcNt))
            return hFile;
        birdSetErrnoFromNt(rcNt);
    }

    return INVALID_HANDLE_VALUE;
}


static HANDLE birdOpenParentDirCommon(HANDLE hRoot, MY_UNICODE_STRING *pNtPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                                      ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs,
                                      MY_UNICODE_STRING *pNameUniStr)
{
    MY_NTSTATUS rcNt;

    /*
     * Strip the path down to the directory.
     */
    USHORT offName = pNtPath->Length / sizeof(WCHAR);
    USHORT cwcName = offName;
    WCHAR  wc = 0;
    while (   offName > 0
           && (wc = pNtPath->Buffer[offName - 1]) != '\\'
           && wc != '/'
           && wc != ':')
        offName--;
    if (   offName > 0
        || (hRoot != NULL && cwcName > 0))
    {
        cwcName -= offName;

        /* Make a copy of the file name, if requested. */
        rcNt = STATUS_SUCCESS;
        if (pNameUniStr)
        {
            pNameUniStr->Length        = cwcName * sizeof(WCHAR);
            pNameUniStr->MaximumLength = pNameUniStr->Length + sizeof(WCHAR);
            pNameUniStr->Buffer        = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, pNameUniStr->MaximumLength);
            if (pNameUniStr->Buffer)
            {
                memcpy(pNameUniStr->Buffer, &pNtPath->Buffer[offName], pNameUniStr->Length);
                pNameUniStr->Buffer[cwcName] = '\0';
            }
            else
                rcNt = STATUS_NO_MEMORY;
        }

        /* Chop, chop. */
        // Bad idea, breaks \\?\c:\pagefile.sys. //while (   offName > 0
        // Bad idea, breaks \\?\c:\pagefile.sys. //       && (   (wc = pNtPath->Buffer[offName - 1]) == '\\'
        // Bad idea, breaks \\?\c:\pagefile.sys. //           || wc == '/'))
        // Bad idea, breaks \\?\c:\pagefile.sys. //    offName--;
        if (offName == 0)
            pNtPath->Buffer[offName++] = '.'; /* Hack for dir handle + dir entry name. */
        pNtPath->Length = offName * sizeof(WCHAR);
        pNtPath->Buffer[offName] = '\0';
        if (MY_NT_SUCCESS(rcNt))
        {
            /*
             * Finally, try open the directory.
             */
            HANDLE hFile;
            fCreateOptions |= FILE_DIRECTORY_FILE;
            rcNt = birdOpenFileUniStr(hRoot, pNtPath, fDesiredAccess, fFileAttribs, fShareAccess,
                                      fCreateDisposition, fCreateOptions, fObjAttribs, &hFile);
            if (MY_NT_SUCCESS(rcNt))
            {
                birdFreeNtPath(pNtPath);
                return hFile;
            }
        }

        if (pNameUniStr)
            birdFreeNtPath(pNameUniStr);
    }
    else
        rcNt = STATUS_INVALID_PARAMETER;

    birdFreeNtPath(pNtPath);
    birdSetErrnoFromNt(rcNt);
    return INVALID_HANDLE_VALUE;
}


HANDLE birdOpenParentDir(HANDLE hRoot, const char *pszPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                         ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs,
                         MY_UNICODE_STRING *pNameUniStr)
{
    /*
     * Convert the path and join up with the UTF-16 version (it'll free NtPath).
     */
    MY_UNICODE_STRING NtPath;
    if (hRoot == INVALID_HANDLE_VALUE)
        hRoot = NULL;
    if (  hRoot == NULL
        ? birdDosToNtPath(pszPath, &NtPath) == 0
        : birdDosToRelativeNtPath(pszPath, &NtPath) == 0)
        return birdOpenParentDirCommon(hRoot, &NtPath, fDesiredAccess, fFileAttribs, fShareAccess,
                                       fCreateDisposition, fCreateOptions, fObjAttribs, pNameUniStr);
    return INVALID_HANDLE_VALUE;
}


HANDLE birdOpenParentDirW(HANDLE hRoot, const wchar_t *pwszPath, ACCESS_MASK fDesiredAccess, ULONG fFileAttribs,
                          ULONG fShareAccess, ULONG fCreateDisposition, ULONG fCreateOptions, ULONG fObjAttribs,
                          MY_UNICODE_STRING *pNameUniStr)
{
    /*
     * Convert the path and join up with the ansi version (it'll free NtPath).
     */
    MY_UNICODE_STRING NtPath;
    if (hRoot == INVALID_HANDLE_VALUE)
        hRoot = NULL;
    if (  hRoot == NULL
        ? birdDosToNtPathW(pwszPath, &NtPath) == 0
        : birdDosToRelativeNtPathW(pwszPath, &NtPath) == 0)
        return birdOpenParentDirCommon(hRoot, &NtPath, fDesiredAccess, fFileAttribs, fShareAccess,
                                       fCreateDisposition, fCreateOptions, fObjAttribs, pNameUniStr);
    return INVALID_HANDLE_VALUE;
}


/**
 * Returns a handle to the current working directory of the process.
 *
 * @returns CWD handle with FILE_TRAVERSE and SYNCHRONIZE access.  May return
 *          INVALID_HANDLE_VALUE w/ errno for invalid CWD.
 */
HANDLE birdOpenCurrentDirectory(void)
{
    PMY_RTL_USER_PROCESS_PARAMETERS pProcParams;
    MY_NTSTATUS rcNt;
    HANDLE hRet = INVALID_HANDLE_VALUE;

    birdResolveImports();

    /*
     * We'll try get this from the PEB.
     */
    g_pfnRtlAcquirePebLock();
    pProcParams = (PMY_RTL_USER_PROCESS_PARAMETERS)MY_NT_CURRENT_PEB()->ProcessParameters;
    if (pProcParams != NULL)
        rcNt = g_pfnNtDuplicateObject(MY_NT_CURRENT_PROCESS, pProcParams->CurrentDirectory.Handle,
                                      MY_NT_CURRENT_PROCESS, &hRet,
                                      FILE_TRAVERSE | SYNCHRONIZE,
                                      0 /*fAttribs*/,
                                      0 /*fOptions*/);
    else
        rcNt = STATUS_INVALID_PARAMETER;
    g_pfnRtlReleasePebLock();
    if (MY_NT_SUCCESS(rcNt))
        return hRet;

    /*
     * Fallback goes thru birdOpenFileW.
     */
    return birdOpenFileW(L".",
                         FILE_TRAVERSE | SYNCHRONIZE,
                         FILE_ATTRIBUTE_NORMAL,
                         FILE_SHARE_READ | FILE_SHARE_WRITE,
                         FILE_OPEN,
                         FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
                         OBJ_CASE_INSENSITIVE);
}


void birdCloseFile(HANDLE hFile)
{
    birdResolveImports();
    g_pfnNtClose(hFile);
}