[3682] | 1 | /* $Id: ntmkdirat.c 3682 2025-08-12 23:34:19Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * MSC + NT mkdir & mkdirat.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2025 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
---|
| 8 | *
|
---|
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
| 10 | * copy of this software and associated documentation files (the "Software"),
|
---|
| 11 | * to deal in the Software without restriction, including without limitation
|
---|
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
| 13 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
| 14 | * Software is furnished to do so, subject to the following conditions:
|
---|
| 15 | *
|
---|
| 16 | * The above copyright notice and this permission notice shall be included
|
---|
| 17 | * in all copies or substantial portions of the Software.
|
---|
| 18 | *
|
---|
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
---|
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
---|
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
---|
| 25 | * IN THE SOFTWARE.
|
---|
| 26 | *
|
---|
| 27 | * Alternatively, the content of this file may be used under the terms of the
|
---|
| 28 | * GPL version 2 or later, or LGPL version 2.1 or later.
|
---|
| 29 | */
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | /*******************************************************************************
|
---|
| 33 | * Header Files *
|
---|
| 34 | *******************************************************************************/
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <fcntl.h>
|
---|
| 38 | #include <io.h>
|
---|
| 39 |
|
---|
| 40 | #include "ntstuff.h"
|
---|
| 41 | #include "nthlp.h"
|
---|
| 42 | #include "nthlpmisc.h"
|
---|
| 43 | #include "ntmkdirat.h"
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Implements mkdir.
|
---|
| 49 | */
|
---|
| 50 | int birdMkDir(const char *pszPath, unsigned __int16 fMode)
|
---|
| 51 | {
|
---|
| 52 | HANDLE hDir = birdOpenFile(pszPath,
|
---|
| 53 | FILE_LIST_DIRECTORY | SYNCHRONIZE,
|
---|
| 54 | FILE_ATTRIBUTE_NORMAL,
|
---|
| 55 | FILE_SHARE_READ | FILE_SHARE_WRITE /*| FILE_SHARE_DELETE*/,
|
---|
| 56 | FILE_CREATE,
|
---|
| 57 | FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
|
---|
| 58 | OBJ_CASE_INSENSITIVE);
|
---|
| 59 | if (hDir != INVALID_HANDLE_VALUE)
|
---|
| 60 | {
|
---|
| 61 | birdCloseFile(hDir);
|
---|
| 62 | return 0;
|
---|
| 63 | }
|
---|
| 64 | (void)fMode;
|
---|
| 65 | return -1;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Implements mkdirat.
|
---|
| 71 | */
|
---|
| 72 | int birdMkDirAt(int fdDir, const char *pszPath, unsigned __int16 fMode)
|
---|
| 73 | {
|
---|
| 74 | HANDLE hDirParent;
|
---|
| 75 |
|
---|
| 76 | /*
|
---|
| 77 | * Redirect to regular 'mkdir' when we can.
|
---|
| 78 | */
|
---|
| 79 | if (fdDir == AT_FDCWD)
|
---|
| 80 | return birdMkDir(pszPath, fMode);
|
---|
| 81 |
|
---|
| 82 | if (IS_SLASH(pszPath[0]))
|
---|
| 83 | {
|
---|
| 84 | if (IS_SLASH(pszPath[1]) && !IS_SLASH(pszPath[2]) && pszPath[2] != '\0')
|
---|
| 85 | return birdMkDir(pszPath, fMode);
|
---|
| 86 | }
|
---|
| 87 | else if (IS_ALPHA(pszPath[0]) && pszPath[1] == ':')
|
---|
| 88 | {
|
---|
| 89 | if (IS_SLASH(pszPath[2]))
|
---|
| 90 | return birdMkDir(pszPath, fMode);
|
---|
| 91 | /*
|
---|
| 92 | * Drive letter relative path like "C:kernel32.dll".
|
---|
| 93 | * We could try use fdDir as the CWD here if it refers to the same drive,
|
---|
| 94 | * however that's can be implemented later...
|
---|
| 95 | */
|
---|
| 96 | return birdMkDir(pszPath, fMode);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | * Otherwise, we leave to to NT to get this done... It isn't necessarily as
|
---|
| 101 | * atomic as the opengroup spec wants, but we cannot do much better.
|
---|
| 102 | */
|
---|
| 103 | hDirParent = (HANDLE)_get_osfhandle(fdDir);
|
---|
| 104 | if (hDirParent != INVALID_HANDLE_VALUE)
|
---|
| 105 | {
|
---|
| 106 | HANDLE hDir = birdOpenFileEx(hDirParent,
|
---|
| 107 | pszPath,
|
---|
| 108 | FILE_LIST_DIRECTORY | SYNCHRONIZE,
|
---|
| 109 | FILE_ATTRIBUTE_NORMAL,
|
---|
| 110 | FILE_SHARE_READ | FILE_SHARE_WRITE /*| FILE_SHARE_DELETE*/,
|
---|
| 111 | FILE_CREATE,
|
---|
| 112 | FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT | FILE_SYNCHRONOUS_IO_NONALERT,
|
---|
| 113 | OBJ_CASE_INSENSITIVE);
|
---|
| 114 | if (hDir != INVALID_HANDLE_VALUE)
|
---|
| 115 | {
|
---|
| 116 | birdCloseFile(hDir);
|
---|
| 117 | return 0;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | return -1;
|
---|
| 121 | }
|
---|
| 122 |
|
---|