[1] | 1 | /* $Id: dir-win.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[47534] | 3 | * IPRT - Directory, Windows.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_DIR
|
---|
[62592] | 42 | #include <iprt/win/windows.h>
|
---|
[1] | 43 |
|
---|
| 44 | #include <iprt/dir.h>
|
---|
| 45 | #include <iprt/path.h>
|
---|
[47534] | 46 | #include <iprt/mem.h>
|
---|
[1] | 47 | #include <iprt/string.h>
|
---|
| 48 | #include <iprt/assert.h>
|
---|
| 49 | #include <iprt/err.h>
|
---|
| 50 | #include <iprt/file.h>
|
---|
| 51 | #include <iprt/log.h>
|
---|
| 52 | #include "internal/fs.h"
|
---|
| 53 | #include "internal/path.h"
|
---|
| 54 |
|
---|
| 55 |
|
---|
| 56 |
|
---|
[39612] | 57 | RTDECL(int) RTDirCreate(const char *pszPath, RTFMODE fMode, uint32_t fCreate)
|
---|
[1] | 58 | {
|
---|
| 59 | /*
|
---|
| 60 | * Validate the file mode.
|
---|
| 61 | */
|
---|
| 62 | int rc;
|
---|
[79155] | 63 | fMode = rtFsModeNormalize(fMode, pszPath, 0, RTFS_TYPE_DIRECTORY);
|
---|
[1] | 64 | if (rtFsModeIsValidPermissions(fMode))
|
---|
| 65 | {
|
---|
| 66 | /*
|
---|
[7418] | 67 | * Convert to UTF-16.
|
---|
[1] | 68 | */
|
---|
[7418] | 69 | PRTUTF16 pwszString;
|
---|
[74460] | 70 | rc = RTPathWinFromUtf8(&pwszString, pszPath, 0 /*fFlags*/);
|
---|
[1] | 71 | AssertRC(rc);
|
---|
| 72 | if (RT_SUCCESS(rc))
|
---|
| 73 | {
|
---|
| 74 | /*
|
---|
| 75 | * Create the directory.
|
---|
| 76 | */
|
---|
[7418] | 77 | if (CreateDirectoryW((LPCWSTR)pwszString, NULL))
|
---|
[1] | 78 | rc = VINF_SUCCESS;
|
---|
| 79 | else
|
---|
| 80 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
| 81 |
|
---|
[6971] | 82 | /*
|
---|
| 83 | * Turn off indexing of directory through Windows Indexing Service
|
---|
| 84 | */
|
---|
[49078] | 85 | /** @todo This FILE_ATTRIBUTE_NOT_CONTENT_INDEXED hack (for .VDI files,
|
---|
| 86 | * really) may cause failures on samba shares. That really sweet and
|
---|
| 87 | * need to be addressed differently. We shouldn't be doing this
|
---|
| 88 | * unless the caller actually asks for it, must less returning failure,
|
---|
| 89 | * for crying out loud! This is only important a couple of places in
|
---|
| 90 | * main, if important is the right way to put it... */
|
---|
| 91 | if ( RT_SUCCESS(rc)
|
---|
| 92 | && !(fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_DONT_SET))
|
---|
[6971] | 93 | {
|
---|
[49078] | 94 | if ( SetFileAttributesW((LPCWSTR)pwszString, FILE_ATTRIBUTE_NOT_CONTENT_INDEXED)
|
---|
| 95 | || (fCreate & RTDIRCREATE_FLAGS_NOT_CONTENT_INDEXED_NOT_CRITICAL) )
|
---|
[6971] | 96 | rc = VINF_SUCCESS;
|
---|
| 97 | else
|
---|
| 98 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[74460] | 101 | RTPathWinFree(pwszString);
|
---|
[1] | 102 | }
|
---|
| 103 | }
|
---|
| 104 | else
|
---|
| 105 | {
|
---|
| 106 | AssertMsgFailed(("Invalid file mode! %RTfmode\n", fMode));
|
---|
| 107 | rc = VERR_INVALID_FMODE;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | LogFlow(("RTDirCreate(%p:{%s}, %RTfmode): returns %Rrc\n", pszPath, pszPath, fMode, rc));
|
---|
| 111 | return rc;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | RTDECL(int) RTDirRemove(const char *pszPath)
|
---|
| 116 | {
|
---|
| 117 | /*
|
---|
[7418] | 118 | * Convert to UTF-16.
|
---|
[1] | 119 | */
|
---|
[7418] | 120 | PRTUTF16 pwszString;
|
---|
[74460] | 121 | int rc = RTPathWinFromUtf8(&pwszString, pszPath, 0 /*fFlags*/);
|
---|
[1] | 122 | AssertRC(rc);
|
---|
| 123 | if (RT_SUCCESS(rc))
|
---|
| 124 | {
|
---|
| 125 | /*
|
---|
| 126 | * Remove the directory.
|
---|
| 127 | */
|
---|
[7418] | 128 | if (RemoveDirectoryW((LPCWSTR)pwszString))
|
---|
[1] | 129 | rc = VINF_SUCCESS;
|
---|
| 130 | else
|
---|
| 131 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
| 132 |
|
---|
[74460] | 133 | RTPathWinFree(pwszString);
|
---|
[1] | 134 | }
|
---|
| 135 |
|
---|
| 136 | LogFlow(("RTDirRemove(%p:{%s}): returns %Rrc\n", pszPath, pszPath, rc));
|
---|
| 137 | return rc;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
[28688] | 141 | RTDECL(int) RTDirFlush(const char *pszPath)
|
---|
| 142 | {
|
---|
[62584] | 143 | RT_NOREF_PV(pszPath);
|
---|
[28688] | 144 | return VERR_NOT_SUPPORTED;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
[1] | 148 | RTDECL(int) RTDirRename(const char *pszSrc, const char *pszDst, unsigned fRename)
|
---|
| 149 | {
|
---|
| 150 | /*
|
---|
| 151 | * Validate input.
|
---|
| 152 | */
|
---|
[90781] | 153 | AssertPtrReturn(pszSrc, VERR_INVALID_POINTER);
|
---|
| 154 | AssertPtrReturn(pszDst, VERR_INVALID_POINTER);
|
---|
[1] | 155 | AssertMsgReturn(*pszSrc, ("%p\n", pszSrc), VERR_INVALID_PARAMETER);
|
---|
| 156 | AssertMsgReturn(*pszDst, ("%p\n", pszDst), VERR_INVALID_PARAMETER);
|
---|
| 157 | AssertMsgReturn(!(fRename & ~RTPATHRENAME_FLAGS_REPLACE), ("%#x\n", fRename), VERR_INVALID_PARAMETER);
|
---|
| 158 |
|
---|
| 159 | /*
|
---|
| 160 | * Call the worker.
|
---|
| 161 | */
|
---|
| 162 | int rc = rtPathWin32MoveRename(pszSrc, pszDst,
|
---|
| 163 | fRename & RTPATHRENAME_FLAGS_REPLACE ? MOVEFILE_REPLACE_EXISTING : 0,
|
---|
| 164 | RTFS_TYPE_DIRECTORY);
|
---|
| 165 |
|
---|
| 166 | LogFlow(("RTDirRename(%p:{%s}, %p:{%s}, %#x): returns %Rrc\n", pszSrc, pszSrc, pszDst, pszDst, fRename, rc));
|
---|
| 167 | return rc;
|
---|
| 168 | }
|
---|
| 169 |
|
---|